An unit test failed when I built my code on BitBucket, it threw System.TimeZoneNotFoundException
. It happens where I try to convert a UTC Time to local time in a specific time zone. Interestingly the same unit test passes on my local running Windows 10 and on Appveyor.
The exact error message is
The time zone ID 'Pacific Standard Time' was not found on the local computer.
The exception occurred on the following line of code, its parameter timeZoneId
got passed in the value "Pacific Standard Time".
TimeZoneInfo userTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
The issue comes from the fact that there is more than one source that provides time zone values, to list a few
- The Unicode CLDR project
- The IANA time zone data
- Microsoft Windows time zone updates
And they output different time zone values, for example while on Windows you have "Eastern Standard Time", IANA calls it "America/New_York".
I found this cool library TimeZoneConverter that solves exactly this problem with minimal code change, my previous code becomes
TimeZoneInfo userTimeZone = TZConvert.GetTimeZoneInfo(timeZoneId);
It has the ability to work with any time zone provider.
// Either of these will work on any platform: TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("Eastern Standard Time"); TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("America/New_York");
Alternatively I read that Noda Time lib doesn't have these issues, for right now before a release I just want to resolve it with the least code change.