Is there any ISO standard for how we should return a timezone in a REST API?
If I return a
TimeZone type from a ASP.NET WebApi 2 action, the framework serialises the response model to look like this:
"Id": "Dateline Standard Time",
"DisplayName": "(UTC-12:00) International Date Line West",
"StandardName": "Dateline Standard Time",
"DaylightName": "Dateline Daylight Time",
Best How To :
Microsoft time zones, such as the ones used by the
TimeZoneInfo object, can indeed be serialized and deserialized, using the
ToSerializedString methods. They can also be referenced with their
Id property. Both of these are on the
TimeZoneInfo class, not the
TimeZone class (which you should not be using at all).
However, if you send either a Microsoft time zone ID or the serialized form of the time zone, your API will only be easily consumable by other .NET applications. You will have a really difficult time using Microsoft time zones outside of .NET.
There is no official standard for time zones, but the closest thing would be the identifiers provided by the IANA time zone database. For example, to convey the US Eastern time zone, you would pass
"America/New_York" in your API. You can read more about these in the timezone tag wiki, and also on Wikipedia. The nice thing about IANA time zones, is that it is implemented on every platform - so while it's not a standard, it is much more portable than the Microsoft time zones that only work on Windows.
For .NET, the best way I know of to work with IANA time zones is through the Noda Time library. For example:
DateTimeZone tz = DateTimeZoneProviders.Tzdb["Asia/Tokyo"];
You can read more in the Noda Time user guide.
If you've already built your system around
TimeZoneInfo, consider that you may still wish to translate to IANA time zones in your API layer. You can read about how to do that in this answer.