Click here to Skip to main content
Licence CPOL
First Posted 20 Jul 2007
Views 15,508
Downloads 563
Bookmarked 14 times

Custom Clock for Different Time Zones

By | 20 Jul 2007 | Article
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.

License

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

About the Author

Mahendra Kumar Srivastava

Software Developer (Senior)
Misys
India India

Member

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 with Misys in Banking domain.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 20 Jul 2007
Article Copyright 2007 by Mahendra Kumar Srivastava
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid