/// <summary> /// Fetches a calendar system by its ordinal value, constructing it if necessary. /// </summary> internal static CalendarSystem ForOrdinal([Trusted] CalendarOrdinal ordinal) { Preconditions.DebugCheckArgument(ordinal >= 0 && ordinal < CalendarOrdinal.Size, nameof(ordinal), "Unknown ordinal value {0}", ordinal); // Avoid an array lookup for the overwhelmingly common case. if (ordinal == CalendarOrdinal.Iso) { return(Iso); } CalendarSystem calendar = CalendarByOrdinal[(int)ordinal]; if (calendar != null) { return(calendar); } // Not found it in the array. This can happen if the calendar system was initialized in // a different thread, and the write to the array isn't visible in this thread yet. // A simple switch will do the right thing. This is separated out (directly below) to allow // it to be tested separately. (It may also help this method be inlined...) The return // statement below is unlikely to ever be hit by code coverage, as it's handling a very // unusual and hard-to-provoke situation. return(ForOrdinalUncached(ordinal)); }
/// <summary> /// Constructs an instance from the number of days since the unix epoch, and a calendar /// system. The calendar system is assumed to be non-null, but the days since the epoch are /// validated. /// </summary> internal LocalDate(int daysSinceEpoch, [Trusted][NotNull] CalendarSystem calendar) { Preconditions.DebugCheckNotNull(calendar, nameof(calendar)); this.yearMonthDayCalendar = calendar.GetYearMonthDayCalendarFromDaysSinceEpoch(daysSinceEpoch); }
/// <summary> /// Constructs an instance for the given era, year of era, month and day in the specified calendar. /// </summary> /// <param name="era">The era within which to create a date. Must be a valid era within the specified calendar.</param> /// <param name="yearOfEra">The year of era.</param> /// <param name="month">The month of year.</param> /// <param name="day">The day of month.</param> /// <param name="calendar">Calendar system in which to create the date.</param> /// <returns>The resulting date.</returns> /// <exception cref="ArgumentOutOfRangeException">The parameters do not form a valid date.</exception> public LocalDate([NotNull] Era era, int yearOfEra, int month, int day, [NotNull] CalendarSystem calendar) : this(Preconditions.CheckNotNull(calendar, nameof(calendar)).GetAbsoluteYear(yearOfEra, era), month, day, calendar) { }
public Diary(IClock clock, CalendarSystem calendar, DateTimeZone timeZone) { _clock = clock; _calendar = calendar; _timeZone = timeZone; }
/// <summary> /// Creates a new <see cref="ZonedClock"/> with the given clock, time zone and calendar system. /// </summary> /// <param name="clock">Clock to use to obtain instants.</param> /// <param name="zone">Time zone to adjust instants into.</param> /// <param name="calendar">Calendar system to use.</param> public ZonedClock([NotNull] IClock clock, [NotNull] DateTimeZone zone, [NotNull] CalendarSystem calendar) { this.clock = Preconditions.CheckNotNull(clock, nameof(clock)); this.zone = Preconditions.CheckNotNull(zone, nameof(zone)); this.calendar = Preconditions.CheckNotNull(calendar, nameof(calendar)); }
/// <summary> /// To LocalDateTime /// </summary> /// <param name="lt"></param> /// <param name="calendar"></param> /// <returns></returns> public static LocalDateTime ToLocalDateTime(this LocalTime lt, CalendarSystem calendar) => LocalDateTime.FromDateTime(DateTime.Today, calendar).SetTime(lt);
static CalendarSystem() { var gregorianCalculator = new GregorianYearMonthDayCalculator(); var gregorianEraCalculator = new GJEraCalculator(gregorianCalculator); IsoCalendarSystem = new CalendarSystem(CalendarOrdinal.Iso, IsoId, IsoName, gregorianCalculator, 4, gregorianEraCalculator); }
public ZonedDateTime InZone([NotNull] DateTimeZone zone, [NotNull] CalendarSystem calendar) { Preconditions.CheckNotNull(zone, nameof(zone)); Preconditions.CheckNotNull(calendar, nameof(calendar)); return(new ZonedDateTime(this, zone, calendar)); }
internal YearMonthDayCalendar WithCalendar([CanBeNull] CalendarSystem calendar) => new YearMonthDayCalendar(value, calendar == null ? 0 : calendar.Ordinal);
static IslamicCalendars() { ByLeapYearPatterAndEpoch = new CalendarSystem[4, 2]; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 2; j++) { var leapYearPattern = (IslamicLeapYearPattern) i; var epoch = (IslamicEpoch) j; var calculator = new IslamicYearMonthDayCalculator((IslamicLeapYearPattern) i, (IslamicEpoch) j); CalendarOrdinal ordinal = CalendarOrdinal.IslamicAstronomicalBase15 + (i - 1) + (j - 1) * 4; ByLeapYearPatterAndEpoch[i - 1, j - 1] = new CalendarSystem(ordinal, GetIslamicId(leapYearPattern, epoch), IslamicName, calculator, Era.AnnoHegirae); } } }
/// <summary> /// Creates a new <see cref="ZonedClock"/> with the given clock, time zone and calendar system. /// </summary> /// <param name="clock">Clock to use to obtain instants.</param> /// <param name="zone">Time zone to adjust instants into.</param> /// <param name="calendar">Calendar system to use.</param> public ZonedClock(IClock clock, DateTimeZone zone, CalendarSystem calendar) { Clock = Preconditions.CheckNotNull(clock, nameof(clock)); Zone = Preconditions.CheckNotNull(zone, nameof(zone)); Calendar = Preconditions.CheckNotNull(calendar, nameof(calendar)); }
/// <summary> /// Returns the <see cref="OffsetDateTime"/> representing the same point in time as this instant, with /// the specified UTC offset and calendar system. /// </summary> /// <param name="offset">The offset from UTC with which to represent this instant.</param> /// <param name="calendar">The calendar system in which to represent this instant.</param> /// <returns>An <see cref="OffsetDateTime"/> for the same instant, with the given offset /// and calendar</returns> /// <exception cref="ArgumentNullException"><paramref name="calendar"/> is null.</exception> public OffsetDateTime WithOffset(Offset offset, CalendarSystem calendar) { Preconditions.CheckNotNull(calendar, "calendar"); return(new OffsetDateTime(new LocalDateTime(this.Plus(offset), calendar), offset)); }
public Diary(IClock clock, CalendarSystem calendar, DateTimeZone timeZone) { this.clock = clock; this.calendar = calendar; this.timeZone = timeZone; }
internal YearMonthDayCalendar WithCalendar(CalendarSystem calendar) => new YearMonthDayCalendar(value, calendar?.Ordinal ?? 0);
static GregorianJulianCalendars() { var julianCalculator = new JulianYearMonthDayCalculator(); Julian = new CalendarSystem(CalendarOrdinal.Julian, JulianId, JulianName, new JulianYearMonthDayCalculator(), 4, new GJEraCalculator(julianCalculator)); // Variations for the calendar systems which have different objects for different "minimum first day of week" // values. These share eras and year/month/day calculators where appropriate. GregorianByMinWeekLength = new CalendarSystem[7]; for (int i = 1; i <= 7; i++) { // CalendarOrdinal is set up to make this simple :) // The calculators are pinched from the ISO calendar system as they're the same for all of these calendar systems. GregorianByMinWeekLength[i - 1] = new CalendarSystem((CalendarOrdinal)i, GetGregorianId(i), GregorianName, IsoCalendarSystem.YearMonthDayCalculator, i, IsoCalendarSystem.eraCalculator); } }
public ZonedDateTime WithCalendar(CalendarSystem calendar) { return(new ZonedDateTime(offsetDateTime.WithCalendar(calendar), zone)); }
public LocalDate WithCalendar([NotNull] CalendarSystem calendarSystem) { return(new LocalDate(localTime.WithCalendar(calendarSystem))); }
/// <summary> /// Private constructor only present for serialization. /// </summary> /// <param name="info">The <see cref="SerializationInfo"/> to fetch data from.</param> /// <param name="context">The source for this deserialization.</param> private LocalDate(SerializationInfo info, StreamingContext context) : this(new LocalDateTime(new LocalInstant(info.GetInt64(LocalTicksSerializationName)), CalendarSystem.ForId(info.GetString(CalendarIdSerializationName)))) { }
public OffsetDate WithCalendar([NotNull] CalendarSystem calendar) => new OffsetDate(Date.WithCalendar(calendar), offset);
/// <summary> /// Constructs an instance for the given year, month and day in the specified calendar. /// </summary> /// <param name="year">The year. This is the "absolute year", so, for /// the ISO calendar, a value of 0 means 1 BC, for example.</param> /// <param name="month">The month of year.</param> /// <param name="day">The day of month.</param> /// <param name="calendar">Calendar system in which to create the date.</param> /// <returns>The resulting date.</returns> /// <exception cref="ArgumentOutOfRangeException">The parameters do not form a valid date.</exception> public LocalDate(int year, int month, int day, [NotNull] CalendarSystem calendar) : this(new LocalDateTime(year, month, day, 0, 0, calendar)) { }
/// <summary> /// To LocalDateTime /// </summary> /// <param name="lt"></param> /// <param name="year"></param> /// <param name="month"></param> /// <param name="day"></param> /// <param name="calendar"></param> /// <returns></returns> public static LocalDateTime ToLocalDateTime(this LocalTime lt, int year, int month, int day, CalendarSystem calendar) => new LocalDateTime(year, month, day, lt.Hour, lt.Minute, lt.Second, lt.Millisecond, calendar);
/// <summary> /// Constructs an instance for the given era, year of era, month and day in the specified calendar. /// </summary> /// <param name="era">The era within which to create a date. Must be a valid era within the specified calendar.</param> /// <param name="yearOfEra">The year of era.</param> /// <param name="month">The month of year.</param> /// <param name="day">The day of month.</param> /// <param name="calendar">Calendar system in which to create the date.</param> /// <returns>The resulting date.</returns> /// <exception cref="ArgumentOutOfRangeException">The parameters do not form a valid date.</exception> public LocalDate(Era era, int yearOfEra, int month, int day, [NotNull] CalendarSystem calendar) : this(new LocalDateTime(Preconditions.CheckNotNull(calendar, "calendar").GetLocalInstant(era, yearOfEra, month, day), calendar)) { }
/// <summary> /// Constructs an instance from values which are assumed to already have been validated. /// </summary> // TODO: See if we still need this. internal LocalDate([Trusted] YearMonthDay yearMonthDay, [Trusted][CanBeNull] CalendarSystem calendar) { this.yearMonthDayCalendar = yearMonthDay.WithCalendar(calendar); }
public OffsetDateTime WithCalendar([NotNull] CalendarSystem calendarSystem) { return(new OffsetDateTime(localDateTime.WithCalendar(calendarSystem), offset)); }
/// <summary> /// Constructs an instance for the given year, month and day in the specified calendar. /// </summary> /// <param name="year">The year. This is the "absolute year", so, for /// the ISO calendar, a value of 0 means 1 BC, for example.</param> /// <param name="month">The month of year.</param> /// <param name="day">The day of month.</param> /// <param name="calendar">Calendar system in which to create the date.</param> /// <returns>The resulting date.</returns> /// <exception cref="ArgumentOutOfRangeException">The parameters do not form a valid date.</exception> public LocalDate(int year, int month, int day, [NotNull] CalendarSystem calendar) { Preconditions.CheckNotNull(calendar, nameof(calendar)); calendar.ValidateYearMonthDay(year, month, day); yearMonthDayCalendar = new YearMonthDayCalendar(year, month, day, calendar.Ordinal); }
/// <summary> /// Private constructor only present for serialization. /// </summary> /// <param name="info">The <see cref="SerializationInfo"/> to fetch data from.</param> /// <param name="context">The source for this deserialization.</param> private OffsetDateTime(SerializationInfo info, StreamingContext context) : this(new LocalDateTime(new LocalInstant(info.GetInt64(LocalTicksSerializationName)), CalendarSystem.ForId(info.GetString(CalendarIdSerializationName))), Offset.FromMilliseconds(info.GetInt32(OffsetMillisecondsSerializationName))) { }
/// <summary> /// Converts a <see cref="DateTime" /> of any kind to a LocalDate in the specified calendar, ignoring the time of day. /// This does not perform any time zone conversions, so a DateTime with a <see cref="DateTime.Kind"/> of /// <see cref="DateTimeKind.Utc"/> will still represent the same year/month/day - it won't be converted into the local system time. /// </summary> /// <param name="dateTime">Value to convert into a Noda Time local date</param> /// <param name="calendar">The calendar system to convert into</param> /// <returns>A new <see cref="LocalDate"/> with the same values as the specified <c>DateTime</c>.</returns> public static LocalDate FromDateTime(DateTime dateTime, [NotNull] CalendarSystem calendar) { int days = NonNegativeTicksToDays(dateTime.Ticks) - NodaConstants.BclDaysAtUnixEpoch; return(new LocalDate(days, calendar)); }
public OffsetDate WithCalendar(CalendarSystem calendar) => new OffsetDate(date.WithCalendar(calendar), offset);
/// <summary> /// Returns the period between a start and an end date/time, using only the given units. /// </summary> /// <remarks> /// If <paramref name="end"/> is before <paramref name="start" />, each property in the returned period /// will be negative. If the given set of units cannot exactly reach the end point (e.g. finding /// the difference between 1am and 3:15am in hours) the result will be such that adding it to <paramref name="start"/> /// will give a value between <paramref name="start"/> and <paramref name="end"/>. In other words, /// any rounding is "towards start"; this is true whether the resulting period is negative or positive. /// </remarks> /// <param name="start">Start date/time</param> /// <param name="end">End date/time</param> /// <param name="units">Units to use for calculations</param> /// <exception cref="ArgumentException"><paramref name="units"/> is empty or contained unknown values.</exception> /// <exception cref="ArgumentException"><paramref name="start"/> and <paramref name="end"/> use different calendars.</exception> /// <returns>The period between the given date/times, using the given units.</returns> public static Period Between(LocalDateTime start, LocalDateTime end, PeriodUnits units) { Preconditions.CheckArgument(units != 0, nameof(units), "Units must not be empty"); Preconditions.CheckArgument((units & ~PeriodUnits.AllUnits) == 0, nameof(units), "Units contains an unknown value: {0}", units); CalendarSystem calendar = start.Calendar; Preconditions.CheckArgument(calendar.Equals(end.Calendar), nameof(end), "start and end must use the same calendar system"); if (start == end) { return Zero; } // Adjust for situations like "days between 5th January 10am and 7th Janary 5am" which should be one // day, because if we actually reach 7th January with date fields, we've overshot. // The date adjustment will always be valid, because it's just moving it towards start. // We need this for all date-based period fields. We could potentially optimize by not doing this // in cases where we've only got time fields... LocalDate endDate = end.Date; if (start < end) { if (start.TimeOfDay > end.TimeOfDay) { endDate = endDate.PlusDays(-1); } } else if (start > end && start.TimeOfDay < end.TimeOfDay) { endDate = endDate.PlusDays(1); } // Optimization for single field switch (units) { case PeriodUnits.Years: return FromYears(DatePeriodFields.YearsField.UnitsBetween(start.Date, endDate)); case PeriodUnits.Months: return FromMonths(DatePeriodFields.MonthsField.UnitsBetween(start.Date, endDate)); case PeriodUnits.Weeks: return FromWeeks(DatePeriodFields.WeeksField.UnitsBetween(start.Date, endDate)); case PeriodUnits.Days: return FromDays(DaysBetween(start.Date, endDate)); case PeriodUnits.Hours: return FromHours(TimePeriodField.Hours.UnitsBetween(start, end)); case PeriodUnits.Minutes: return FromMinutes(TimePeriodField.Minutes.UnitsBetween(start, end)); case PeriodUnits.Seconds: return FromSeconds(TimePeriodField.Seconds.UnitsBetween(start, end)); case PeriodUnits.Milliseconds: return FromMilliseconds(TimePeriodField.Milliseconds.UnitsBetween(start, end)); case PeriodUnits.Ticks: return FromTicks(TimePeriodField.Ticks.UnitsBetween(start, end)); case PeriodUnits.Nanoseconds: return FromNanoseconds(TimePeriodField.Nanoseconds.UnitsBetween(start, end)); } // Multiple fields LocalDateTime remaining = start; int years = 0, months = 0, weeks = 0, days = 0; if ((units & PeriodUnits.AllDateUnits) != 0) { LocalDate remainingDate = DateComponentsBetween( start.Date, endDate, units, out years, out months, out weeks, out days); remaining = new LocalDateTime(remainingDate, start.TimeOfDay); } if ((units & PeriodUnits.AllTimeUnits) == 0) { return new Period(years, months, weeks, days); } // The remainder of the computation is with fixed-length units, so we can do it all with // Duration instead of Local* values. We don't know for sure that this is small though - we *could* // be trying to find the difference between 9998 BC and 9999 CE in nanoseconds... // Where we can optimize, do everything with long arithmetic (as we do for Between(LocalTime, LocalTime)). // Otherwise (rare case), use duration arithmetic. long hours, minutes, seconds, milliseconds, ticks, nanoseconds; var duration = end.ToLocalInstant().TimeSinceLocalEpoch - remaining.ToLocalInstant().TimeSinceLocalEpoch; if (duration.IsInt64Representable) { TimeComponentsBetween(duration.ToInt64Nanoseconds(), units, out hours, out minutes, out seconds, out milliseconds, out ticks, out nanoseconds); } else { hours = UnitsBetween(PeriodUnits.Hours, TimePeriodField.Hours); minutes = UnitsBetween(PeriodUnits.Minutes, TimePeriodField.Minutes); seconds = UnitsBetween(PeriodUnits.Seconds, TimePeriodField.Seconds); milliseconds = UnitsBetween(PeriodUnits.Milliseconds, TimePeriodField.Milliseconds); ticks = UnitsBetween(PeriodUnits.Ticks, TimePeriodField.Ticks); nanoseconds = UnitsBetween(PeriodUnits.Ticks, TimePeriodField.Nanoseconds); } return new Period(years, months, weeks, days, hours, minutes, seconds, milliseconds, ticks, nanoseconds); long UnitsBetween(PeriodUnits mask, TimePeriodField timeField) { if ((mask & units) == 0) { return 0; } long value = timeField.GetUnitsInDuration(duration); duration -= timeField.ToDuration(value); return value; } }
public OffsetDateTime WithCalendar([NotNull] CalendarSystem calendar) { LocalDate newDate = Date.WithCalendar(calendar); return(new OffsetDateTime(newDate.YearMonthDayCalendar, nanosecondsAndOffset)); }
public OffsetDateTime WithCalendar([NotNull] CalendarSystem calendar) { LocalDate newDate = Date.WithCalendar(calendar); return(new OffsetDateTime(newDate, offsetTime)); }
/// <summary> /// Constructs an instance for the given year, month and day in the specified calendar. /// </summary> /// <param name="year">The year. This is the "absolute year", so, for /// the ISO calendar, a value of 0 means 1 BC, for example.</param> /// <param name="month">The month of year.</param> /// <param name="calendar">Calendar system in which to create the year/month.</param> /// <returns>The resulting year/month.</returns> /// <exception cref="ArgumentOutOfRangeException">The parameters do not form a valid year/month.</exception> public YearMonth(int year, int month, CalendarSystem calendar) { Preconditions.CheckNotNull(calendar, nameof(calendar)); calendar.ValidateYearMonthDay(year, month, 1); startOfMonth = new YearMonthDayCalendar(year, month, 1, calendar.Ordinal); }
/// <summary> /// Initializes a new instance of the <see cref="ZonedDateTime"/> struct. /// </summary> /// <param name="instant">The instant.</param> /// <param name="zone">The time zone.</param> /// <param name="calendar">The calendar system.</param> public ZonedDateTime(Instant instant, DateTimeZone zone, CalendarSystem calendar) { this.zone = Preconditions.CheckNotNull(zone, nameof(zone)); offsetDateTime = new OffsetDateTime(instant, zone.GetUtcOffset(instant), Preconditions.CheckNotNull(calendar, nameof(calendar))); }
/// <summary> /// Constructs an instance for the given era, year of era and month in the specified calendar. /// </summary> /// <param name="era">The era within which to create a year/month. Must be a valid era within the specified calendar.</param> /// <param name="yearOfEra">The year of era.</param> /// <param name="month">The month of year.</param> /// <param name="calendar">Calendar system in which to create the year/month.</param> /// <returns>The resulting year/month.</returns> /// <exception cref="ArgumentOutOfRangeException">The parameters do not form a valid year/month.</exception> public YearMonth(Era era, int yearOfEra, int month, CalendarSystem calendar) : this(Preconditions.CheckNotNull(calendar, nameof(calendar)).GetAbsoluteYear(yearOfEra, era), month, calendar) { }
public OffsetDateTime WithOffset(Offset offset, [NotNull] CalendarSystem calendar) { Preconditions.CheckNotNull(calendar, nameof(calendar)); return(new OffsetDateTime(this, offset, calendar)); }
static GregorianJulianCalendars() { var julianCalculator = new JulianYearMonthDayCalculator(); Julian = new CalendarSystem(CalendarOrdinal.Julian, JulianId, JulianName, julianCalculator, new GJEraCalculator(julianCalculator)); Gregorian = new CalendarSystem(CalendarOrdinal.Gregorian, GregorianId, GregorianName, IsoCalendarSystem.YearMonthDayCalculator, IsoCalendarSystem.eraCalculator); }