https://github.com/robthree/timezonemapper
Library for mapping *N*X TimeZone ID's (e.g. Europe/Amsterdam) to .Net's TimeZoneInfo classes.
https://github.com/robthree/timezonemapper
c-sharp cldr cldr-data dotnet mapper timezone unicode-consortium
Last synced: 6 months ago
JSON representation
Library for mapping *N*X TimeZone ID's (e.g. Europe/Amsterdam) to .Net's TimeZoneInfo classes.
- Host: GitHub
- URL: https://github.com/robthree/timezonemapper
- Owner: RobThree
- License: mit
- Created: 2013-11-06T18:43:57.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2024-03-07T18:11:55.000Z (over 1 year ago)
- Last Synced: 2025-04-14T18:05:50.101Z (6 months ago)
- Topics: c-sharp, cldr, cldr-data, dotnet, mapper, timezone, unicode-consortium
- Language: C#
- Homepage:
- Size: 1.91 MB
- Stars: 19
- Watchers: 4
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
#  TimeZoneMapper
 [](https://www.nuget.org/packages/TimeZoneMapper/)
Library for mapping \*N\*X TimeZone ID's (e.g. `Europe/Amsterdam`) to .Net's [TimeZoneInfo](http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx) classes. This mapping is one-way since, for example, `Europe/Amsterdam` maps to `W. Europe Standard Time` but `W. Europe Standard Time` could map to `Europe/Stockholm` or `Arctic/Longyearbyen` just as easily.
The library provides a simple static `TimeZoneMap` object that exposes 3 types of mappers, each described below under [usage](#usage). The project is kept up-to-date with the latest mapping information as much as I can, but TimeZoneMapper can use the latest mapping information available online completely transparently.
TimeZoneMapper is available as a [NuGet package](https://www.nuget.org/packages/TimeZoneMapper/) and comes with (basic) documentation in the form of a Windows Helpfile (.chm).
# Usage
The most basic example is as follows:
```c#
TimeZoneInfo tzi = TimeZoneMap.DefaultValuesTZMapper.MapTZID("Europe/Amsterdam");
````This uses the static TimeZoneMap.DefaultValuesTZMapper to map the string to the specific [TimeZoneInfo](http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx). The DefaultValuesTZMapper object uses a built-in resource containing the mapping information. If you want more up-to-date mapping information, you can use the `OnlineValuesTZMapper`.
```c#
TimeZoneInfo tzi = TimeZoneMap.OnlineValuesTZMapper.MapTZID("Europe/Amsterdam");
````This will retrieve the [information](https://unicode-org.github.io/cldr-staging/charts/) from the [Unicode Consortium](http://unicode.org/)'s latest [CLDR data](https://raw.githubusercontent.com/unicode-org/cldr/master/common/supplemental/windowsZones.xml). There is a catch though: what if, for some reason, this information is not available (for example an outbound HTTP request is blocked, the data is not available (HTTP status 404 for example) or the data is corrupt (invalid XML for some reason))? Well, simple, we just use the `OnlineWithFallbackValuesTZMapper`!
```c#
TimeZoneInfo tzi = TimeZoneMap.OnlineWithFallbackValuesTZMapper.MapTZID("Europe/Amsterdam");
````This will try to download the CLDR data from the Unicode Consortium and when that, for some reason fails, it uses the built-in values as fallback.
Note that an HTTP request will be made only once for as long as the TimeZoneMapper is around (usually the lifetime of the application). Also note that the TimeZoneMapper is **case-*in*sensitive**; the TimeZone ID `Europe/Amsterdam` works just as well as `EUROPE/AMSTERDAM` or `eUrOpE/AmStErDaM`.
Finally, when you want control over the actual CLDR data and where it is stored, how you cache it etc. you can use the `CustomValuesTZMapper`. Be sure to add the `TimeZoneMapper.TZMappers` namespace if you want to use this class. This class' constructor has 3 overloads demonstrated below:
```c#
// Overload 1: CustomValuesTZMapper(string, Encoding)// Load XML from file
var mapper = new CustomValuesTZMapper("myfile.xml", Encoding.UTF8);
TimeZoneInfo tzi = mapper.MapTZID("Europe/Amsterdam");
````
```c#
// Overload 2: CustomValuesTZMapper(string)
// Get XML from database, cache, online resource, file, etc. or, in this case, "hard-coded":
string cldrdata = "...";
var mapper = new CustomValuesTZMapper(cldrdata);
TimeZoneInfo tzi = mapper.MapTZID("Europe/Amsterdam");
````
```c#
// Overload 3: CustomValuesTZMapper(Stream)// Use a Stream
using (var mystream = new GZipStream(File.OpenRead("myfile.gz"), CompressionMode.Decompress))
{
var mapper = new CustomValuesTZMapper(mystream);
TimeZoneInfo tzi = mapper.MapTZID("Europe/Amsterdam");
}
````
All you need to do is ensure the data you supply to the CustomValuesTZMapper is valid CLDR data (see [this example](TimeZoneMapper/ResourceFiles/windowsZones.xml))# Future
I will try to update the built-in resource every now-and-then.
---
[Icon](https://www.deviantart.com/deviantdark/art/Oxygen-Refit-70199755) made by [deviantdark](https://www.deviantart.com/deviantdark), licensed by [Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)](https://creativecommons.org/licenses/by-nc-sa/3.0/) ([Archived page](http://riii.me/jcgob)).