I am using a date (1/1/2018 11:30 AM) with timezone (Eastern Standard Time) and converting it to a UTC date (2018-01-01T16:30:00Z). The original date is really Eastern Daylights Savings, so when developers do a conversion with the UTC, they get 12:30 PM instead of 11:30 AM. If I do 8/26/2018 11:30 AM, it works fine. My time zones are in the .NET Windows formats.
Is there a way with my method below to get the correct UTC that has the daylight savings from the timezone that is Standard with NodaTime?
2018-01-01T16:30:00Z = Helper.GetUtcTimeZone("1/1/2018 11:30 AM", "Eastern Standard Time").ToString();
Method
public static Instant GetUtcTimeZone(DateTime dateTime, string timeZone)
{
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone ?? TimeZoneInfo.Local.StandardName);
if (timeZoneInfo != null)
{
if (dateTime.Kind == DateTimeKind.Unspecified)
{
dateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZoneInfo);
}
}
return Instant.FromDateTimeUtc(dateTime);
}
If you want to keep using
TimeZoneInfo
, just use it directly. No need for all the extra logic you've added.Though really, once you are using NodaTime, there's no reason for that. Just use its built-in functionality:
One important note: You had a fall-back to
TimeZoneInfo.Local.StandardName
. That's not safe - as theStandardName
fields are localized, and don't always match the names of the identifiers even in English. UseId
instead ofStandardName
if you are expecting an identifier. In NodaTime, you can useDateTimeZoneProviders.Bcl.GetSystemDefault()
.