public CalendarDateTimeDuration(CalendarDateDuration dateDuration, TimeDuration timeDuration) { if (dateDuration == null) { throw new ArgumentNullException(nameof(dateDuration)); } if (timeDuration == null) { throw new ArgumentNullException(nameof(timeDuration)); } _dateDuration = dateDuration; _timeDuration = timeDuration; }
public static CalendarDate Add(CalendarDate x, CalendarDateDuration y) { var day = x.Day; var month = x.Month; var year = x.Year; var century = x.Century; var precision = x.Precision; if (y.Centuries != null) { century += y.Centuries.Value; } year += y.Years; if (y.Months != null) { month += y.Months.Value; if (precision < CalendarDatePrecision.Month) { precision = CalendarDatePrecision.Month; } } if (y.Days != null) { day += y.Days.Value; if (precision < CalendarDatePrecision.Day) { precision = CalendarDatePrecision.Day; } } while (month > 12) { month -= 12; year++; } var daysInMonth = DaysInMonth(year, month); while (day > daysInMonth) { day -= daysInMonth; month++; daysInMonth = DaysInMonth(year, month); } switch (precision) { case CalendarDatePrecision.Century: return CalendarDate.FromCentury(century); case CalendarDatePrecision.Year: return new CalendarDate(year); case CalendarDatePrecision.Month: return new CalendarDate(year, month); case CalendarDatePrecision.Day: return new CalendarDate(year, month, day); default: return null; } }
public static CalendarDate Subtract(CalendarDate x, CalendarDateDuration y) { long century = x.Century; long year = x.Year - y.Years; double month = x.Month; double day = x.Day; CalendarDatePrecision precision = x.Precision; if (y.Days != null) { day -= y.Days.Value; if (precision < CalendarDatePrecision.Day) { precision = CalendarDatePrecision.Day; } } if (y.Months != null) { month -= y.Months.Value; if (precision < CalendarDatePrecision.Month) { precision = CalendarDatePrecision.Month; } } while (month < 1) { month += MonthsPerYear; year--; } while (day < 1) { day += DaysInMonth(year, (int)month); month--; } switch (precision) { case CalendarDatePrecision.Century: return CalendarDate.FromCentury(century); case CalendarDatePrecision.Year: return new CalendarDate(year); case CalendarDatePrecision.Month: return new CalendarDate(year, (int)month); case CalendarDatePrecision.Day: return new CalendarDate(year, (int)month, (int)day); default: return null; } }
public static CalendarDateTime Add(CalendarDateTime x, CalendarDateDuration y) { double day = x.Day; double month = x.Month; double year = x.Year; var second = x.Second; var minute = x.Minute; var hour = x.Hour; var precision = x.Precision; year += y.Years; if (y.Months != null) { month += y.Months.Value; } if (y.Days != null) { day += y.Days.Value; } while ((int)month > MonthsPerYear) { month -= MonthsPerYear; year++; } int daysInMonth = DaysInMonth((long)year, (int)month); while (day > daysInMonth) { day -= daysInMonth; month++; daysInMonth = DaysInMonth((long)year, (int)month); } switch (precision) { case TimePrecision.Hour: return new CalendarDateTime(new CalendarDate((long)year, (int)month, (int)day), new Time(hour)); case TimePrecision.Minute: return new CalendarDateTime(new CalendarDate((long)year, (int)month, (int)day), new Time((int)hour, minute)); case TimePrecision.Second: return new CalendarDateTime(new CalendarDate((long)year, (int)month, (int)day), new Time((int)hour, (int)minute, second)); default: return null; } }
public static CalendarDateTime Subtract(CalendarDateTime x, CalendarDateDuration y) { long year = x.Year - y.Years; double month = x.Month; double day = x.Day; double hour = x.Hour; double minute = x.Minute; double second = x.Second; TimePrecision precision = x.Precision; if (y.Months != null) { month -= y.Months.Value; if (month != (int)month) { day += (month - (int)month) * DaysInMonth(year, (int)month); month = (int)month; } } if (y.Days != null) { day -= (int)y.Days.Value; if (day != (int)day) { hour += (day - (int)day) * HoursPerDay; day = (int)day; } } while (second <= -SecondsPerMinute) { second += SecondsPerMinute; minute--; } while (minute <= -MinutesPerHour) { minute += MinutesPerHour; hour--; } while (hour <= -HoursPerDay) { hour += HoursPerDay; day--; } while (month < 1) { month += MonthsPerYear; year--; } while (day < 1) { day += DaysInMonth(year, (int)month); month--; } switch (precision) { case TimePrecision.Hour: return new CalendarDateTime(new CalendarDate(year, (int)month, (int)day), new Time(hour)); case TimePrecision.Minute: return new CalendarDateTime(new CalendarDate(year, (int)month, (int)day), new Time((int)hour, minute)); case TimePrecision.Second: return new CalendarDateTime(new CalendarDate(year, (int)month, (int)day), new Time((int)hour, (int)minute, second)); default: return null; } }