|
 |
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.1.7.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 - 2009p
- 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
|
|
| |
 |
Download |
 |
 |
|
version 3.6.1 November 1, 2009 |
|
tz4netv3-2009p.zip (size: 5.19 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 leapseconds: |
|
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 leapseconds: |
|
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);
} |
| |