TimeZoneConverter is a lightweight library to convert quickly between IANA, Windows, and Rails time zone names.
Note: A separate TimeZoneConverter.Posix
package is also available if you need POSIX time zone support.
- Add the
TimeZoneConverter
NuGet package to your project. - Import the
TimeZoneConverter
namespace where needed.
As of version 6.0.0, TimeZoneConverter works with all of the following:
- .NET 5 or greater
- .NET Core 2.0 or greater
- .NET Framework 4.6.2 and greater
.NET Framework versions less than 4.6.2 are no longer supported.
.NET 6 has built-in support for IANA and Windows time zones in a cross-platform manner, somewhat reducing the need for this library. It relies on .NET's ICU integration to perform this functionality. See the .NET blog for details.
Restated, if you are targeting only .NET 6 (or higher), and you have either platform-provided or App-local ICU enabled, you might not need to use this library. However, it is indeed still supported, and many have found it continues to be useful in certain environments or on specific platforms.
TimeZoneConverter has no external data dependencies at runtime. All of the data it needs is embedded in the library itself.
Some functions in TimeZoneConverter, such as TZConvert.GetTimeZoneInfo
rely on the underlying TimeZoneInfo
object having access to
time zone data of the operating system. On Windows, this data comes from the registry and is maintained via Windows Updates.
On OSX and Linux, this data comes from a distribution of the IANA time zone database, usually via the tzdata
package. If your environment does not have the tzdata
package installed, you will need to install it for TZConvert.GetTimeZoneInfo
to work correctly.
For example, the Alpine Linux Docker images for .NET Core no longer ship with tzdata
. See dotnet/dotnet-docker#1366 for instructions on how to add it to your Docker images.
This library uses a combination of data sources to achieve its goals:
- The Unicode CLDR project
- The IANA time zone data
- Microsoft Windows time zone updates
- The
MAPPING
data fromActiveSupport::TimeZone
in the Rails source code. - The author's best-informed knowledge and opinions
Usually, the latter is reserved for edge cases, and for newly-introduced zones that may or may not have been published to official sources yet.
Important: Since this data can change whenever new time zones are introduced from any of these sources, it is recommended that you always use the most current revision, and check for updates regularly.
Additionally, this library does not attempt to determine if the time zone IDs provided are actually present on the computer where the code is running. It is assumed that the computer is kept current with time zone updates.
For example, if one attempts to convert Africa/Khartoum
to a Windows time zone ID, they will get Sudan Standard Time
. If it is then used on a Windows computer that does not yet have KB4051956
installed (which created this time zone), they will likely get a TimeZoneNotFoundException
.
It is possible for a zone to be unmappable - meaning that there is no logical equivalent from one type of time zone to another.
Currently there is only one IANA zone that is unmappable to Windows, which is Antarctica/Troll
. In other words, there is no "correct" time zone for Windows users who may happen to be stationed in Troll Station, Antarctica. Therefore, if you try to convert Antarctica/Troll
to Windows, you will get a TimeZoneNotFoundException
.
There are many zones that are unmappable to Rails. The complete list is in the unit test code here.
Convert an IANA time zone name to the best fitting Windows time zone ID.
string tz = TZConvert.IanaToWindows("America/New_York");
// Result: "Eastern Standard Time"
Convert a Windows time zone name to the best fitting IANA time zone name.
string tz = TZConvert.WindowsToIana("Eastern Standard Time");
// result: "America/New_York"
Convert a Windows time zone name to the best fitting IANA time zone name, with regard to a specific country.
string tz = TZConvert.WindowsToIana("Eastern Standard Time", "CA");
// result: "America/Toronto"
Get a TimeZoneInfo
object from .NET Core, regardless of what OS you are running on:
// Either of these will work on any platform:
TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("Eastern Standard Time");
TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("America/New_York");
Convert a Rails time zone name to the best fitting IANA time zone name.
string tz = TZConvert.RailsToIana("Mexico City");
// result: "America/Mexico_City"
Convert a Rails time zone name to the best fitting Windows time zone ID.
string tz = TZConvert.RailsToWindows("Mexico City");
// result: "Central Standard Time (Mexico)"
Convert an IANA time zone name to one or more Rails time zone names.
IList<string> tz = TZConvert.IanaToRails("America/Mexico_City");
// Result: { "Guadalajara", "Mexico City" }
Convert a Windows time zone ID to one or more Rails time zone names.
IList<string> tz = TZConvert.WindowsToRails("Central Standard Time (Mexico)");
// Result: { "Guadalajara", "Mexico City" }
There are a few additional helpers you may find useful.
-
These properties provide lists of the various types of time zones known to this library:
TZConvert.KnownIanaTimeZoneNames
TZConvert.KnownWindowsTimeZoneIds
TZConvert.KnownRailsTimeZoneNames
-
If you need a list of time zones that are applicable in a given region, you can use:
TZConvert.GetIanaTimeZoneNamesByTerritory()
This library is provided free of charge, under the terms of the MIT license.