public static DateTime AsZonedDateTime(this DateTime dateTime, string timezone) { Instant instant = dateTime.ToUniversalTime().ToInstant(); ZonedDateTime zoned = instant.InZone(DateTimeZoneProviders.Tzdb[timezone]); return(zoned.ToDateTimeUnspecified()); }
/// <summary> /// Converts the UTC time to local time. /// </summary> /// <param name="utcTime">The UTC time.</param> /// <param name="tzID">The tz identifier. It is the same as IANA TZdb in database</param> /// <returns></returns> public static DateTime ConvertUnixEpochToLocalTime(int secondsSinceUnixEpoch, string tzID) { Instant time = Instant.FromSecondsSinceUnixEpoch(secondsSinceUnixEpoch); var dateTimeZone = GetTimeZoneProvider()[tzID]; ZonedDateTime zonedDateTime = time.InZone(dateTimeZone); return(zonedDateTime.ToDateTimeUnspecified()); }
public DateTime DateTimeUtcToLocalWithOlsenZone(DateTime dateTimeUtc, string olsenTimeZone) { var dateTimeZoneProvider = DateTimeZoneProviders.Tzdb; var dateTimeZone = dateTimeZoneProvider[olsenTimeZone]; //var utcInstant = new Instant(dateTimeUtc.Ticks); var utcInstant = Instant.FromDateTimeUtc(dateTimeUtc); var zonedDateTime = new ZonedDateTime(utcInstant, dateTimeZone); return zonedDateTime.ToDateTimeUnspecified(); }
public void ToDateTimeUnspecified() { ZonedDateTime zoned = SampleZone.AtStrictly(new LocalDateTime(2011, 3, 5, 1, 0, 0)); DateTime expected = new DateTime(2011, 3, 5, 1, 0, 0, DateTimeKind.Unspecified); DateTime actual = zoned.ToDateTimeUnspecified(); Assert.AreEqual(expected, actual); // Kind isn't checked by Equals... Assert.AreEqual(DateTimeKind.Unspecified, actual.Kind); }
public DateTime DateTimeUtcToLocalWithOlsenZone(DateTime dateTimeUtc, string olsenTimeZone) { var dateTimeZoneProvider = DateTimeZoneProviders.Tzdb; var dateTimeZone = dateTimeZoneProvider[olsenTimeZone]; //var utcInstant = new Instant(dateTimeUtc.Ticks); var utcInstant = Instant.FromDateTimeUtc(dateTimeUtc); var zonedDateTime = new ZonedDateTime(utcInstant, dateTimeZone); return(zonedDateTime.ToDateTimeUnspecified()); }
public void ToDateTimeUnspecified_JulianCalendar() { // Non-Gregorian calendar systems are handled by converting to the same // date, just like the DateTime constructor does. ZonedDateTime zoned = SampleZone.AtStrictly(new LocalDateTime(2011, 3, 5, 1, 0, 0, CalendarSystem.Julian)); DateTime expected = new DateTime(2011, 3, 5, 1, 0, 0, 0, new JulianCalendar(), DateTimeKind.Unspecified); DateTime actual = zoned.ToDateTimeUnspecified(); Assert.AreEqual(expected, actual); // Kind isn't checked by Equals... Assert.AreEqual(DateTimeKind.Unspecified, actual.Kind); }
/// <summary> /// Converts a non-local-time DateTime to a local-time DateTime based on the /// specified timezone. The returned object will be of Unspecified DateTimeKind /// which represents local time agnostic to servers timezone. To be used when /// we want to convert UTC to local time somewhere in the world. /// </summary> /// <param name="dateTime">Non-local DateTime as UTC or Unspecified DateTimeKind.</param> /// <param name="timezone">Timezone name (in TZDB format).</param> /// <returns>Local DateTime as Unspecified DateTimeKind.</returns> public static DateTime ToZone(this DateTime dateTime, string timezone) { if (dateTime.Kind == DateTimeKind.Local) { throw new ArgumentException("Expected non-local kind of DateTime"); } var zone = DateTimeZoneProviders.Tzdb[timezone]; Instant instant = dateTime.ToInstant(); ZonedDateTime inZone = instant.InZone(zone); DateTime unspecified = inZone.ToDateTimeUnspecified(); return(unspecified); }
/// <summary> /// Converts a non-local-time DateTime to a local-time DateTime based on the /// specified timezone. The returned object will be of Unspecified DateTimeKind /// which represents local time agnostic to servers timezone. To be used when /// we want to convert UTC to local time somewhere in the world. /// </summary> /// <param name="dateTime">Non-local DateTime as UTC or Unspecified DateTimeKind.</param> /// <param name="timezone">Timezone name (in TZDB format).</param> /// <returns>Local DateTime as Unspecified DateTimeKind.</returns> public static DateTime ToZone(this DateTime dateTime, string timezone) { var dte = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, DateTimeKind.Utc); if (dateTime.Kind == DateTimeKind.Local) { throw new ArgumentException("Expected non-local kind of DateTime"); } var zone = DateTimeZoneProviders.Tzdb[timezone]; Instant instant = dte.ToInstant(); ZonedDateTime inZone = instant.InZone(zone); DateTime unspecified = inZone.ToDateTimeUnspecified(); return(unspecified); }
public static object UnixSecondsToTime(long unixSeconds, string timezoneName = "") { if (unixSeconds < 0) { return("Invalid unixSeconds."); } Instant instant = Instant.FromUnixTimeSeconds(unixSeconds); // utc DateTimeZone tz = (string.IsNullOrWhiteSpace(timezoneName)) ? SystemTimeZone : DateTimeZoneProviders.Tzdb.GetZoneOrNull(timezoneName); if (tz == null) { return($"Invalid timezone: {timezoneName}."); } ZonedDateTime zdt = instant.InZone(tz); return(zdt.ToDateTimeUnspecified()); }
/// <summary> /// Converts a non-local-time DateTime to a local-time DateTime based on the /// specified timezone. The returned object will be of Unspecified DateTimeKind /// which represents local time agnostic to servers timezone. To be used when /// we want to convert UTC to local time somewhere in the world. /// </summary> /// <param name="dateTime">Non-local DateTime as UTC or Unspecified DateTimeKind.</param> /// <returns>Local DateTime as Unspecified DateTimeKind.</returns> internal static DateTime ToUser(this DateTime utcDateTime) { if (utcDateTime.Kind == DateTimeKind.Local) { return(utcDateTime); } else if (utcDateTime.Kind == DateTimeKind.Unspecified) { utcDateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Utc); } Instant instant = utcDateTime.ToInstant(); DateTimeZone zone = CultureHelper.GetDateTimeZone(); ZonedDateTime inZone = instant.InZone(zone); return(inZone.ToDateTimeUnspecified()); }
/// <summary> /// Converts the date time to the local time /// </summary> /// <param name="dt">The date time to convert</param> /// <returns>The localized date time</returns> public DateTime Convert(DateTime dt) { if (UseCurrentCulture) { CultureInfo currentCulture = CultureInfo.CurrentUICulture; RegionInfo regionInfo = new RegionInfo(currentCulture.Name); IEnumerable <string> zoneIds = NodaTime.TimeZones.TzdbDateTimeZoneSource.Default.ZoneLocations .Where(x => string.Compare(x.CountryCode, regionInfo.TwoLetterISORegionName, true) == 0) .Select(x => x.ZoneId); if (!zoneIds.Any()) { return(dt); } DateTime dateTime = DateTime.SpecifyKind(dt, DateTimeKind.Utc); Instant dateTimeInstant = Instant.FromDateTimeUtc(dateTime); DateTimeZone timeZone = DateTimeZoneProviders.Tzdb[zoneIds.FirstOrDefault()]; ZonedDateTime zonedDateTime = dateTimeInstant.InZone(timeZone); return(zonedDateTime.ToDateTimeUnspecified()); } else { DateTime dateTime = DateTime.SpecifyKind(dt, DateTimeKind.Utc); // Get instant from DateTime instance Instant instant = Instant.FromDateTimeUtc(dateTime); // Get user time zone IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb; DateTimeZone usersTimezone = timeZoneProvider[TimeZone]; // Set the instant to the local time zone ZonedDateTime usersZonedDateTime = instant.InZone(usersTimezone); DateTime localDateTime = usersZonedDateTime.ToDateTimeUnspecified(); return(localDateTime); } }
/// <summary> /// Converts the date time to UTC time /// </summary> /// <param name="dt">The date time to convert</param> /// <returns>The UTC date time</returns> public DateTime Convert(DateTime dt) { // Fork this method into the use of the current culture to do the utc conversion if (UseCurrentCulture) { CultureInfo currentCulture = CultureInfo.CurrentUICulture; RegionInfo regionInfo = new RegionInfo(currentCulture.Name); IEnumerable <string> zoneIds = NodaTime.TimeZones.TzdbDateTimeZoneSource.Default.ZoneLocations .Where(x => string.Compare(x.CountryCode, regionInfo.TwoLetterISORegionName, StringComparison.OrdinalIgnoreCase) == 0) .Select(x => x.ZoneId); if (!zoneIds.Any()) { return(dt); } DateTime dateTime = DateTime.SpecifyKind(dt, DateTimeKind.Utc); Instant dateTimeInstant = Instant.FromDateTimeUtc(dateTime); DateTimeZone timeZone = DateTimeZoneProviders.Tzdb[zoneIds.FirstOrDefault()]; ZonedDateTime zonedDateTime = dateTimeInstant.InZone(timeZone); DateTime localDateTime = zonedDateTime.ToDateTimeUnspecified(); return(localDateTime); } else { // Get local DateTime instance into a LocalDateTime object LocalDateTime localDateTime = new LocalDateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute); // Get the users' time zone IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb; DateTimeZone usersTimezone = timeZoneProvider[TimeZone]; // Format the local DateTime instance with the time zones ZonedDateTime zonedDbDateTime = localDateTime.InZoneLeniently(usersTimezone); // At this point we have all information to convert to UTC: release the kraken! return(zonedDbDateTime.ToDateTimeUtc()); } }