/jni/FreeRDP/scripts/TimeZones.cs

https://bitbucket.org/starup/android-rdp · C# · 255 lines · 198 code · 39 blank · 18 comment · 7 complexity · 94b1180f5e6453adeb62350385970ca4 MD5 · raw file

  1. /**
  2. * FreeRDP: A Remote Desktop Protocol Implementation
  3. * Time Zone Redirection Table Generator
  4. *
  5. * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  6. *
  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
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  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.
  18. */
  19. using System;
  20. using System.IO;
  21. using System.Globalization;
  22. using System.Collections.ObjectModel;
  23. namespace TimeZones
  24. {
  25. struct SYSTEM_TIME_ENTRY
  26. {
  27. public UInt16 wYear;
  28. public UInt16 wMonth;
  29. public UInt16 wDayOfWeek;
  30. public UInt16 wDay;
  31. public UInt16 wHour;
  32. public UInt16 wMinute;
  33. public UInt16 wSecond;
  34. public UInt16 wMilliseconds;
  35. };
  36. struct TIME_ZONE_RULE_ENTRY
  37. {
  38. public long TicksStart;
  39. public long TicksEnd;
  40. public Int32 DaylightDelta;
  41. public SYSTEM_TIME_ENTRY StandardDate;
  42. public SYSTEM_TIME_ENTRY DaylightDate;
  43. };
  44. struct TIME_ZONE_ENTRY
  45. {
  46. public string Id;
  47. public UInt32 Bias;
  48. public bool SupportsDST;
  49. public string DisplayName;
  50. public string StandardName;
  51. public string DaylightName;
  52. public string RuleTable;
  53. public UInt32 RuleTableCount;
  54. };
  55. class TimeZones
  56. {
  57. static void Main(string[] args)
  58. {
  59. int i;
  60. UInt32 index;
  61. const string file = @"TimeZones.txt";
  62. TimeZoneInfo.AdjustmentRule[] rules;
  63. StreamWriter stream = new StreamWriter(file, false);
  64. ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();
  65. stream.WriteLine();
  66. stream.WriteLine("struct _SYSTEM_TIME_ENTRY");
  67. stream.WriteLine("{");
  68. stream.WriteLine("\tuint16 wYear;");
  69. stream.WriteLine("\tuint16 wMonth;");
  70. stream.WriteLine("\tuint16 wDayOfWeek;");
  71. stream.WriteLine("\tuint16 wDay;");
  72. stream.WriteLine("\tuint16 wHour;");
  73. stream.WriteLine("\tuint16 wMinute;");
  74. stream.WriteLine("\tuint16 wSecond;");
  75. stream.WriteLine("\tuint16 wMilliseconds;");
  76. stream.WriteLine("};");
  77. stream.WriteLine("typedef struct _SYSTEM_TIME_ENTRY SYSTEM_TIME_ENTRY;");
  78. stream.WriteLine();
  79. stream.WriteLine("struct _TIME_ZONE_RULE_ENTRY");
  80. stream.WriteLine("{");
  81. stream.WriteLine("\tuint64 TicksStart;");
  82. stream.WriteLine("\tuint64 TicksEnd;");
  83. stream.WriteLine("\tsint32 DaylightDelta;");
  84. stream.WriteLine("\tSYSTEM_TIME_ENTRY StandardDate;");
  85. stream.WriteLine("\tSYSTEM_TIME_ENTRY DaylightDate;");
  86. stream.WriteLine("};");
  87. stream.WriteLine("typedef struct _TIME_ZONE_RULE_ENTRY TIME_ZONE_RULE_ENTRY;");
  88. stream.WriteLine();
  89. stream.WriteLine("struct _TIME_ZONE_ENTRY");
  90. stream.WriteLine("{");
  91. stream.WriteLine("\tconst char* Id;");
  92. stream.WriteLine("\tuint32 Bias;");
  93. stream.WriteLine("\tboolean SupportsDST;");
  94. stream.WriteLine("\tconst char* DisplayName;");
  95. stream.WriteLine("\tconst char* StandardName;");
  96. stream.WriteLine("\tconst char* DaylightName;");
  97. stream.WriteLine("\tTIME_ZONE_RULE_ENTRY* RuleTable;");
  98. stream.WriteLine("\tuint32 RuleTableCount;");
  99. stream.WriteLine("};");
  100. stream.WriteLine("typedef struct _TIME_ZONE_ENTRY TIME_ZONE_ENTRY;");
  101. stream.WriteLine();
  102. index = 0;
  103. foreach (TimeZoneInfo timeZone in timeZones)
  104. {
  105. rules = timeZone.GetAdjustmentRules();
  106. if ((!timeZone.SupportsDaylightSavingTime) || (rules.Length < 1))
  107. {
  108. index++;
  109. continue;
  110. }
  111. stream.WriteLine("static const TIME_ZONE_RULE_ENTRY TimeZoneRuleTable_{0}[] =", index);
  112. stream.WriteLine("{");
  113. i = 0;
  114. foreach (TimeZoneInfo.AdjustmentRule rule in rules)
  115. {
  116. DateTime time;
  117. TIME_ZONE_RULE_ENTRY tzr;
  118. TimeZoneInfo.TransitionTime transition;
  119. tzr.TicksStart = rule.DateEnd.ToUniversalTime().Ticks;
  120. tzr.TicksEnd = rule.DateStart.ToUniversalTime().Ticks;
  121. tzr.DaylightDelta = (Int32)rule.DaylightDelta.TotalMinutes;
  122. transition = rule.DaylightTransitionEnd;
  123. time = transition.TimeOfDay;
  124. tzr.StandardDate.wYear = (UInt16)0;
  125. tzr.StandardDate.wMonth = (UInt16)transition.Month;
  126. tzr.StandardDate.wDayOfWeek = (UInt16)transition.DayOfWeek;
  127. tzr.StandardDate.wDay = (UInt16)transition.Day;
  128. tzr.StandardDate.wHour = (UInt16)time.Hour;
  129. tzr.StandardDate.wMinute = (UInt16)time.Minute;
  130. tzr.StandardDate.wSecond = (UInt16)time.Second;
  131. tzr.StandardDate.wMilliseconds = (UInt16)time.Millisecond;
  132. transition = rule.DaylightTransitionStart;
  133. time = transition.TimeOfDay;
  134. tzr.DaylightDate.wYear = (UInt16)0;
  135. tzr.DaylightDate.wMonth = (UInt16)transition.Month;
  136. tzr.DaylightDate.wDayOfWeek = (UInt16)transition.DayOfWeek;
  137. tzr.DaylightDate.wDay = (UInt16)transition.Day;
  138. tzr.DaylightDate.wHour = (UInt16)time.Hour;
  139. tzr.DaylightDate.wMinute = (UInt16)time.Minute;
  140. tzr.DaylightDate.wSecond = (UInt16)time.Second;
  141. tzr.DaylightDate.wMilliseconds = (UInt16)time.Millisecond;
  142. stream.Write("\t{");
  143. stream.Write(" {0}ULL, {1}ULL, {2},", tzr.TicksStart, tzr.TicksEnd, tzr.DaylightDelta);
  144. stream.Write(" { ");
  145. stream.Write("{0}, {1}, {2}, {3}, {4}, {5}",
  146. tzr.StandardDate.wYear, tzr.StandardDate.wMonth, tzr.StandardDate.wDayOfWeek,
  147. tzr.StandardDate.wDay, tzr.StandardDate.wHour, tzr.StandardDate.wMinute,
  148. tzr.StandardDate.wSecond, tzr.StandardDate.wMilliseconds);
  149. stream.Write(" }, ");
  150. stream.Write("{ ");
  151. stream.Write("{0}, {1}, {2}, {3}, {4}, {5}",
  152. tzr.DaylightDate.wYear, tzr.DaylightDate.wMonth, tzr.DaylightDate.wDayOfWeek,
  153. tzr.DaylightDate.wDay, tzr.DaylightDate.wHour, tzr.DaylightDate.wMinute,
  154. tzr.DaylightDate.wSecond, tzr.DaylightDate.wMilliseconds);
  155. stream.Write(" },");
  156. if (++i < rules.Length)
  157. stream.WriteLine(" },");
  158. else
  159. stream.WriteLine(" }");
  160. }
  161. stream.WriteLine("};");
  162. stream.WriteLine();
  163. index++;
  164. }
  165. index = 0;
  166. stream.WriteLine("static const TIME_ZONE_ENTRY TimeZoneTable[] =");
  167. stream.WriteLine("{");
  168. foreach (TimeZoneInfo timeZone in timeZones)
  169. {
  170. Int32 sbias;
  171. TIME_ZONE_ENTRY tz;
  172. TimeSpan offset = timeZone.BaseUtcOffset;
  173. rules = timeZone.GetAdjustmentRules();
  174. tz.Id = timeZone.Id;
  175. if (offset.Hours >= 0)
  176. {
  177. sbias = (offset.Hours * 60) + offset.Minutes;
  178. tz.Bias = (UInt32) sbias;
  179. }
  180. else
  181. {
  182. sbias = (offset.Hours * 60) + offset.Minutes;
  183. tz.Bias = (UInt32) (1440 + sbias);
  184. }
  185. tz.SupportsDST = timeZone.SupportsDaylightSavingTime;
  186. tz.DisplayName = timeZone.DisplayName;
  187. tz.StandardName = timeZone.StandardName;
  188. tz.DaylightName = timeZone.DaylightName;
  189. if ((!tz.SupportsDST) || (rules.Length < 1))
  190. {
  191. tz.RuleTableCount = 0;
  192. tz.RuleTable = "NULL";
  193. }
  194. else
  195. {
  196. tz.RuleTableCount = (UInt32)rules.Length;
  197. tz.RuleTable = "&TimeZoneRuleTable_" + index;
  198. tz.RuleTable = "(TIME_ZONE_RULE_ENTRY*) &TimeZoneRuleTable_" + index;
  199. }
  200. stream.WriteLine("\t{");
  201. stream.WriteLine("\t\t\"{0}\", {1}, {2}, \"{3}\",",
  202. tz.Id, tz.Bias, tz.SupportsDST ? "true" : "false", tz.DisplayName);
  203. stream.WriteLine("\t\t\"{0}\", \"{1}\",", tz.StandardName, tz.DaylightName);
  204. stream.WriteLine("\t\t{0}, {1}", tz.RuleTable, tz.RuleTableCount);
  205. index++;
  206. if ((int)index < timeZones.Count)
  207. stream.WriteLine("\t},");
  208. else
  209. stream.WriteLine("\t}");
  210. }
  211. stream.WriteLine("};");
  212. stream.WriteLine();
  213. stream.Close();
  214. }
  215. }
  216. }