The Inner Drive Extensible Architecture™—the Idea™

Demonstration: Time zones

The IDEA™ includes a fully-object-oriented, easy-to-integrate set of tools to allow you to use the Internet-standard tzinfo database—used throughout the world on millions of Unix computers—in your .NET apps.

Explore time zones

We've loaded 503 time zones for you to check out:

Choose a world region
Find the zone name
Current time
UTC Thursday, October 30, 2014 12:11:44 PM UTC
Inner Drive HQ Thursday, October 30, 2014 7:11:44 AM CDT
America/Chicago Thursday, October 30, 2014 7:11:44 AM CDT

Buy a license

If you want to use the IDEA™ in your commercial application, check out our licensing options.

How this works

The time zone handling classes are in the InnerDrive.Geography assembly. Most are internal to the assembly; generally, you only need to work with the TimeZoneFactory class. The factory parses the raw tzinfo database and makes it available through a singleton instance.

First, the App.config or Web.config file (or wherever you store app settings) needs the following keys:

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
   <appSettings>
      <add key="DefaultTimeZoneRuleFile" value="C:\Source\InnerDrive\Utilities\tzinfo database\northamerica"/>
      <add key="DefaultTimeZoneHomeZone" value="America/Chicago"/>
      <add key="TimeZoneRuleFolder" value="C:\Source\InnerDrive\Utilities\tzinfo database"/>
      <add key="TimeZoneRuleFileFull" value="europe,northAmerica,pacificnew,southamerica,
		africa,antarctica,asia,australasia,backward,etcetera,leapseconds,solar87" />
   </appSettings>
</configuration>

At application start, the factory initializes. The Application_Start event handler calls this method:

private static void LoadTimeZones()
{
   var fileSpecListRaw = ConfigurationManager.AppSettings[TimeZoneFactory.DefaultTimeZoneRuleFileListSettingKey];
   var fileSpecList = fileSpecListRaw.Split(',');
   var dataFolder = ConfigurationManager.AppSettings[TimeZoneFactory.DefaultTimeZoneRuleFolderSettingKey];
   try
   {
      TimeZoneFactory.Instance.LoadTzInfoFiles(dataFolder, fileSpecList);
   }
   catch (InvalidTimeZoneException itex)
   {
      // log the exception
   }
   // other exceptions elided
}
	

(By default, the factory will not throw an exception if a time zone definition is corrupt. The time zone will be unavaliable, however.)

At this point, you can use the default time zone—on this site, "America/Chicago", UTC-05:00—in one method call:

var thisZone = TimeZoneFactory.DefaultTimeZone;
var localTime = thisZone.ToLocalTime(DateTimeOffset.UtcNow);
	

Using a different zone is almost that easy. You simply call the TimeZoneFactory.FindZone method with the name of the zone you want. Here's the code from this page's code-behind:

private void SetLocalTime()
{
   var localZone = SelectedZone;
   resultPrompt.Text = localZone.Name;
   SetText(localZone, resultText);
}

private void SetText(IdtTimeZone timeZone, ITextControl control)
{
   var currentInfo = timeZone.CurrentInfo(SelectedTime);
   control.Text = string.Format(CultureInfo.CurrentCulture, "{0:D} {0:T} {1}",
      currentInfo.LocalTime,
      currentInfo.Abbreviation);
}
	

Behind the scenes, time zone information is cached, making lookups very fast, and saving the trouble (and computing resources) of loading the tzinfo database again.

More information

For more information, send us some feedback and someone will get right back to you.

InnerDrive.Geography 3.1.5251.1 Release CLR 4.0.30319.34014
Copyright ©2014 Inner Drive Technology. All rights reserved. Portions Copyright ©1986-2013 David Braverman.

Return home.

Copyright ©2014 Inner Drive Technology. Purveyors of fine, hand-crafted computer software. Legal information. Page printed 2014-10-30T12:11:44 UTC.