TimeZoneMapper 1.3.1
dotnet add package TimeZoneMapper --version 1.3.1
NuGet\Install-Package TimeZoneMapper -Version 1.3.1
<PackageReference Include="TimeZoneMapper" Version="1.3.1" />
paket add TimeZoneMapper --version 1.3.1
#r "nuget: TimeZoneMapper, 1.3.1"
// Install TimeZoneMapper as a Cake Addin #addin nuget:?package=TimeZoneMapper&version=1.3.1 // Install TimeZoneMapper as a Cake Tool #tool nuget:?package=TimeZoneMapper&version=1.3.1
TimeZoneMapper
Library for mapping *N*X TimeZone ID's (e.g. Europe/Amsterdam
) to .Net's TimeZoneInfo 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. 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 and comes with (basic) documentation in the form of a Windows Helpfile (.chm).
Usage
The most basic example is as follows:
TimeZoneInfo tzi = TimeZoneMap.DefaultValuesTZMapper.MapTZID("Europe/Amsterdam");
This uses the static TimeZoneMap.DefaultValuesTZMapper to map the string to the specific TimeZoneInfo. 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
.
TimeZoneInfo tzi = TimeZoneMap.OnlineValuesTZMapper.MapTZID("Europe/Amsterdam");
This will retrieve the information from the Unicode Consortium's latest CLDR data. 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
!
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-insensitive; 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:
// Overload 1: CustomValuesTZMapper(string, Encoding)
// Load XML from file
var mapper = new CustomValuesTZMapper("myfile.xml", Encoding.UTF8);
TimeZoneInfo tzi = mapper.MapTZID("Europe/Amsterdam");
// Overload 2: CustomValuesTZMapper(string)
// Get XML from database, cache, online resource, file, etc. or, in this case, "hard-coded":
string cldrdata = "<supplementalData><windowsZones><mapTimezones otherVersion=\"xyz\" typeVersion=\"zyx\">...";
var mapper = new CustomValuesTZMapper(cldrdata);
TimeZoneInfo tzi = mapper.MapTZID("Europe/Amsterdam");
// 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)
Future
I will try to update the built-in resource every now-and-then.
Icon made by deviantdark, licensed by Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0) (Archived page).
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on TimeZoneMapper:
Package | Downloads |
---|---|
DensoCreate.TimeTracker.ApiClient.Net
デンソークリエイトのプロジェクト管理ツール Time Tracker NX(https://www.timetracker.jp/)の WebAPI を利用するための C# ライブラリです。 なお、WebAPI の主要なメソッド、パラメータに対応します。 WebAPI の詳細はリファレンスを参照してください。 # Time Tracker NX - Web API ヘルプ - https://www.timetracker.jp/support/help/web-api/index.html |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.3.1 | 1,030 | 3/7/2024 |
1.3.0 | 7,230 | 7/19/2023 |
1.2.31 | 29,073 | 5/8/2021 |
1.2.30 | 60,408 | 10/20/2020 |
1.2.29 | 3,144 | 8/27/2020 |
1.2.28 | 46,487 | 4/26/2020 |
1.2.27 | 4,935 | 8/30/2019 |
1.2.26 | 45,991 | 1/2/2019 |
1.2.25 | 2,090 | 10/30/2018 |
1.2.24 | 12,300 | 8/18/2018 |
1.2.23 | 39,804 | 5/15/2018 |
1.2.22 | 1,081 | 5/7/2018 |
1.2.21 | 3,347 | 3/28/2018 |
1.2.20 | 2,593 | 2/7/2018 |
1.2.19 | 1,144 | 1/25/2018 |
1.2.18 | 36,486 | 5/26/2017 |
1.2.17 | 3,006 | 3/8/2017 |
1.2.16 | 2,788 | 11/29/2016 |
1.2.15 | 1,265 | 11/15/2016 |
1.2.14 | 1,376 | 11/4/2016 |
1.2.13 | 1,178 | 10/12/2016 |
1.2.12 | 2,806 | 9/13/2016 |
1.2.11 | 1,813 | 8/30/2016 |
1.2.10 | 2,628 | 4/20/2016 |
1.2.9 | 1,530 | 4/4/2016 |
1.2.8 | 19,229 | 2/2/2016 |
1.2.7 | 2,606 | 10/12/2015 |
1.2.6 | 1,259 | 8/27/2015 |
1.2.5 | 3,048 | 6/19/2015 |
1.2.4 | 11,139 | 3/2/2015 |
1.2.3 | 1,494 | 1/10/2015 |
1.2.2 | 1,452 | 10/2/2014 |
1.2.1 | 2,143 | 9/4/2014 |
1.2.0 | 1,459 | 7/10/2014 |
1.1.3 | 1,240 | 7/9/2014 |
1.1.2 | 1,444 | 4/4/2014 |
1.1.1 | 1,369 | 1/15/2014 |
1.1.0 | 3,010 | 11/7/2013 |
1.0.0 | 1,504 | 11/6/2013 |