public static DateTimeEx RollOne( DateTimeEx dateTime, string fieldName, bool flagValue) { switch (fieldName) { case "date": return(flagValue ? dateTime.AddDays(1) : dateTime.AddDays(-1)); default: throw new EPException("Invalid field name '" + fieldName + "'"); } }
/// <summary> /// NOTE: Code-generation-invoked method, method name and parameter order matters /// </summary> /// <param name="dtx">calendar</param> /// <param name="factor">factor</param> /// <param name="tp">duration</param> public static DateTimeEx ActionCalendarPlusMinusTimePeriod( DateTimeEx dtx, int factor, TimePeriod tp) { if (tp == null) { return dtx; } if (tp.Years != null) { dtx.AddYears(factor * tp.Years.Value); } if (tp.Months != null) { dtx.AddMonths(factor * tp.Months.Value); } if (tp.Weeks != null) { dtx.AddDays(factor * tp.Weeks.Value * 7); } if (tp.Days != null) { dtx.AddDays(factor * tp.Days.Value); } if (tp.Hours != null) { dtx.AddHours(factor * tp.Hours.Value); } if (tp.Minutes != null) { dtx.AddMinutes(factor * tp.Minutes.Value); } if (tp.Seconds != null) { dtx.AddSeconds(factor * tp.Seconds.Value); } if (tp.Milliseconds != null) { dtx.AddMilliseconds(factor * tp.Milliseconds.Value); } return dtx; }
public static void Action(DateTimeEx dateTime, int factor, TimePeriod tp) { if (tp == null) { return; } if (tp.Years != null) { dateTime.AddYears(tp.Years.Value * factor); } if (tp.Months != null) { dateTime.AddMonths(tp.Months.Value * factor, DateTimeMathStyle.Java); } if (tp.Weeks != null) { dateTime.AddDays(tp.Weeks.Value * 7 * factor, DateTimeMathStyle.Java); } if (tp.Days != null) { dateTime.AddDays(tp.Days.Value * factor, DateTimeMathStyle.Java); } if (tp.Hours != null) { dateTime.AddHours(tp.Hours.Value * factor, DateTimeMathStyle.Java); } if (tp.Minutes != null) { dateTime.AddMinutes(tp.Minutes.Value * factor, DateTimeMathStyle.Java); } if (tp.Seconds != null) { dateTime.AddSeconds(tp.Seconds.Value * factor, DateTimeMathStyle.Java); } if (tp.Milliseconds != null) { dateTime.AddMilliseconds(tp.Milliseconds.Value * factor, DateTimeMathStyle.Java); } }
/// <summary> /// NOTE: Code-generation-invoked method, method name and parameter order matters /// </summary> /// <param name="dtx">calendar</param> /// <param name="factor">factor</param> /// <param name="duration">duration</param> public static DateTimeEx ActionCalendarPlusMinusNumber( DateTimeEx dtx, int factor, long? duration) { if (duration == null) { return dtx; } if (duration < int.MaxValue) { dtx.AddMilliseconds((int) (factor * duration)); return dtx; } var days = (int) (duration / (1000L * 60 * 60 * 24)); var msec = (int) (duration - days * 1000L * 60 * 60 * 24); dtx.AddMilliseconds(factor * msec); dtx.AddDays(factor * days); return dtx; }
public void Add(DateTimeEx dateTime, int value) { dateTime.AddDays(value, DateTimeMathStyle.Java); }
/// <summary> /// Determine the next valid day of month based on the given specification of valid days in month and /// valid days in week. If both days in week and days in month are supplied, the days are OR-ed. /// </summary> /// <param name="spec"></param> /// <param name="after"></param> /// <param name="result"></param> /// <returns></returns> private static int DetermineDayOfMonth(ScheduleSpec spec, DateTimeEx after, ScheduleCalendar result) { ICollection <Int32> daysOfMonthSet = spec.UnitValues.Get(ScheduleUnit.DAYS_OF_MONTH); ICollection <Int32> daysOfWeekSet = spec.UnitValues.Get(ScheduleUnit.DAYS_OF_WEEK); ICollection <Int32> secondsSet = spec.UnitValues.Get(ScheduleUnit.SECONDS); ICollection <Int32> minutesSet = spec.UnitValues.Get(ScheduleUnit.MINUTES); ICollection <Int32> hoursSet = spec.UnitValues.Get(ScheduleUnit.HOURS); int dayOfMonth; // If days of week is a wildcard, just go by days of month if (spec.OptionalDayOfMonthOperator != null || spec.OptionalDayOfWeekOperator != null) { var isWeek = false; var op = spec.OptionalDayOfMonthOperator; if (spec.OptionalDayOfMonthOperator == null) { op = spec.OptionalDayOfWeekOperator; isWeek = true; } // may return the current day or a future day in the same month, // and may advance the "after" date to the next month int currentYYMMDD = GetTimeYYYYMMDD(after); IncreaseAfterDayOfMonthSpecialOp(op.Operator, op.Day, op.Month, isWeek, after); int rolledYYMMDD = GetTimeYYYYMMDD(after); // if rolled then reset time portion if (rolledYYMMDD > currentYYMMDD) { result.Second = (NextValue(secondsSet, 0)); result.Minute = (NextValue(minutesSet, 0)); result.Hour = (NextValue(hoursSet, 0)); return(after.GetFieldValue(DateTimeFieldEnum.DAY_OF_MONTH)); } // rolling backwards is not allowed else if (rolledYYMMDD < currentYYMMDD) { throw new IllegalStateException("Failed to evaluate special date op, rolled date less then current date"); } else { var work = new DateTimeEx(after); work.SetFieldValue(DateTimeFieldEnum.SECOND, result.Second); work.SetFieldValue(DateTimeFieldEnum.MINUTE, result.Minute); work.SetFieldValue(DateTimeFieldEnum.HOUR_OF_DAY, result.Hour); if (work <= after) { // new date is not after current date, so bump after.AddUsingField(DateTimeFieldEnum.DAY_OF_MONTH, 1); result.Second = NextValue(secondsSet, 0); result.Minute = NextValue(minutesSet, 0); result.Hour = NextValue(hoursSet, 0); IncreaseAfterDayOfMonthSpecialOp(op.Operator, op.Day, op.Month, isWeek, after); } return(after.GetFieldValue(DateTimeFieldEnum.DAY_OF_MONTH)); } } else if (daysOfWeekSet == null) { dayOfMonth = NextValue(daysOfMonthSet, after.Day); if (dayOfMonth != after.Day) { result.Second = NextValue(secondsSet, 0); result.Minute = NextValue(minutesSet, 0); result.Hour = NextValue(hoursSet, 0); } if (dayOfMonth == -1) { dayOfMonth = NextValue(daysOfMonthSet, 0); after.AddMonths(1, DateTimeMathStyle.Java); } } // If days of weeks is not a wildcard and days of month is a wildcard, go by days of week only else if (daysOfMonthSet == null) { // Loop to find the next day of month that works for the specified day of week values while (true) { dayOfMonth = after.Day; int dayOfWeek = (int)after.DayOfWeek; // TODO // // Check the DayOfWeek logic in this section. The former code reads something // like the following: // // Calendar.Get(after, SupportClass.CalendarManager.DAY_OF_WEEK) - 1; // // Java calendars are one based which means that subtracting one makes them // zero-based. CLR DateTimes are zero-based so there should be no need to // tweak the dates to make this work. // If the day matches neither the day of month nor the day of week if (!daysOfWeekSet.Contains(dayOfWeek)) { result.Second = NextValue(secondsSet, 0); result.Minute = NextValue(minutesSet, 0); result.Hour = NextValue(hoursSet, 0); after.AddDays(1, DateTimeMathStyle.Java); } else { break; } } } // Both days of weeks and days of month are not a wildcard else { // Loop to find the next day of month that works for either day of month OR day of week while (true) { dayOfMonth = after.Day; int dayOfWeek = (int)after.DayOfWeek; // TODO // // See my discussion above about day of week conversion // If the day matches neither the day of month nor the day of week if ((!daysOfWeekSet.Contains(dayOfWeek)) && (!daysOfMonthSet.Contains(dayOfMonth))) { result.Second = NextValue(secondsSet, 0); result.Minute = NextValue(minutesSet, 0); result.Hour = NextValue(hoursSet, 0); after.AddDays(1, DateTimeMathStyle.Java); } else { break; } } } return(dayOfMonth); }
public void Add( DateTimeEx dtx, int value) { dtx.AddDays(7 * value); }