// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Turns CLDR's `windowsZones.xml` into `src/windowszones.zig`, the table //! that gets from a Windows zone name such as "Central Standard Time" to //! the IANA name "America/Chicago". //! //! Windows does not use the IANA database. It has its own set of zone //! names, and the only published correspondence between the two is the //! one CLDR maintains. `GetDynamicTimeZoneInformation` reports a Windows //! name, so a library whose data is IANA's has to come through here. //! //! The file lists a mapping per territory, because a Windows zone often //! covers several IANA ones -- "Central Standard Time" is Chicago in the //! United States, Winnipeg in Canada and Mexico City in Mexico. Only the //! `001` rows are kept, which is CLDR's world-wide default and the one //! answer that does not need to know where the machine is. //! //! Unlike the timezone database, this table is checked into the tree //! rather than generated on every build, so that the Windows path needs //! no network and no extra dependency. See `zig build windowszones` for //! how to refresh it, and `src/windowszones.zig` for what came out. //! //! This runs on the build host. See `build.zig`. const std = @import("std"); /// Reads the `windowsZones.xml` at `argv[1]` and writes the Zig table to /// `argv[2]`, recording the CLDR release named by `argv[3]`. pub fn main(init: std.process.Init.Minimal) !void { var gpa_state: std.heap.DebugAllocator(.{}) = .init; defer _ = gpa_state.deinit(); const gpa = gpa_state.allocator(); var threaded: std.Io.Threaded = .init(gpa, .{}); defer threaded.deinit(); const io = threaded.io(); var arena_state: std.heap.ArenaAllocator = .init(gpa); defer arena_state.deinit(); const arena = arena_state.allocator(); const args = try init.args.toSlice(arena); if (args.len != 4) return error.WrongNumberOfArguments; const input_path = args[1]; const output_path = args[2]; const cldr_release = args[3]; const xml = try std.Io.Dir.cwd().readFileAlloc(io, input_path, arena, .limited(1 << 22)); // The two versions the file records for itself: which Windows zone // list it was written against, and which IANA release the names on // the other side come from. const other_version = attribute(xml, "') orelse return error.UnterminatedElement; const element = rest[0 .. end + 1]; rest = rest[end + 1 ..]; const territory = attribute(element, "') orelse return null; var buffer: [64]u8 = undefined; const needle = std.fmt.bufPrint(&buffer, " {s}=\"", .{name}) catch return null; const found = std.mem.find(u8, body[0..close], needle) orelse return null; const value = body[found + needle.len ..]; const end = std.mem.findScalar(u8, value, '"') orelse return null; return value[0..end]; } test attribute { const xml = \\ \\ ; try std.testing.expectEqualStrings("2021a", attribute(xml, "` is for. try std.testing.expectEqual(@as(?[]const u8, null), attribute(xml, "