1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4:
5: namespace TimeZoneConversion
6: {
7: class Program
8: {
9: // Demonstrates using TimeZoneInfo to convert to/from UTC, and between two time zones
10: // using UTC as an interim step. Shows the real differences between two time zones as
11: // the time change occurs, and how .NET handles the overlapping hour when the clock
12: // rolls back to Standard Time.
13: //
14: // The time zones in this example are Eastern and Pacific Time, and can adjusted to anything.
15: static void Main(string[] args)
16: {
17: TimeZone zone = TimeZone.CurrentTimeZone;
18: DateTime dt1 = new DateTime(2008, 1, 1);
19: DateTime dt2 = new DateTime(2008, 6, 1);
20: Console.WriteLine(zone.IsDaylightSavingTime(dt1));
21: Console.WriteLine(zone.IsDaylightSavingTime(dt2));
22: Console.WriteLine(zone.GetUtcOffset(dt1));
23: Console.WriteLine(zone.GetUtcOffset(dt2));
24:
25: TimeZoneInfo zone1 = TimeZoneInfo.Local;
26: Console.WriteLine(zone1.StandardName);
27: Console.WriteLine(zone1.DaylightName);
28:
29: string Zone_ID = "Eastern Standard Time";
30: foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
31: {
32: Console.WriteLine(z.Id);
33: if (z.Id == Zone_ID) Console.WriteLine("********************");
34: }
35:
36: Console.WriteLine();
37: Console.WriteLine("******************** Convert UTC to time zone in Zone_ID: {0}", Zone_ID);
38:
39: string[] TestTimes1 = new string[] {
40: "3/8/2009 05:59:59", // last minute before Daylight Savings Time switch
41: "3/8/2009 06:00:00", // first minute of Daylight Savings Time switch
42: "3/8/2009 06:59:59",
43: "3/8/2009 07:00:00",
44: "11/1/2009 05:59:00", // last minute before Standard Time switch
45: "11/1/2009 06:00:00", // last minute before Standard Time switch
46: "11/1/2009 06:59:00",
47: "11/1/2009 07:00:00"
48: };
49:
50: foreach (string timestamp in TestTimes1)
51: {
52: DateTime TestTime = DateTime.Parse(timestamp); // UTC
53: DateTime Zoned = TimeZoneInfo.ConvertTimeFromUtc(TestTime, TimeZoneInfo.FindSystemTimeZoneById(Zone_ID));
54: Console.WriteLine("UTC: {0} = EST/EDT: {1}", TestTime, Zoned);
55: }
56:
57: // Convert between two time zones, using UTC as the axis. Note the effect during the
58: // 2:00AM time change between zones.
59: TimeZoneInfo EasternTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
60: Console.WriteLine(EasternTimeZone.StandardName);
61: Console.WriteLine(EasternTimeZone.DaylightName);
62: TimeZoneInfo PacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
63: Console.WriteLine(PacificTimeZone.StandardName);
64: Console.WriteLine(PacificTimeZone.DaylightName);
65:
66: Console.WriteLine();
67: Console.WriteLine("******************** Convert Eastern Time to Pacific Time");
68:
69: string[] TestTimes2 = new string[] {
70: "3/8/2009 01:59:59", // last minute before Daylight Savings Time switch
71: "3/8/2009 03:00:00", // first minute of Daylight Savings Time switch.. 4 hours ahead of PST
72: "3/8/2009 04:00:00", // 4 hours ahead of PST
73: "3/8/2009 05:00:00", // 4 hours ahead of PST
74: "3/8/2009 06:00:00", // first minute of PDT, now 3 hour difference resumes.
75: "11/1/2009 00:59:00", // last minute before Standard Time switch
76: "11/1/2009 01:00:00", // first minute of Daylight Savings Time switch.. 2 hours ahead of PST
77: "11/1/2009 02:00:00", // 2 hours ahead of PDT
78: "11/1/2009 03:00:00", // 2 hours ahead of PDT
79: "11/1/2009 04:00:00" // first minute of PST, now 3 hour difference resumes.
80: };
81:
82: // So here is the ambiguity: switching from Daylight Savings Time to Standard Time
83: // causes the 1AM hour to occur twice on the day of the switch. The TimeZoneInfo
84: // object will treat it as the 2nd occurrence.
85:
86: // Note: 02:00:00 to 02:59:59 on the day when the clock is moved ahead (i.e.
87: // Standard to Daylight Savings switch) is illegal. This hour is lost when switching
88: // to Daylight Savings from Standard, and will throw an error.
89:
90: foreach (string timestamp in TestTimes2)
91: {
92: DateTime TestTime = DateTime.Parse(timestamp); // Eastern Time
93: DateTime UTC = TimeZoneInfo.ConvertTimeToUtc(TestTime, EasternTimeZone);
94: DateTime Pacific = TimeZoneInfo.ConvertTimeFromUtc(UTC, PacificTimeZone);
95: Console.WriteLine("ExT: {0} UTC: {1} PxT: {2}", TestTime, UTC, Pacific);
96: }
97:
98: #if DEBUG
99: Console.ReadLine();
100: #endif
101: }
102: }
103: }