Click here to Skip to main content
15,868,292 members
Articles / Programming Languages / C#
Article

Custom Clock for Different Time Zones

Rate me:
Please Sign up or sign in to vote.
3.40/5 (7 votes)
20 Jul 2007CPOL1 min read 34.1K   1.4K   14   2
This project is a Custom Clock project which displays time for different TimeZones.
Screenshot - CustomClock.jpg

Introduction

The custom clock is an application which reads the TimeZone information from the registry and starts the application. Based upon the selection by user, the display below the combobox displays the current time for the selected time zone.

Background

I was looking for a code snippet to fetch TimeZone information in C#. I did not find one which satisfies my needs. So I wrote this application.

Using the Code

The code basically depends upon the information stored in registry for different TimeZones.

The following function fetches the data from registry and creates a table for this information:

C#
/// <summary>
/// Loads the time zones information from Registry.
/// </summary>
private DataTable LoadTimeZones()
{
 DataTable dtTimeZoneInfo = new DataTable();
 dtTimeZoneInfo.Columns.Add("Display", typeof(string));
 dtTimeZoneInfo.Columns.Add("Dlt", typeof(string));
 dtTimeZoneInfo.Columns.Add("Std", typeof(string));
 dtTimeZoneInfo.Columns.Add("MapID", typeof(string));
 dtTimeZoneInfo.Columns.Add("Index", typeof(string));
 dtTimeZoneInfo.Columns.Add("TZI", typeof(byte[]));
 dtTimeZoneInfo.Columns.Add("TIME_ZONE_INFORMATION", typeof(TIME_ZONE_INFORMATION));
 RegistryKey registryTimeInfo = Registry.LocalMachine.OpenSubKey
      (@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones");
 foreach (string strTimeInfo in registryTimeInfo.GetSubKeyNames())
 {
  RegistryKey registryZoneKey = registryTimeInfo.OpenSubKey(strTimeInfo);
  DataRow drTz = dtTimeZoneInfo.NewRow();
  foreach (string specificInfo in registryZoneKey.GetValueNames())
  {
   object obValue = registryZoneKey.GetValue(specificInfo);
   drTz[specificInfo] = obValue;
  }
  drTz["TIME_ZONE_INFORMATION"] = CreateStructure((byte[])drTz["TZI"]);
  dtTimeZoneInfo.Rows.Add(drTz);
 }
 return dtTimeZoneInfo;
}

The following structure is used to store the Binary registry data for TimeZone. We use the BitConverter class to convert available bits into specific data types.

C#
/// <summary>
/// The Time Zone structure used to hold
/// the related data for an specific time zone.
/// </summary>
public struct TIME_ZONE_INFORMATION
{
 public int Bias;    // 4
 public int StandardBias;  // 4
 public int DaylightBias;  // 4
 public SYSTEMTIME StandardDate; // 16
 public SYSTEMTIME DaylightDate; // 16
}
/// <summary>
/// This structure is not used for our application.
/// </summary>
public struct SYSTEMTIME
{
 public ushort wYear;
 public ushort wMonth;
 public ushort wDayOfWeek;
 public ushort wDay;
 public ushort wHour;
 public ushort wMinute;
 public ushort wSecond;
 public ushort wMilliseconds;
}

For more information on TimeZone structure, you can refer to this MSDN link.

Points of Interest

The application also uses the NotifyIcon class for placing a SystemTray icon. It can be a sample for developers who want to have an idea on how to place an icon in system tray. Do remember the class is only available in .NET 2.0 or higher versions.

It also implements the moving of form by grabbing any part of form. This might be helpful to some developers.

History

  • 20th July, 2007: First version of the application submitted

Ideas are welcome and you are free to use the code anywhere as per your requirement.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader M*Modal
India India
Started carreer with CEERI, Delhi, India as a C Programmer in 2004 and migrated my learning experiance to C#.Net in 2005 with DIGISIGN Noida, Joined Bally in 2006, worked with JK Technosoft since Jun 2007 till Apr 2009, joined back Bally in June 2009 and joined Misys in June 2010. Worked in different domains such as Casino Management, Digital Signage, and Medical and currently working M*Modal as Lead - Product Development.

Comments and Discussions

 
BugTo make it work in windows 8 or any do the following Pin
yakirmanor11-Jun-13 23:29
yakirmanor11-Jun-13 23:29 
GeneralMy vote of 5 Pin
bellay29-Oct-12 22:46
bellay29-Oct-12 22:46 
Nice

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.