/// <summary> /// Constructs a new offset date/time with the given local date and time, and the given offset from UTC. /// </summary> /// <param name="localDateTime">Local date and time to represent</param> /// <param name="offset">Offset from UTC</param> public OffsetDateTime(LocalDateTime localDateTime, Offset offset) { var date = localDateTime.Date; yearMonthDayCalendar = date.YearMonthDayCalendar; // TODO(2.0): Remove this constructor, and convert the calculation below to a method; it's being done // in too many places. nanosecondsAndOffset = localDateTime.NanosecondOfDay | (((long)offset.Seconds) << NanosecondsBits); }
/// <summary> /// Constructs an instance for the given year and month in the ISO calendar. /// </summary> /// <param name="year">The year. This is the "absolute year", so a value of 0 means 1 BC, for example.</param> /// <param name="month">The month of year.</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) { // Note: not delegating to (year, month, calendar) to optimize performance. if (year < MinGregorianYear || year > MaxGregorianYear || month < 1 || month > 12) { Preconditions.CheckArgumentRange(nameof(year), year, MinGregorianYear, MaxGregorianYear); Preconditions.CheckArgumentRange(nameof(month), month, 1, 12); } startOfMonth = new YearMonthDayCalendar(year, month, 1, CalendarOrdinal.Iso); }
/// <summary> /// Optimized conversion from an Instant to an OffsetDateTime in the ISO calendar. /// This is equivalent to <c>new OffsetDateTime(new LocalDateTime(instant.Plus(offset)), offset)</c> /// but with less overhead. /// </summary> internal OffsetDateTime(Instant instant, Offset offset) { unchecked { int days = instant.DaysSinceEpoch; long nanoOfDay = instant.NanosecondOfDay + offset.Nanoseconds; if (nanoOfDay >= NanosecondsPerDay) { days++; nanoOfDay -= NanosecondsPerDay; } else if (nanoOfDay < 0) { days--; nanoOfDay += NanosecondsPerDay; } yearMonthDayCalendar = GregorianYearMonthDayCalculator.GetGregorianYearMonthDayCalendarFromDaysSinceEpoch(days); nanosecondsAndOffset = CombineNanoOfDayAndOffset(nanoOfDay, offset); } }
/// <summary> /// Optimized conversion from an Instant to an OffsetDateTime in the specified calendar. /// This is equivalent to <c>new OffsetDateTime(new LocalDateTime(instant.Plus(offset), calendar), offset)</c> /// but with less overhead. /// </summary> internal OffsetDateTime(Instant instant, Offset offset, CalendarSystem calendar) { unchecked { int days = instant.DaysSinceEpoch; long nanoOfDay = instant.NanosecondOfDay + offset.Nanoseconds; if (nanoOfDay >= NanosecondsPerDay) { days++; nanoOfDay -= NanosecondsPerDay; } else if (nanoOfDay < 0) { days--; nanoOfDay += NanosecondsPerDay; } yearMonthDayCalendar = calendar.GetYearMonthDayFromDaysSinceEpoch(days).WithCalendar(calendar); nanosecondsAndOffset = nanoOfDay | (((long)offset.Seconds) << NanosecondsBits); } }
/// <summary> /// Constructs an instance for the given year, month and day in the ISO calendar. /// </summary> /// <param name="year">The year. This is the "absolute year", so 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> /// <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) { GregorianYearMonthDayCalculator.ValidateGregorianYearMonthDay(year, month, day); yearMonthDayCalendar = new YearMonthDayCalendar(year, month, day, CalendarOrdinal.Iso); }
/// <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 from the number of days since the unix epoch, in the ISO /// calendar system. /// </summary> internal LocalDate([Trusted] int daysSinceEpoch) { Preconditions.DebugCheckArgumentRange(nameof(daysSinceEpoch), daysSinceEpoch, CalendarSystem.Iso.MinDays, CalendarSystem.Iso.MaxDays); this.yearMonthDayCalendar = GregorianYearMonthDayCalculator.GetGregorianYearMonthDayCalendarFromDaysSinceEpoch(daysSinceEpoch); }
/// <summary> /// Constructs an instance from values which are assumed to already have been validated. /// </summary> internal LocalDate([Trusted] YearMonthDayCalendar yearMonthDayCalendar) { this.yearMonthDayCalendar = yearMonthDayCalendar; }
/// <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> /// 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); }
/// <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); }
internal OffsetDateTime([Trusted] YearMonthDayCalendar yearMonthDayCalendar, LocalTime time, Offset offset) { this.yearMonthDayCalendar = yearMonthDayCalendar; this.nanosecondsAndOffset = CombineNanoOfDayAndOffset(time.NanosecondOfDay, offset); Calendar.DebugValidateYearMonthDay(YearMonthDay); }
internal OffsetDateTime([Trusted] YearMonthDayCalendar yearMonthDayCalendar, [Trusted] long nanosecondsAndOffset) { this.yearMonthDayCalendar = yearMonthDayCalendar; this.nanosecondsAndOffset = nanosecondsAndOffset; Calendar.DebugValidateYearMonthDay(YearMonthDay); }
internal OffsetDateTime([Trusted] YearMonthDayCalendar yearMonthDayCalendar, LocalTime time, Offset offset) { this.yearMonthDayCalendar = yearMonthDayCalendar; this.nanosecondsAndOffset = time.NanosecondOfDay | (((long)offset.Seconds) << NanosecondsBits); Calendar.DebugValidateYearMonthDay(YearMonthDay); }