/// <summary> /// Get the start or end date time in UTC for the specified date range type in the specified time zone. /// </summary> /// <param name="dateRangeType">The date range type (today, current week etc.)</param> /// <param name="ianaTimeZoneName">The IANA time zone name</param> /// <param name="isStart">True for start and false for end of date range</param> /// <param name="useEndOfCurrentDay">Flag to indicate if end of current periods are now or the end of the current day</param> /// <returns>The start or end UTC for the range in the time zone</returns> public static DateTime?UtcForDateRangeType(this DateRangeType dateRangeType, string ianaTimeZoneName, bool isStart, bool useEndOfCurrentDay = false) { if (dateRangeType == DateRangeType.Custom || dateRangeType == DateRangeType.ProjectExtents || string.IsNullOrEmpty(ianaTimeZoneName)) { return(null); } DateTimeZone timeZone = DateTimeZoneProviders.Tzdb[ianaTimeZoneName]; ZonedClock clock = SystemClock.Instance.InZone(timeZone); LocalDateTime localNow = clock.GetCurrentLocalDateTime(); var local = localNow.LocalDateTimeForDateRangeType(dateRangeType, isStart); var zoned = timeZone.AtLeniently(local); return(zoned.ToDateTimeUtc()); }
public void GetCurrent() { var julian = CalendarSystem.Julian; FakeClock underlyingClock = new FakeClock(NodaConstants.UnixEpoch); ZonedClock zonedClock = underlyingClock.InZone(SampleZone, julian); Assert.AreEqual(NodaConstants.UnixEpoch, zonedClock.GetCurrentInstant()); Assert.AreEqual(new ZonedDateTime(underlyingClock.GetCurrentInstant(), SampleZone, julian), zonedClock.GetCurrentZonedDateTime()); Assert.AreEqual(new LocalDateTime(1969, 12, 19, 2, 0, julian), zonedClock.GetCurrentLocalDateTime()); Assert.AreEqual(new LocalDateTime(1969, 12, 19, 2, 0, julian).WithOffset(Offset.FromHours(2)), zonedClock.GetCurrentOffsetDateTime()); Assert.AreEqual(new LocalDate(1969, 12, 19, julian), zonedClock.GetCurrentDate()); Assert.AreEqual(new LocalTime(2, 0, 0), zonedClock.GetCurrentTimeOfDay()); }