Introduction
A few weeks back I had a requirement in ASP.NET in which I had to get the time of all the cities around the world. There is an hourly
scheduler that fires SMS
and want it to support world times and country codes.
There're two components needed here:
- A drop down of countries with timezones.
- SMS's for specific timezone are to be sent at 9am. The server time is currently +10 AEST (this may change) and the scheduler fires every hour (i.e., 9am, 10am, 11am etc).
I want a simple formula that calculates which timezones are at 9am (calculated by using the current server's time).
Background
To get my work done I used
TimeZoneInfo
class to get the local time in all over the world.
(http://msdn.microsoft.com/en-us/library/system.timezoneinfo.local.aspx) Timer
class which is executed at regular intervals infinitely in an application.
Using the Code
I used TimeZoneInfo.ConvertTimeBySystemTimeZoneId
in the sample code. It converts a time from one time zone to another based on time zone identifiers.
private void ShowTime()
{
DataTable dt = new DataTable();
int i=0;
string time = string.Empty;
string hour = string.Empty;
string minutes = string.Empty;
dt.Columns.Add("ID"); dt.Columns.Add("TimeZone");
lblLocalTime.Text = DateTime.Now.ToString();
ReadOnlyCollection<TimeZoneInfo> tzi;
tzi = TimeZoneInfo.GetSystemTimeZones();
lblTimeZone.Text = "<table border='1'>";
foreach (TimeZoneInfo timeZone in tzi)
{
dt.Rows.Add(i, timeZone.DisplayName);
i++;
time = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, TimeZoneInfo.Local.Id,
timeZone.Id).ToString().Substring(TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now,
TimeZoneInfo.Local.Id, timeZone.Id).ToString().IndexOf(" ") + 1); ;
hour = time.Substring(0, time.IndexOf(":"));
minutes = time.Substring(time.IndexOf(":") + 1, time.Length - time.IndexOf(" ") - 1);
if (hour.Equals("9"))
{
lblTimeZone.Text += "<tr><td>";
lblTimeZone.Text += timeZone.DisplayName;
lblTimeZone.Text += "</td><td>";
lblTimeZone.Text += TimeZoneInfo.ConvertTimeBySystemTimeZoneId(
DateTime.Now, TimeZoneInfo.Local.Id, timeZone.Id).ToString();
lblTimeZone.Text += "</td><td>";
lblTimeZone.Text += time;
lblTimeZone.Text += "</td><td>";
lblTimeZone.Text += timeZone.DisplayName.Substring(timeZone.DisplayName.IndexOf(")")+1);
lblTimeZone.Text += "</td><td>";
lblTimeZone.Text += hour + ", " + minutes;
lblTimeZone.Text += "</td></tr>";
}
}
lblTimeZone.Text += "</table>";
ddlCountry.DataSource = dt;
ddlCountry.DataTextField = "TimeZone";
ddlCountry.DataValueField = "ID";
ddlCountry.DataBind();
}
Once the Timer
control is added in the ASPX page, the page will postback at regular intervals you specify in the 'Interval
' property of the control:
<asp:Timer runat="server" ID="UpdateTimer"
Interval="6000" OnTick="UpdateTimer_Tick" />
Here the UpdateTimer_Tick
method will be called after every 6 seconds.
protected void UpdateTimer_Tick(object sender, EventArgs e)
{
ShowTime();
}
If the time in any city is 9am then that city name will be displayed in a grid.
Points of Interest
This article provides a very basic introduction to the TimeZoneInfo
and
Timer
classes.