|
 |
TZ4Net Library |
|
|
C# /.NET implementation of timezone class based on
Olson database (often called tz or zoneinfo). As opposite to Win32/.NET
API, it allows to perform conversion of arbitrary time between arbitrary
time zones.
Key features:
- OlsonTimeZone class which subclasses
System.TimeZone
- Leapsecond support
- Time validation against "spring forward" and "fall back" regions
-
Unicode CLDR v.22.1 based
mapping between Win32 Id and Olson name
- Multiple naming scheme: TZIDs, aliases,
abbreviations, Win32 ids and names, military letters and names
- Latest version of tz package - 2013a
- Zone explorer in demo application to allow
browsing timezone data
- 100% managed code
- ASP.NET hosting, No-touch and Click-once
deployment friendly: no access to file system or registry needed
- Freeware
|
|
|
Thread safety: Library is not thread
safe by design. None of static and instance members are guaranteed
to be thread safe. In multithreaded environments like ASP.NET, an access
to library class members must by synchronized by the client code. |
| |
 |
Download |
 |
|
version 3.8.5 March 09, 2013 |
|
tz4netv3-2013a.zip (size: 7.15 MB) |
| |
Important
note for VS 2008 users : Project
migrated to VS 2008 fails on accessing both embedded resource files.
Workaround is to add additional .resources suffix to their names so they
are named zoneinfo.resources.resources and
cldrinfo.resources.resources accordingly. |
|
Architecture diagram : |
 |
|
Sample code that does not use leap seconds: |
|
using TZ4Net;
static DateTime Convert(DateTime srcTime,
string srcName,
string dstName)
{
if (OlsonTimeZone.LookupName(srcName) ==
null)
{
throw new
ArgumentException("Unknown source timezone name.");
}
if (OlsonTimeZone.LookupName(dstName) == null)
{
throw new
ArgumentException("Unknown destintation timezone name.");
}
OlsonTimeZone srcZone = OlsonTimeZone.GetInstance(srcName);
TimeCheckResult srcCheckRes = srcZone.CheckLocalTime(srcTime);
switch (srcCheckRes)
{
case TimeCheckResult.Valid :
{
OlsonTimeZone dstZone =
OlsonTimeZone.GetInstance(dstName);
DateTime dstTime =
dstZone.ToLocalTime(srcZone.ToUniversalTime(srcTime));
return dstTime;
}
case
TimeCheckResult.InSpringForwardGap :
case
TimeCheckResult.InFallBackRange :
{
throw new
ArgumentException("Source time in transition period.");
}
default :
{
throw new
ArgumentException("Source time out of range.");
}
}
} |
|
Sample code that uses leap seconds: |
|
using TZ4Net;
static ZoneInfo.Time Convert(ZoneInfo.Time srcTime,
string srcName, string dstName)
{
if (Array.IndexOf(ZoneInfo.GetAllNames("zoneinfo-leaps"),
srcName) < 0)
{
throw new
ArgumentException("Unknown source timezone name.");
}
if (Array.IndexOf(ZoneInfo.GetAllNames("zoneinfo-leaps"),
dstName) < 0)
{
throw new ArgumentException("Unknown
destintation timezone name.");
}
ZoneInfo srcZone = new ZoneInfo(srcName, "zoneinfo-leaps");
ZoneInfo dstZone = new ZoneInfo(dstName, "zoneinfo-leaps");
long srcClock = ZoneInfo.MinClock;
try
{
srcClock = srcZone.GetClockFromLocal(srcTime);
}
catch (ArgumentException)
{
throw new ArgumentException("Invalid
source time.");
}
return dstZone.GetLocalTime(srcClock);
} |
| |