// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Packs a tree of TZif files, as produced by `zic -d`, into the two files //! that give the library its embedded timezone database: `tzdata.bin`, //! holding every zone's bytes end to end, and `tzdata.zig`, an index of //! where each zone starts. //! //! This runs on the build host. See `build.zig`. const std = @import("std"); /// Walks the TZif tree at `argv[1]`, writing the concatenated zone bytes /// and the Zig index of them into the directory at `argv[2]`, recording /// the IANA release in `argv[3]` and the `zic -r` cutoff in `argv[4]`. /// /// Zones are sorted by name before they are written so that `tzdb` can /// binary search the index. 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 != 5) return error.WrongNumberOfArguments; const input_path = args[1]; const output_path = args[2]; const version = args[3]; // The zic -r argument the tree was built with, empty when the full // history was kept. Recorded so that callers can tell how far back // the data actually reaches. const history_from = args[4]; const Zone = struct { name: []const u8, bytes: []const u8, /// Orders zones by name, which is the order the index is written /// in and the order the lookup expects. fn lessThan(_: void, a: @This(), b: @This()) bool { return std.mem.lessThan(u8, a.name, b.name); } }; var input = try std.Io.Dir.cwd().openDir(io, input_path, .{ .iterate = true }); defer input.close(io); var zones: std.ArrayList(Zone) = .empty; var walker = try input.walk(arena); defer walker.deinit(); while (try walker.next(io)) |entry| { if (entry.kind != .file) continue; const bytes = try input.readFileAlloc(io, entry.path, arena, .limited(1 << 20)); try zones.append(arena, .{ .name = try arena.dupe(u8, entry.path), .bytes = bytes, }); } // The index is binary searched at run time, so it has to be sorted. std.mem.sort(Zone, zones.items, {}, Zone.lessThan); // The build system creates the output directory before running us. var output = try std.Io.Dir.cwd().openDir(io, output_path, .{}); defer output.close(io); var blob: std.ArrayList(u8) = .empty; var source: std.ArrayList(u8) = .empty; try source.appendSlice(arena, \\//! Generated by tools/tzpack.zig from the IANA timezone database. \\//! Do not edit. \\ \\ ); try source.print(arena, \\/// The IANA release this data was generated from. \\pub const version = "{f}"; \\ \\/// The `zic -r` argument the data was trimmed to, empty when the \\/// full history was kept. \\pub const history_from = "{f}"; \\ \\/// The TZif files, one after another. \\pub const blob: []const u8 = @embedFile("tzdata.bin"); \\ \\/// Where one zone's bytes sit inside `blob`. \\pub const Entry = struct {{ \\ name: []const u8, \\ start: u32, \\ len: u32, \\}}; \\ \\/// The zones in the database, sorted by name. \\pub const entries = [_]Entry{{ \\ , .{ std.zig.fmtString(version), std.zig.fmtString(history_from) }); for (zones.items) |zone| { try source.print(arena, " .{{ .name = \"{f}\", .start = {d}, .len = {d} }},\n", .{ std.zig.fmtString(zone.name), blob.items.len, zone.bytes.len, }); try blob.appendSlice(arena, zone.bytes); } try source.appendSlice(arena, "};\n"); try output.writeFile(io, .{ .sub_path = "tzdata.bin", .data = blob.items }); try output.writeFile(io, .{ .sub_path = "tzdata.zig", .data = source.items }); }