Kevin Tran
Kevin Tran

Reputation: 147

Missing 'Mountain Standard Time' timezone when using FindTimeZoneById() c# on mac

I'm running an Azure Functions project locally on Mac OS. My issue is using the TimeZoneInfo.FindSystemTimeZoneById(String) Method.

On input of 'Mountain Standard Time', which is included on Windows Platforms according to this link. This is fine when pushed to the hosting platform which is Windows, but on my local machine an error is thrown.

How do I handle this locally?

Upvotes: 2

Views: 991

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241758

The IDs and data used with TimeZoneInfo are platform dependent:

  • On Windows, the IDs are the Microsoft time zone identifiers, as used with the tzutil.exe command, Win32 APIs, and are sourced from the Registry.
    • Example: "Mountain Standard Time"
  • On Mac OSX, Linux, and other non-Windows platforms, the IDs are IANA time zone identifiers, as used by the TZ environment variable, timedatectl, sudo dpkg-reconfigure tzdata, sudo systemsetup -settimezone timezone, and other platform-specific utilities. The data is sourced from the IANA tz database.
    • Example: "America/Denver"

Thus you can change your code to the following, which will run on Linux and Mac OSX.

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("America/Denver");

If you cannot change the identifiers used in your application, or are designing your application to be cross-platform and need to work with both sets of identifiers, then use my TimeZoneConverter library:

// Either of these will work on any platform
TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("Mountain Standard Time");
TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("America/Denver");

See also the "Time Zone Databases" section of the timezone tag wiki.

Upvotes: 4

Related Questions