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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Formats a corpus of instants against a corpus of strftime format//! strings and writes the results out for `tools/oracle_strftime.c` to//! check against the C library's own `strftime` and `strptime`.//!//! The corpus lives here for the same reason the other oracles' do: a//! format string has to be comptime known for `strftime.format` to take//! it apart while this is compiled, so only this side can enumerate them.//!//! Output is one record per line, tab separated, of two kinds. An `F`//! record is a formatting result: the instant as milliseconds since the//! Unix epoch, the offset it is read at in minutes east of UTC, the zone//! name the reading carries, the format string, and what this library//! wrote. A `P` record is what came back when that text was read again//! under the same format string: how many bytes were consumed and the six//! fields a `struct tm` would carry, or `err` when it would not read.//!//! The zone name is carried because `%Z` writes it. A reading with no//! name is not in the corpus at all: `%Z` writes nothing for one, where//! glibc falls back to the running process's zone, and that difference is//! a property of a `struct tm` rather than something to check.
const std = @import("std");const Io = std.Io;
const datetime = @import("datetime");const DateTime = datetime.DateTime;const Designation = datetime.Designation;const Instant = datetime.Instant;const strftime = datetime.strftime;
/// The instants to format, as milliseconds since the Unix epoch.const instants = [_]i64{ 0, // the epoch, a Thursday 1710513005123, // an ordinary afternoon 1704240429000, // single digit month, day, hour, minute and second 1704240000000, // midnight, where the twelve hour clock reads 12 1710460800000, // noon, the other end of the meridiem 1709164800000, // the leap day 1735689599000, // the last second of a year 1704067200000, // New Year's Day, where the week counts start over -2208988800000, // 1900, well before the epoch 1136073600000, // a Sunday, where %u and %w disagree most 1483228800000, // 2017-01-01, a Sunday: ISO week 52 of the year before};
/// The offsets those instants are read at, in minutes east of UTC, paired/// with the name the zone goes by.const zones = [_]struct { minutes: i32, name: []const u8 }{ .{ .minutes = 0, .name = "UTC" }, .{ .minutes = -300, .name = "CDT" }, .{ .minutes = 345, .name = "+0545" }, .{ .minutes = 840, .name = "LINT" },};
/// Every conversion on its own, so that a disagreement names the/// conversion responsible rather than a combination.const conversions = [_][]const u8{ "%a", "%A", "%b", "%B", "%c", "%C", "%d", "%D", "%e", "%F", "%g", "%G", "%h", "%H", "%I", "%j", "%k", "%l", "%m", "%M", "%p", "%r", "%R", "%S", "%T", "%u", "%U", "%V", "%w", "%W", "%x", "%X", "%y", "%Y", "%z", "%Z", "%%", "%P",};
/// The flags and widths, over the conversions they say anything about.const decorated = [_][]const u8{ "%-d", "%_d", "%0d", "%-e", "%0e", "%_e", "%-m", "%_m", "%-H", "%_H", "%-k", "%0k", "%-I", "%_I", "%-l", "%0l", "%-j", "%_j", "%-y", "%_y", "%-Y", "%0Y", "%_Y", "%-C", "%-S", "%-M", "%-U", "%-W", "%-V", "%-G", "%-u", "%-w", "%4Y", "%6Y", "%10Y", "%3d", "%09d", "%5a", "%5p", "%5Z", "%^a", "%^A", "%^b", "%^B", "%^p", "%^Z", "%^c", "%#a", "%#A", "%#b", "%#B", "%#p", "%#Z", "%^P", "%#P", "%Ec", "%EY", "%Ey", "%EC", "%Ex", "%EX", "%Od", "%Oe", "%OH", "%OI", "%Om", "%OM", "%OS", "%Ou", "%OU", "%OV", "%Ow", "%OW", "%Oy", "%4C", "%_10Y", "%0j", "%2d",};
/// The shapes callers actually write.const shapes = [_][]const u8{ "%Y-%m-%d", "%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S%z", "%a, %d %b %Y %H:%M:%S %z", "%A, %B %-d, %Y", "%d/%m/%Y %I:%M %p", "%m/%d/%y", "%H:%M", "%j of %Y", "%G-W%V-%u", "%Y week %U (%W)", "%a %b %e %H:%M:%S %Z %Y", "%c", "%x %X", "%F %T%z", "%s",};
/// The corners of the tokenizer, where text around a conversion has to/// come through untouched.const corners = [_][]const u8{ "hello", "", "%%Y", "100%% of %Y", "[%Y]", "%n", "%t", "%Y%m%d", "%H%M%S", "%%%%", " %Y ", "%d.%m.%Y.",};
const formats = conversions ++ decorated ++ shapes ++ corners;
pub fn main(init: std.process.Init) !void { const io = init.io;
var buffer: [4096]u8 = undefined; var stdout: Io.File.Writer = .init(.stdout(), io, &buffer); const out = &stdout.interface;
var line: [256]u8 = undefined;
for (instants) |at| { for (zones) |zone| { const seconds = zone.minutes * 60; const shifted: Instant = .{ .timestamp = Instant.fromMilliTimestamp(at).timestamp + @as(i128, seconds) * std.time.ns_per_s, }; var value = shifted.asDateTime(); value.offset = seconds; value.designation = .from(zone.name);
inline for (formats) |current| { var writer = std.Io.Writer.fixed(&line); try strftime.format(value, current, &writer); const formatted = writer.buffered();
// The runtime formatter walks the same scanner and field // writer, so it has to agree with the comptime one // exactly; anything else is a bug in one of them. var again: [256]u8 = undefined; const runtime = try strftime.bufFormatRuntime(&again, value, current, .{}); if (!std.mem.eql(u8, formatted, runtime)) { std.debug.print("oracle: \"{s}\" is \"{s}\" at comptime and \"{s}\" at run time\n", .{ current, formatted, runtime }); std.process.exit(1); }
// The record separator would otherwise be ambiguous, and // `%n` and `%t` write both characters on purpose. if (std.mem.findAny(u8, current, "\t\n") == null and std.mem.findAny(u8, formatted, "\t\n") == null) { try out.print("F\t{d}\t{d}\t{s}\t{s}\t{s}\n", .{ at, zone.minutes, zone.name, current, formatted, });
// And read it straight back, which is the case that // has to work and which puts every conversion through // the parser without a second corpus to keep in step. if (strftime.parse(current, formatted)) |back| { try out.print("P\t{s}\t{s}\t{d}\t{d}\t{d}\t{d}\t{d}\t{d}\t{d}\n", .{ current, formatted, back.str.len, back.value.year, back.value.month.monthNumber(), back.value.day, back.value.hour, back.value.minute, back.value.second, }); } else |_| { try out.print("P\t{s}\t{s}\terr\t\t\t\t\t\t\n", .{ current, formatted }); } } } } }
try writeExhaustive(out); try out.flush();}
/// Every flag, width and modifier on every letter, and on a few that are/// not conversions at all, formatted at run time with unknown conversions/// copied through. That is the mode meant to answer the way glibc does/// for whatever a user types, so the whole space of short conversions is/// put to it rather than a list of the ones someone thought of.const exhaustive_letters = "aAbBcCdDeFgGhHIjklmMnNpPrRsStTuUVwWxXyYzZ%qQfiJKLvE:!";const exhaustive_decorations = [_][]const u8{ "", "-", "_", "0", "^", "#", "^#", "#^", "_^", "1", "3", "5", "-5", "_5", "05", "^5", "#5", "12", "-12", "_12", "012", "^12", "#12", "0-12", "-012", "_012", "^#12",};const exhaustive_modifiers = [_][]const u8{ "", "E", "O", ":", "::", "E:", ":E" };
/// Format strings that end inside a conversion, which glibc writes as/// they are and which the copying mode has to as well.const unfinished = [_][]const u8{ "%", "x%", "x%-", "x%5", "x%_3", "x%E", "x%O", "x%^", "x%:", "x%5::" };
fn writeExhaustive(out: *Io.Writer) !void { var format_buffer: [32]u8 = undefined; var line: [512]u8 = undefined;
// Two instants are enough here: one with every field single-digit // and one with every field wide, in the zones that differ. for ([_]i64{ 1704240429000, 1710513005123 }) |at| { for (zones) |zone| { const seconds = zone.minutes * 60; const shifted: Instant = .{ .timestamp = Instant.fromMilliTimestamp(at).timestamp + @as(i128, seconds) * std.time.ns_per_s, }; var value = shifted.asDateTime(); value.offset = seconds; value.designation = .from(zone.name);
for (exhaustive_decorations) |decoration| { for (exhaustive_modifiers) |modifier| { for (exhaustive_letters) |letter| { const current = try std.fmt.bufPrint(&format_buffer, "%{s}{s}{c}", .{ decoration, modifier, letter }); try writeRuntime(out, &line, value, at, zone.minutes, zone.name, current); } } } for (unfinished) |current| { try writeRuntime(out, &line, value, at, zone.minutes, zone.name, current); } } }}
fn writeRuntime( out: *Io.Writer, line: []u8, value: DateTime, at: i64, minutes: i32, name: []const u8, current: []const u8,) !void { const formatted = try strftime.bufFormatRuntime(line, value, current, .{ .unknown = .copy }); if (std.mem.findAny(u8, formatted, "\t\n") != null) return; try out.print("F\t{d}\t{d}\t{s}\t{s}\t{s}\n", .{ at, minutes, name, current, formatted });}