/// <summary> /// Converts a <see cref="DateTime" /> of any kind to a LocalDate in the ISO 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> /// <returns>A new <see cref="LocalDate"/> with the same values as the specified <c>DateTime</c>.</returns> public static LocalDate FromDateTime(DateTime dateTime) { long ticks = dateTime.Ticks - NodaConstants.BclTicksAtUnixEpoch; int days = TickArithmetic.TicksToDays(ticks); return(new LocalDate(days)); }
/// <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) { long ticks = dateTime.Ticks - NodaConstants.BclTicksAtUnixEpoch; int days = TickArithmetic.TicksToDays(ticks); return(new LocalDate(days, calendar)); }
public void TicksToDaysAndTickOfDayAndBack(long ticks) { long tickOfDay; int days = TickArithmetic.TicksToDaysAndTickOfDay(ticks, out tickOfDay); Assert.AreEqual(ticks, TickArithmetic.DaysAndTickOfDayToTicks(days, tickOfDay)); }
public void DaysAndTickOfDayToTicksUncheckedBoundaries() { // Only a useful test under debug, but this proves that the arithmetic won't overflow when used from // LocalDateTime or Instant. (In debug mode, we have TickArithmetic.BoundedDaysAndTickOfDayToTicks(CalendarSystem.Iso.MinDays, 0); TickArithmetic.BoundedDaysAndTickOfDayToTicks(CalendarSystem.Iso.MaxDays, NodaConstants.TicksPerDay - 1); }
/// <summary> /// Returns a <see cref="Duration"/> that represents the given number of ticks. /// </summary> /// <param name="ticks">The number of ticks.</param> /// <returns>A <see cref="Duration"/> representing the given number of ticks.</returns> public static Duration FromTicks(long ticks) { long tickOfDay; int days = TickArithmetic.TicksToDaysAndTickOfDay(ticks, out tickOfDay); return(new Duration(days, tickOfDay * NanosecondsPerTick)); }
public DateTime ToDateTimeUnspecified() { long ticks = TickArithmetic.BoundedDaysAndTickOfDayToTicks(date.DaysSinceEpoch, time.TickOfDay) + NodaConstants.BclTicksAtUnixEpoch; if (ticks < 0) { throw new InvalidOperationException("LocalDateTime out of range of DateTime"); } return(new DateTime(ticks, DateTimeKind.Unspecified)); }
public long ToUnixTimeTicks() => TickArithmetic.DaysAndTickOfDayToTicks(duration.FloorDays, duration.NanosecondOfFloorDay / NanosecondsPerTick);
/// <summary> /// Converts a <see cref="DateTime" /> of any kind to a LocalDateTime in the specified calendar. This does not perform /// any time zone conversions, so a DateTime with a <see cref="DateTime.Kind"/> of <see cref="DateTimeKind.Utc"/> /// will still have the same day/hour/minute etc - it won't be converted into the local system time. /// </summary> /// <param name="dateTime">Value to convert into a Noda Time local date and time</param> /// <param name="calendar">The calendar system to convert into</param> /// <returns>A new <see cref="LocalDateTime"/> with the same values as the specified <c>DateTime</c>.</returns> public static LocalDateTime FromDateTime(DateTime dateTime, [NotNull] CalendarSystem calendar) { int days = TickArithmetic.NonNegativeTicksToDaysAndTickOfDay(dateTime.Ticks, out long tickOfDay) - NodaConstants.BclDaysAtUnixEpoch; return(new LocalDateTime(new LocalDate(days, calendar), new LocalTime(unchecked (tickOfDay * NodaConstants.NanosecondsPerTick)))); }