65.9K
CodeProject is changing. Read more.
Home

Custom Clock for Different Time Zones

Jul 20, 2007

CPOL

1 min read

viewsIcon

34585

downloadIcon

1403

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:

  /// <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.

 /// <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.