Something went wrong. Try again.
Dates, times and timezones for Zig 0.17: IANA tzdata, ISO 8601, RFC 822, strftime, CLDR, Go, .NET and git date formats
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// 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, "<mapTimezones", "otherVersion") orelse return error.MissingOtherVersion; const type_version = attribute(xml, "<mapTimezones", "typeVersion") orelse return error.MissingTypeVersion;
const Pair = struct { windows: []const u8, iana: []const u8,
/// Orders by the Windows name, which is what the table is /// searched by. fn lessThan(_: void, a: @This(), b: @This()) bool { return std.mem.lessThan(u8, a.windows, b.windows); } };
var pairs: std.ArrayList(Pair) = .empty;
var rest = xml; while (std.mem.find(u8, rest, "<mapZone")) |at| { rest = rest[at..]; const end = std.mem.findScalar(u8, rest, '>') orelse return error.UnterminatedElement; const element = rest[0 .. end + 1]; rest = rest[end + 1 ..];
const territory = attribute(element, "<mapZone", "territory") orelse return error.MissingTerritory; // Everything else is a country's answer to the same question. if (!std.mem.eql(u8, territory, "001")) continue;
const windows = attribute(element, "<mapZone", "other") orelse return error.MissingOther; const iana = attribute(element, "<mapZone", "type") orelse return error.MissingType;
// A territory row may list several zones separated by spaces, but // the `001` row names exactly one. Anything else means the file // has changed shape and the table below would be a guess. if (std.mem.findScalar(u8, iana, ' ') != null) return error.AmbiguousDefault;
try pairs.append(arena, .{ .windows = windows, .iana = iana }); }
if (pairs.items.len == 0) return error.NoMappings;
std.mem.sort(Pair, pairs.items, {}, Pair.lessThan);
// A Windows name appearing twice would make the search below depend // on which copy it landed on. for (pairs.items[1..], pairs.items[0 .. pairs.items.len - 1]) |pair, previous| { if (std.mem.eql(u8, pair.windows, previous.windows)) return error.DuplicateWindowsName; }
var source: std.ArrayList(u8) = .empty;
try source.print(arena, \\// SPDX-FileCopyrightText: © 1991-2026 Unicode, Inc. \\// SPDX-License-Identifier: Unicode-3.0 \\ \\//! Generated by tools/gen_windowszones.zig from CLDR's \\//! windowsZones.xml. Do not edit. \\//! \\//! Windows names its own timezones rather than using the IANA \\//! ones, and CLDR publishes the correspondence between the two. \\//! This is the world-wide default half of it: one IANA name per \\//! Windows name, which is what `tzdb.windows` needs to turn the \\//! answer from `GetDynamicTimeZoneInformation` into a zone this \\//! library can load. \\ \\/// The CLDR release this table was generated from. \\pub const cldr_version = "{f}"; \\ \\/// The Windows zone list CLDR mapped, as the file records it. \\pub const other_version = "{f}"; \\ \\/// The IANA release CLDR mapped to, as the file records it. \\/// The names on the IANA side are the ones current in that \\/// release, so a few of them are links by now rather than zones. \\pub const type_version = "{f}"; \\ \\/// One Windows zone name and the IANA zone CLDR calls its \\/// world-wide default. \\pub const Entry = struct {{ \\ windows: []const u8, \\ iana: []const u8, \\}}; \\ \\/// The mapping, sorted by Windows name so that it can be \\/// binary searched. \\pub const entries = [_]Entry{{ \\ , .{ std.zig.fmtString(cldr_release), std.zig.fmtString(other_version), std.zig.fmtString(type_version), });
for (pairs.items) |pair| { try source.print(arena, " .{{ .windows = \"{f}\", .iana = \"{f}\" }},\n", .{ std.zig.fmtString(pair.windows), std.zig.fmtString(pair.iana), }); } try source.appendSlice(arena, "};\n");
try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = output_path, .data = source.items });}
/// Returns the value of `name` in the first `element` in `xml`, or null/// when there is no such element or it carries no such attribute.////// This is not an XML parser and does not try to be one: `windowsZones.xml`/// is generated, every attribute in it is a quoted literal with no/// entities or single quotes, and a parser would be more code than the/// table it produces. Anything unexpected shows up as a missing attribute,/// which the caller turns into a failed build rather than a quiet gap.fn attribute(xml: []const u8, element: []const u8, name: []const u8) ?[]const u8 { const at = std.mem.find(u8, xml, element) orelse return null; const body = xml[at..]; const close = std.mem.findScalar(u8, body, '>') 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 = \\<mapTimezones otherVersion="7e11800" typeVersion="2021a"> \\<mapZone other="Dateline Standard Time" territory="001" type="Etc/GMT+12"/> ;
try std.testing.expectEqualStrings("2021a", attribute(xml, "<mapTimezones", "typeVersion").?); try std.testing.expectEqualStrings("001", attribute(xml, "<mapZone", "territory").?);
// An attribute of a later element is not one of this element's, which // is what the search stopping at `>` is for. try std.testing.expectEqual(@as(?[]const u8, null), attribute(xml, "<mapTimezones", "territory"));
// And an element or attribute that is not there at all. try std.testing.expectEqual(@as(?[]const u8, null), attribute(xml, "<mapNothing", "type")); try std.testing.expectEqual(@as(?[]const u8, null), attribute(xml, "<mapZone", "nothing"));}