/// <summary> /// Initializes a new instance from the specified <paramref name="timestamp"/>. /// </summary> /// <param name="timestamp">The timestamp.</param> public EssentialsWeek(DateTimeOffset timestamp) { Week = TimeUtils.GetIso8601WeekNumber(timestamp); Start = TimeUtils.GetStartOfWeek(timestamp); End = TimeUtils.GetEndOfWeek(timestamp); Year = GetYear(); }
/// <summary> /// Returns a <see cref="EssentialsPeriod"/> representing the upcoming weekend relative to the specified /// <paramref name="timestamp"/> and according to <paramref name="timeZone"/>. /// /// If <paramref name="timestamp"/> represents a Saturday or a Sunday, the returned period will representing /// the weekend of the next week instead. /// </summary> /// <param name="timestamp">The timestamp.</param> /// <param name="timeZone">The time zone.</param> /// <returns>An instance of <see cref="EssentialsPeriod"/>.</returns> public static EssentialsPeriod NextWeekend(DateTimeOffset timestamp, TimeZoneInfo timeZone) { // Time zone may not be null if (timeZone == null) { throw new ArgumentNullException(nameof(timeZone)); } // Convert "timestamp" to same offset as "timeZone" timestamp = TimeZoneInfo.ConvertTime(timestamp, timeZone); // If "dto" is already within a weekend, we jump to the next week instead if (timestamp.DayOfWeek == DayOfWeek.Saturday || timestamp.DayOfWeek == DayOfWeek.Sunday) { // Adjust to UTC and add seven days (aka a full week) timestamp = timestamp.ToUniversalTime().AddDays(7); // Adjust back to the input time zone timestamp = TimeZoneInfo.ConvertTime(timestamp, timeZone); } DateTimeOffset start = TimeUtils.GetStartOfWeek(timestamp, timeZone).AddDays(5); DateTimeOffset end = TimeUtils.GetEndOfWeek(timestamp, timeZone); // Wrap the result in a new EssentialsPeriod return(new EssentialsPeriod(start, end, timeZone)); }
/// <summary> /// Initializes a new instance from the specified <paramref name="timestamp"/>. /// </summary> /// <param name="timestamp">The timestamp.</param> public EssentialsWeek(EssentialsTime timestamp) { Week = TimeUtils.GetIso8601WeekNumber(timestamp.DateTimeOffset); Start = TimeUtils.GetStartOfWeek(timestamp.DateTimeOffset, timestamp.TimeZone); End = TimeUtils.GetEndOfWeek(timestamp.DateTimeOffset, timestamp.TimeZone); Year = GetYear(); }
/// <summary> /// Initializes a new instance from the specified <paramref name="timestamp"/> and <paramref name="timeZone"/>. /// </summary> /// <param name="timestamp">The timestamp.</param> /// <param name="timeZone">The time zone.</param> public EssentialsWeek(DateTimeOffset timestamp, TimeZoneInfo timeZone) { WeekNumber = Iso8601Utils.GetWeekNumber(timestamp); Start = TimeUtils.GetStartOfWeek(timestamp, timeZone); End = TimeUtils.GetEndOfWeek(timestamp, timeZone); Year = GetYear(); }