/// <summary> /// Runs the compiler from the command line. /// </summary> /// <param name="arguments">The command line arguments. Each compiler defines its own.</param> /// <returns>0 for success, non-0 for error.</returns> private static int Main(string[] arguments) { CompilerOptions options = new CompilerOptions(); ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error)); if (!parser.ParseArguments(arguments, options)) { return(1); } var writer = CreateWriter(options); var tzdbCompiler = new TzdbZoneInfoCompiler(); var tzdb = tzdbCompiler.Compile(options.SourceDirectoryName); tzdb.LogCounts(); var windowsZones = CldrWindowsZonesParser.Parse(options.WindowsMappingFile); LogWindowsZonesSummary(windowsZones); writer.Write(tzdb, windowsZones); Console.WriteLine("Reading generated data and validating..."); var source = Read(options); source.Validate(); return(0); }
/// <summary> /// Runs the compiler from the command line. /// </summary> /// <param name="arguments">The command line arguments. Each compiler defines its own.</param> /// <returns>0 for success, non-0 for error.</returns> private static async Task <int> Main(string[] arguments) { CompilerOptions options = new CompilerOptions(); ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error) { MutuallyExclusive = true }); if (!parser.ParseArguments(arguments, options)) { return(1); } var tzdbCompiler = new TzdbZoneInfoCompiler(); var tzdb = await tzdbCompiler.CompileAsync(options.SourceDirectoryName !); tzdb.LogCounts(); if (options.ZoneId != null) { tzdb.GenerateDateTimeZone(options.ZoneId); return(0); } var windowsZones = LoadWindowsZones(options, tzdb.Version); if (options.WindowsOverride != null) { var overrideFile = CldrWindowsZonesParser.Parse(options.WindowsOverride); windowsZones = MergeWindowsZones(windowsZones, overrideFile); } LogWindowsZonesSummary(windowsZones); var writer = new TzdbStreamWriter(); using (var stream = CreateOutputStream(options)) { writer.Write(tzdb, windowsZones, NameIdMappingSupport.StandardNameToIdMap, stream); } if (options.OutputFileName != null) { Console.WriteLine("Reading generated data and validating..."); var source = Read(options); source.Validate(); } if (options.XmlSchema is object) { Console.WriteLine($"Writing XML schema to {options.XmlSchema}"); var source = Read(options); var provider = new DateTimeZoneCache(source); XmlSerializationSettings.DateTimeZoneProvider = provider; var settings = new XmlWriterSettings { Indent = true, NewLineChars = "\n", Encoding = new UTF8Encoding() }; using var xmlWriter = XmlWriter.Create(options.XmlSchema, settings); XmlSchemaDefinition.NodaTimeXmlSchema.Write(xmlWriter); } return(0); }
/// <summary> /// Runs the compiler from the command line. /// </summary> /// <param name="arguments">The command line arguments. Each compiler defines its own.</param> /// <returns>0 for success, non-0 for error.</returns> private static int Main(string[] arguments) { CompilerOptions options = new CompilerOptions(); ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error) { MutuallyExclusive = true }); if (!parser.ParseArguments(arguments, options)) { return(1); } var tzdbCompiler = new TzdbZoneInfoCompiler(); var tzdb = tzdbCompiler.Compile(options.SourceDirectoryName !); tzdb.LogCounts(); if (options.ZoneId != null) { tzdb.GenerateDateTimeZone(options.ZoneId); return(0); } var windowsZones = LoadWindowsZones(options, tzdb.Version); if (options.WindowsOverride != null) { var overrideFile = CldrWindowsZonesParser.Parse(options.WindowsOverride); windowsZones = MergeWindowsZones(windowsZones, overrideFile); } LogWindowsZonesSummary(windowsZones); var writer = new TzdbStreamWriter(); using (var stream = CreateOutputStream(options)) { writer.Write(tzdb, windowsZones, NameIdMappingSupport.StandardNameToIdMap, stream); } if (options.OutputFileName != null) { Console.WriteLine("Reading generated data and validating..."); var source = Read(options); source.Validate(); } return(0); }
public void Versions_Absent() { string xml = @" <supplementalData> <generation date='$Date: 2012-01-14 03:54:32 -0600 (Sat, 14 Jan 2012) $' /> <windowsZones> <mapTimezones> </mapTimezones> </windowsZones> </supplementalData> "; var doc = XDocument.Parse(xml); var parsed = CldrWindowsZonesParser.Parse(doc); Assert.AreEqual("", parsed.Version); Assert.AreEqual("", parsed.WindowsVersion); Assert.AreEqual("", parsed.TzdbVersion); }
public void Versions_Present() { string xml = @" <supplementalData> <version number='$Revision: rev-version $'/> <generation date='$Date: 2012-01-14 03:54:32 -0600 (Sat, 14 Jan 2012) $' /> <windowsZones> <mapTimezones otherVersion='win-version' typeVersion='tzdb-version'> </mapTimezones> </windowsZones> </supplementalData> "; var doc = XDocument.Parse(xml); var parsed = CldrWindowsZonesParser.Parse(doc); Assert.AreEqual("rev-version", parsed.Version); Assert.AreEqual("win-version", parsed.WindowsVersion); Assert.AreEqual("tzdb-version", parsed.TzdbVersion); }
/// <summary> /// Loads the best windows zones file based on the options. If the WindowsMapping option is /// just a straight file, that's used. If it's a directory, this method loads all the XML files /// in the directory (expecting them all to be mapping files) and then picks the best one based /// on the version of TZDB we're targeting - basically, the most recent one before or equal to the /// target version. /// </summary> private static WindowsZones LoadWindowsZones(CompilerOptions options, string targetTzdbVersion) { var mappingPath = options.WindowsMapping !; if (File.Exists(mappingPath)) { return(CldrWindowsZonesParser.Parse(mappingPath)); } if (!Directory.Exists(mappingPath)) { throw new Exception($"{mappingPath} does not exist as either a file or a directory"); } var xmlFiles = Directory.GetFiles(mappingPath, "*.xml"); if (xmlFiles.Length == 0) { throw new Exception($"{mappingPath} does not contain any XML files"); } var allFiles = xmlFiles // Expect that we've ordered the files so that this gives "most recent first", // to handle consecutive CLDR versions that have the same TZDB version. .OrderByDescending(file => file, StringComparer.Ordinal) .Select(file => CldrWindowsZonesParser.Parse(file)) // Note: this is stable, so files with the same TZDB version will stay in reverse filename order. .OrderByDescending(zones => zones.TzdbVersion) .ToList(); var versions = string.Join(", ", allFiles.Select(z => z.TzdbVersion).ToArray()); var bestFile = allFiles .Where(zones => StringComparer.Ordinal.Compare(zones.TzdbVersion, targetTzdbVersion) <= 0) .FirstOrDefault(); if (bestFile is null) { throw new Exception($"No zones files suitable for version {targetTzdbVersion}. Found versions targeting: [{versions}]"); } Console.WriteLine($"Picked Windows Zones with TZDB version {bestFile.TzdbVersion} out of [{versions}] as best match for {targetTzdbVersion}"); return(bestFile); }
public void MapZones() { string xml = @" <supplementalData> <windowsZones> <mapTimezones> <mapZone other='X' territory='t0' type='' /> <mapZone other='X' territory='t1' type='A' /> <mapZone other='X' territory='t2' type='B C' /> </mapTimezones> </windowsZones> </supplementalData> "; var doc = XDocument.Parse(xml); var parsed = CldrWindowsZonesParser.Parse(doc); var mapZones = parsed.MapZones; Assert.AreEqual(3, mapZones.Count); Assert.AreEqual(new MapZone("X", "t0", new string[0]), mapZones[0]); Assert.AreEqual(new MapZone("X", "t1", new[] { "A" }), mapZones[1]); Assert.AreEqual(new MapZone("X", "t2", new[] { "B", "C" }), mapZones[2]); }
/// <summary> /// Loads the best windows zones file based on the options. If the WindowsMapping option is /// just a straight file, that's used. If it's a directory, this method loads all the XML files /// in the directory (expecting them all to be mapping files) and then picks the best one based /// on the version of TZDB we're targeting - basically, the most recent one before or equal to the /// target version. /// </summary> private static WindowsZones LoadWindowsZones(CompilerOptions options, string targetTzdbVersion) { var mappingPath = options.WindowsMapping; if (File.Exists(mappingPath)) { return(CldrWindowsZonesParser.Parse(mappingPath)); } if (!Directory.Exists(mappingPath)) { throw new Exception($"{mappingPath} does not exist as either a file or a directory"); } var xmlFiles = Directory.GetFiles(mappingPath, "*.xml"); if (xmlFiles.Length == 0) { throw new Exception($"{mappingPath} does not contain any XML files"); } var allFiles = xmlFiles .Select(file => CldrWindowsZonesParser.Parse(file)) .OrderByDescending(zones => zones.TzdbVersion) .ToList(); var versions = string.Join(", ", allFiles.Select(z => z.TzdbVersion).ToArray()); var bestFile = allFiles .Where(zones => StringComparer.Ordinal.Compare(zones.TzdbVersion, targetTzdbVersion) <= 0) .FirstOrDefault(); if (bestFile == null) { throw new Exception($"No zones files suitable for version {targetTzdbVersion}. Found versions targeting: [{versions}]"); } Console.WriteLine($"Picked Windows Zones with TZDB version {bestFile.TzdbVersion} out of [{versions}] as best match for {targetTzdbVersion}"); return(bestFile); }