2 * FreeRDP: A Remote Desktop Protocol Implementation
3 * Time Zone Redirection Table Generator
5 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 /* Run with ' csi scripts/TimeZones.csx' from freerdp checkout root */
23 using System.Globalization;
24 using System.Collections.ObjectModel;
26 struct SYSTEM_TIME_ENTRY
30 public UInt16 wDayOfWeek;
33 public UInt16 wMinute;
34 public UInt16 wSecond;
35 public UInt16 wMilliseconds;
38 struct TIME_ZONE_RULE_ENTRY
40 public long TicksStart;
42 public Int32 DaylightDelta;
43 public SYSTEM_TIME_ENTRY StandardDate;
44 public SYSTEM_TIME_ENTRY DaylightDate;
47 struct TIME_ZONE_ENTRY
51 public bool SupportsDST;
52 public string DisplayName;
53 public string StandardName;
54 public string DaylightName;
55 public string RuleTable;
56 public UInt32 RuleTableCount;
61 const string file = @"winpr/libwinpr/timezone/TimeZones.c";
62 TimeZoneInfo.AdjustmentRule[] rules;
63 StreamWriter stream = new StreamWriter(file, false);
64 ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();
66 Console.WriteLine("Updating " + file);
67 stream.WriteLine("/* ");
68 stream.WriteLine(" * Automatically generated with scripts/TimeZones.csx");
69 stream.WriteLine(" */ ");
72 stream.WriteLine("#include \"TimeZones.h\"");
77 foreach (TimeZoneInfo timeZone in timeZones)
79 rules = timeZone.GetAdjustmentRules();
81 if ((!timeZone.SupportsDaylightSavingTime) || (rules.Length < 1))
87 stream.WriteLine("static const TIME_ZONE_RULE_ENTRY TimeZoneRuleTable_{0}[] =", index);
88 stream.WriteLine("{");
91 foreach (TimeZoneInfo.AdjustmentRule rule in rules)
94 TIME_ZONE_RULE_ENTRY tzr;
95 TimeZoneInfo.TransitionTime transition;
97 tzr.TicksStart = rule.DateEnd.ToUniversalTime().Ticks;
98 tzr.TicksEnd = rule.DateStart.ToUniversalTime().Ticks;
99 tzr.DaylightDelta = (Int32)rule.DaylightDelta.TotalMinutes;
101 transition = rule.DaylightTransitionEnd;
102 time = transition.TimeOfDay;
104 tzr.StandardDate.wYear = (UInt16)0;
105 tzr.StandardDate.wMonth = (UInt16)transition.Month;
106 tzr.StandardDate.wDayOfWeek = (UInt16)transition.DayOfWeek;
107 tzr.StandardDate.wDay = (UInt16)transition.Week;
108 tzr.StandardDate.wHour = (UInt16)time.Hour;
109 tzr.StandardDate.wMinute = (UInt16)time.Minute;
110 tzr.StandardDate.wSecond = (UInt16)time.Second;
111 tzr.StandardDate.wMilliseconds = (UInt16)time.Millisecond;
113 transition = rule.DaylightTransitionStart;
114 time = transition.TimeOfDay;
116 tzr.DaylightDate.wYear = (UInt16)0;
117 tzr.DaylightDate.wMonth = (UInt16)transition.Month;
118 tzr.DaylightDate.wDayOfWeek = (UInt16)transition.DayOfWeek;
119 tzr.DaylightDate.wDay = (UInt16)transition.Week;
120 tzr.DaylightDate.wHour = (UInt16)time.Hour;
121 tzr.DaylightDate.wMinute = (UInt16)time.Minute;
122 tzr.DaylightDate.wSecond = (UInt16)time.Second;
123 tzr.DaylightDate.wMilliseconds = (UInt16)time.Millisecond;
126 stream.Write(" {0}ULL, {1}ULL, {2},", tzr.TicksStart, tzr.TicksEnd, tzr.DaylightDelta);
129 stream.Write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}",
130 tzr.StandardDate.wYear, tzr.StandardDate.wMonth, tzr.StandardDate.wDayOfWeek,
131 tzr.StandardDate.wDay, tzr.StandardDate.wHour, tzr.StandardDate.wMinute,
132 tzr.StandardDate.wSecond, tzr.StandardDate.wMilliseconds);
133 stream.Write(" }, ");
136 stream.Write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}",
137 tzr.DaylightDate.wYear, tzr.DaylightDate.wMonth, tzr.DaylightDate.wDayOfWeek,
138 tzr.DaylightDate.wDay, tzr.DaylightDate.wHour, tzr.DaylightDate.wMinute,
139 tzr.DaylightDate.wSecond, tzr.DaylightDate.wMilliseconds);
142 if (++i < rules.Length)
143 stream.WriteLine(" },");
145 stream.WriteLine(" }");
148 stream.WriteLine("};");
154 stream.WriteLine("const TIME_ZONE_ENTRY TimeZoneTable[] =");
155 stream.WriteLine("{");
157 foreach (TimeZoneInfo timeZone in timeZones)
160 TimeSpan offset = timeZone.BaseUtcOffset;
162 rules = timeZone.GetAdjustmentRules();
165 tz.Bias = -(Int32)offset.TotalMinutes;
167 tz.SupportsDST = timeZone.SupportsDaylightSavingTime;
169 tz.DisplayName = timeZone.DisplayName;
170 tz.StandardName = timeZone.StandardName;
171 tz.DaylightName = timeZone.DaylightName;
173 if ((!tz.SupportsDST) || (rules.Length < 1))
175 tz.RuleTableCount = 0;
176 tz.RuleTable = "NULL";
180 tz.RuleTableCount = (UInt32)rules.Length;
181 tz.RuleTable = "TimeZoneRuleTable_" + index;
184 stream.WriteLine("\t{");
186 stream.WriteLine("\t\t\"{0}\", {1}, {2}, \"{3}\",",
187 tz.Id, tz.Bias, tz.SupportsDST ? "TRUE" : "FALSE", tz.DisplayName);
189 stream.WriteLine("\t\t\"{0}\", \"{1}\",", tz.StandardName, tz.DaylightName);
190 stream.WriteLine("\t\t{0}, {1}", tz.RuleTable, tz.RuleTableCount);
194 if ((int)index < timeZones.Count)
195 stream.WriteLine("\t},");
197 stream.WriteLine("\t}");
199 stream.WriteLine("};");
201 stream.WriteLine("const size_t TimeZoneTableNrElements = ARRAYSIZE(TimeZoneTable);");