7 using System.Xml.Schema;
11 private static string app = AppDomain.CurrentDomain.FriendlyName;
12 private static TextWriter stderr = Console.Error;
13 private static TextWriter stdout = Console.Out;
15 private static void usage()
17 stderr.WriteLine(
"Usage:");
18 stderr.WriteLine(app +
" <path to output files>");
22 private static bool writeZoneMapC(
string path)
24 string fname =
"TimeZoneNameMap";
25 string fpath = Path.Combine(path, fname +
"_static.h");
27 using (StreamWriter fs =
new StreamWriter(fpath))
29 fs.WriteLine(
"/* Automatically generated by " + app +
" */");
31 fs.WriteLine(
"#include \"" + fname +
".h\"");
33 fs.WriteLine(
"static const " + fname +
"Entry " + fname +
"[] ={");
36 foreach (System.TimeZoneInfo tz in System.TimeZoneInfo.GetSystemTimeZones())
39 System.TimeZoneInfo.TryConvertWindowsIdToIanaId(tz.Id, out iana);
41 StringBuilder sb =
new StringBuilder();
48 sb.Append(tz.StandardName);
50 sb.Append(tz.DisplayName);
52 sb.Append(tz.DaylightName);
56 fs.WriteLine(sb.ToString());
61 fs.WriteLine(
"static const size_t " + fname +
"Size = ARRAYSIZE(" + fname +
");");
66 private static bool writeZoneMapJSON(
string path)
68 string fname =
"TimeZoneNameMap";
69 string fpath = Path.Combine(path, fname +
".json");
71 using (StreamWriter fs =
new StreamWriter(fpath))
74 fs.WriteLine(
"\"TimeZoneNameMap\": [");
77 foreach (System.TimeZoneInfo tz in System.TimeZoneInfo.GetSystemTimeZones())
80 System.TimeZoneInfo.TryConvertWindowsIdToIanaId(tz.Id, out iana);
82 StringBuilder sb =
new StringBuilder();
87 sb.Append(
"\"Id\": \"");
89 sb.Append(
"\", \"StandardName\": \"");
90 sb.Append(tz.StandardName);
91 sb.Append(
"\", \"DisplayName\": \"");
92 sb.Append(tz.DisplayName);
93 sb.Append(
"\", \"DaylightName\": \"");
94 sb.Append(tz.DaylightName);
95 sb.Append(
"\", \"Iana\": \"");
98 fs.WriteLine(sb.ToString());
107 private static bool writeZoneMap(
string path)
109 if (!writeZoneMapC(path))
111 if (!writeZoneMapJSON(path))
116 private static void onValidation(
object sender, ValidationEventArgs e)
120 case XmlSeverityType.Warning:
121 case XmlSeverityType.Error:
122 stderr.WriteLine(e.ToString());
128 private static bool updateWindowsIanaMap(
string path)
131 "https://raw.githubusercontent.com/unicode-org/cldr/main/common/supplemental/windowsZones.xml";
132 string fname =
"WindowsZones";
133 string fpath = Path.Combine(path, fname +
".c");
135 XmlDocument doc =
new XmlDocument();
138 stdout.WriteLine(
"Downloaded and parsed XML from '" + url +
"'");
140 ValidationEventHandler handler =
new ValidationEventHandler(onValidation);
143 XmlNodeList versions = doc.SelectNodes(
"//supplementalData/version");
144 XmlNodeList zones = doc.SelectNodes(
"//supplementalData/windowsZones/mapTimezones");
146 doc.SelectNodes(
"//supplementalData/windowsZones/mapTimezones/mapZone");
148 using (StreamWriter fs =
new StreamWriter(fpath))
150 fs.WriteLine(
"/* Automatically generated by " + app);
152 fs.WriteLine(
" * url " + url);
154 foreach (XmlNode version
in versions)
156 XmlNode nr = version.Attributes.GetNamedItem(
"number");
157 fs.WriteLine(
" * version: " + nr.InnerText);
160 foreach (XmlNode node
in zones)
162 XmlNode over = node.Attributes.GetNamedItem(
"otherVersion");
163 XmlNode tver = node.Attributes.GetNamedItem(
"typeVersion");
164 fs.WriteLine(
" * mapTimezones: otherVersion: " + over.InnerText +
165 ", typeVersion: " + tver.InnerText);
170 fs.WriteLine(
"#include \"" + fname +
".h\"");
172 fs.WriteLine(
"const WINDOWS_TZID_ENTRY " + fname +
"[] = {");
174 foreach (XmlNode mzone
in mzones)
176 XmlAttributeCollection attrs = mzone.Attributes;
177 XmlNode wzid = attrs.GetNamedItem(
"other");
178 XmlNode territory = attrs.GetNamedItem(
"territory");
179 XmlNode iana = attrs.GetNamedItem(
"type");
180 fs.WriteLine(
"\t{ \"" + iana.InnerText +
"\", \"" + wzid.InnerText +
"\" }, // " +
181 territory.InnerText);
186 fs.WriteLine(
"const size_t " + fname +
"NrElements = ARRAYSIZE(" + fname +
");");
188 stdout.WriteLine(
"Finished update");
192 private static void Main(
string[] args)
196 if (args.Length == 0)
198 stderr.WriteLine(
"Missing arguments!");
202 DirectoryInfo info =
new DirectoryInfo(args[0]);
205 stderr.WriteLine(
"Path '" + info.FullName +
"' does not exist");
209 if (!writeZoneMap(info.FullName))
212 updateWindowsIanaMap(info.FullName);
216 stderr.WriteLine(e.ToString());
217 Environment.Exit(-23);