/// <summary> /// Build the <see cref="DateTimeOffset" /> defined by this builder instance. /// </summary> /// <returns>New date time based on builder parameters.</returns> public DateTimeOffset Build() { DateTime dt = new DateTime(year, month, day, hour, minute, second); TimeSpan offset = TimeZoneUtil.GetUtcOffset(dt, tz ?? TimeZoneInfo.Local); return(new DateTimeOffset(dt, offset)); }
/// <summary> /// Get a <see cref="DateTimeOffset" /> object that represents the given time, on today's date. /// </summary> /// <param name="second">The value (0-59) to give the seconds field of the date</param> /// <param name="minute">The value (0-59) to give the minutes field of the date</param> /// <param name="hour">The value (0-23) to give the hours field of the date</param> /// <returns>the new date</returns> public static DateTimeOffset DateOf(int hour, int minute, int second) { ValidateSecond(second); ValidateMinute(minute); ValidateHour(hour); DateTimeOffset c = SystemTime.Now(); DateTime dt = new DateTime(c.Year, c.Month, c.Day, hour, minute, second); return(new DateTimeOffset(dt, TimeZoneUtil.GetUtcOffset(dt, TimeZoneInfo.Local))); }
/// <summary> /// Get a <see cref="DateTimeOffset" /> object that represents the given time, on the /// given date. /// </summary> /// <remarks> /// </remarks> /// <param name="second">The value (0-59) to give the seconds field of the date</param> /// <param name="minute">The value (0-59) to give the minutes field of the date</param> /// <param name="hour">The value (0-23) to give the hours field of the date</param> /// <param name="dayOfMonth">The value (1-31) to give the day of month field of the date</param> /// <param name="month">The value (1-12) to give the month field of the date</param> /// <param name="year">The value (1970-2099) to give the year field of the date</param> /// <returns>the new date</returns> public static DateTimeOffset DateOf(int hour, int minute, int second, int dayOfMonth, int month, int year) { ValidateSecond(second); ValidateMinute(minute); ValidateHour(hour); ValidateDayOfMonth(dayOfMonth); ValidateMonth(month); ValidateYear(year); DateTime dt = new DateTime(year, month, dayOfMonth, hour, minute, second); return(new DateTimeOffset(dt, TimeZoneUtil.GetUtcOffset(dt, TimeZoneInfo.Local))); }
/// <summary> /// Calculate and set the EndTimeOfDay using count, interval and StarTimeOfDay. This means /// that these must be set before this method is call. /// </summary> /// <param name="count"></param> /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns> public DailyTimeIntervalScheduleBuilder EndingDailyAfterCount(int count) { if (count <= 0) { throw new ArgumentException("Ending daily after count must be a positive number!"); } if (startTimeOfDayUtc == null) { throw new ArgumentException("You must set the StartDailyAt() before calling this EndingDailyAfterCount()!"); } DateTimeOffset today = SystemTime.UtcNow(); DateTimeOffset startTimeOfDayDate = startTimeOfDayUtc.GetTimeOfDayForDate(today); DateTimeOffset maxEndTimeOfDayDate = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59).GetTimeOfDayForDate(today); //apply proper offsets according to timezone TimeZoneInfo targetTimeZone = timeZone ?? TimeZoneInfo.Local; startTimeOfDayDate = new DateTimeOffset(startTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(startTimeOfDayDate.DateTime, targetTimeZone)); maxEndTimeOfDayDate = new DateTimeOffset(maxEndTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(maxEndTimeOfDayDate.DateTime, targetTimeZone)); TimeSpan remainingMillisInDay = maxEndTimeOfDayDate - startTimeOfDayDate; TimeSpan intervalInMillis; if (intervalUnit == IntervalUnit.Second) { intervalInMillis = TimeSpan.FromSeconds(interval); } else if (intervalUnit == IntervalUnit.Minute) { intervalInMillis = TimeSpan.FromMinutes(interval); } else if (intervalUnit == IntervalUnit.Hour) { intervalInMillis = TimeSpan.FromHours(interval); } else { throw new ArgumentException("The IntervalUnit: " + intervalUnit + " is invalid for this trigger."); } if (remainingMillisInDay < intervalInMillis) { throw new ArgumentException("The startTimeOfDay is too late with given Interval and IntervalUnit values."); } long maxNumOfCount = remainingMillisInDay.Ticks / intervalInMillis.Ticks; if (count > maxNumOfCount) { throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount); } TimeSpan incrementInMillis = TimeSpan.FromTicks((count - 1) * intervalInMillis.Ticks); DateTimeOffset endTimeOfDayDate = startTimeOfDayDate.Add(incrementInMillis); if (endTimeOfDayDate > maxEndTimeOfDayDate) { throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount); } DateTime cal = SystemTime.UtcNow().Date; cal = cal.Add(endTimeOfDayDate.TimeOfDay); endTimeOfDayUtc = TimeOfDay.HourMinuteAndSecondOfDay(cal.Hour, cal.Minute, cal.Second); return(this); }