Quick Objects Documentation Akal Tech Logo
Converting DateTime Values To And From UTC To Any Time Zone

Glossary Item Box

Time Zones - Tutorial 1:

With more and more distributed applications being developed and used it is becoming an essential part of the development process to be able to provide DateTime values in a user's time zone. A lot of time zones observe day light savings (DST), their time changes during a pre determined period of time. To add this problem the DST rules can be changes anytime, meaning that there is no guarantee that one rule that works in one year may or may not work in another year. To add to this problem is the inability of the Windows based operating systems to keep track of all these rules. Essentially when one rule changes a new update is issued by Microsoft that updates the DST rule for that time zone in Windows. This however causes the old rule to be lost, and hence now if an application that relies on Windows data and calculations to calculate time zone differences will end up with wrong results for the previous year. Microsoft and other Software Vendors that rely on this technique have to wait for the year in which the DST rule is going to change to issue such an update and can not be issued before that. Once this patch/update is applied on a system the date and time values from previous years that did not fall in DST time may end up being considered as part of DST time and the time calculated by the Windows API will inaccurate. 

In addition to the above mentioned problem of time zone calculations in windows operating system, the .NET Framework does not provide any means at all for a .NET developer to calculate time zone difference or even provide access to the windows time zone rules and data.  QuickObjects TimeZones libraries provides a quick, easy and flexible solution to all of these problems. The data for all time zones including their appropriate DST rules are included in the library itself (only 10 are available in trial version). The library also provides methods that allow the developer to get time in any given time zone when UTC time zone is available or when UTC time from any local time. These calculation automatically apply the correct DST rules appropriate for the time being converted.

The following tutorial shows how to get a list of all the time zones from the library. Each time zone is uniquely marked with a key (an integer value). There are two important classes in the library that make up the time zone conversion calculations.

public Main()

{

InitializeComponent();

// TimeZones class does not provide a public constructor but provides a method that returns an instance of the TimeZones class.

Akal.QuickObjects.TimeZones.TimeZones tz = Akal.QuickObjects.TimeZones.TimeZones.GetInstance();

// We can bind the instance of time zones to any data bound control and display the list of time zones and their key values.

this.GridView.DataSource = tz;

this.DateTimePicker.Text = DateTime.Now.ToString();

}

private void Button_Convert_Click(object sender, EventArgs e)

{

this.Label_ConvertedTime.Text = "";

if (this.GridView.SelectedRows.Count > 0)

{

// Get the time zone key. The time zone key is a number that is used to uniquely identify a time zone.

int key = (int) this.GridView.SelectedRows[0].Cells["Key"].Value;

// Parse the date time entered by the user in the text box.

DateTime value = DateTime.Parse(this.DateTimePicker.Text);

// The following line shows how to get UTC time equivalent to the passed in date time value and the time zone.

// For example if you selected PST (GMT +08:00 Pacific Standard Time) zone then the following line will return the

// UTC time that is equivalent local time in the PST time zone as entered in the text box by the user.

string utc = Akal.QuickObjects.TimeZones.TimeZoneInformation.GetUtcFromLocalTime(value, key).ToString();

// The following line shows how to get local time equivalent to the passed in UTC date time value and the time zone.

// For example if you selected PST (GMT +08:00 Pacific Standard Time) zone then the following line will return the

// local time in the PST time zone from the UTC time.

string local = Akal.QuickObjects.TimeZones.TimeZoneInformation.GetLocalTimeFromUtc(value, key).ToString();

this.Label_ConvertedTime.Text = "If you entered local time then UTC time is " + utc + "\n"

+ "If you entered UTC time then local time in the selected time zone is " + local;

}

else

{

MessageBox.Show("You need to select a time zone from the time zones list first.");

}

}