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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Formatting and parsing with the C library's conversion specifications,//! where the format string is the one everybody has already typed://! `%Y-%m-%d %H:%M:%S`.//!//! A `%` introduces a conversion, everything else is copied through, and//! `%%` is a literal percent sign. Between the `%` and the letter go the//! GNU flags `-`, `_`, `0`, `^` and `#`, then a minimum field width, then//! the `E` or `O` modifier that asks a locale for an alternative//! representation it does not have here://!//! ```zig//! try strftime.format(value, "%A, %-d %B %Y", writer); // Friday, 5 January 2024//! const back = try strftime.parseAll("%Y-%m-%d", "2024-03-15");//! ```//!//! This is the fourth vocabulary in the library, beside the moment.js//! sequences `DateTime.format` uses, Go's layouts in `golayout` and the//! CLDR patterns in `cldr`. It is here because a format string in a//! configuration file, a log template or a shell script is nearly always//! written this way, and translating one by hand is how a date ends up//! wrong.//!//! `format` and `parse` take the format string at comptime, so it is//! taken apart while this is compiled and what is left at runtime is a//! straight line of writes or reads. A conversion the module does not know//! is a compile error rather than text copied through, which is what the//! C library does with one: a `%Q` that silently writes itself is a typo//! nobody finds.//!//! `formatRuntime` takes a format string that was not known until the//! program ran -- a user's `--date=format:`, a line of a configuration//! file -- and either refuses an unknown conversion with an error or, if//! asked, copies it through the way glibc does. Both walk the same//! `Scanner` and the same field writer, so they cannot drift apart, and//! `writeChunk` is that field writer for a caller walking a `Scanner` of//! its own. `bufFormat` and `bufFormatRuntime` write into a slice.//!//! glibc is the specification, in its `C` locale, and//! `tools/oracle_strftime.c` checks it: both sides format the same corpus,//! read what they wrote back, and the results are diffed. Where this//! deliberately differs it is written down on the piece that differs, and//! the oracle carries the same list so that anything else is a failure.//! Writing, there are three://!//! - **`%s` honors the offset.** A `struct tm` reaches a count of//! seconds through `mktime`, so glibc reads the fields against the//! process's own timezone and ignores `tm_gmtoff`. A `DateTime` carries//! the offset that says which instant it names, and uses it.//! - **`%Z` writes nothing when the zone is not known**, which POSIX//! allows in so many words. glibc falls back to the running process's//! zone name, which would be a claim about where a reading was made//! that nothing here can support.//! - **`%N` and `%:z`, `%::z` and `%:::z` exist.** They are `date(1)`'s//! rather than `strftime(3)`'s, and glibc's `strftime` writes them out//! as text. Without them a nanosecond and an RFC 3339 offset could not//! be written at all.//!//! Everything else follows glibc, including where glibc is odd. The oracle//! puts every flag, every few widths and both modifiers to every letter,//! and so `%1d` is still two digits, `%-5d` pads with spaces, `%Ed` is//! not a conversion, and `%7z` counts its width twice.//!//! Reading, glibc's `strptime` is followed as far as it goes and then//! four things are kept that it throws away or refuses: `%G` with `%V`//! resolves an ISO week date, `%j` names a date without a year beside it,//! `%P` is read as well as written, and the flags, widths and `E`/`O`//! modifiers are ignored rather than refused, so that a format string//! which writes a date can read one back. `%s` reads a negative count//! too, which glibc writes and cannot read.
const std = @import("std");
const Date = @import("Date.zig");const DateTime = @import("DateTime.zig");const DayOfWeek = @import("dayofweek.zig").DayOfWeek;const Instant = @import("Instant.zig");const Month = @import("month.zig").Month;const Nanosecond = @import("nanosecond.zig").Nanosecond;const Year = @import("year.zig").Year;const locale = @import("locale.zig");const secondofday = @import("secondofday.zig");
/// Format strings worth having a name for, so that a caller reaching for/// a standard shape does not have to get the punctuation right twice.pub const pattern = struct { /// What `ctime(3)` and `%c` write: `Fri Mar 15 14:30:05 2024`. pub const ctime = "%a %b %e %H:%M:%S %Y"; /// What `date(1)` prints with no arguments. pub const date_command = "%a %b %e %H:%M:%S %Z %Y"; /// ISO 8601's calendar date on its own. pub const iso_date = "%Y-%m-%d"; /// ISO 8601's time of day on its own. pub const iso_time = "%H:%M:%S"; /// RFC 3339, which needs the colon in the offset and so needs the /// `date(1)` extension `%:z`. pub const rfc_3339 = "%Y-%m-%dT%H:%M:%S%:z"; /// The `Date:` header of a mail message, as RFC 5322 writes one. pub const rfc_5322 = "%a, %d %b %Y %H:%M:%S %z";};
/// One conversion, named for what it stands for rather than for its/// letter, with the letter beside it.pub const Conversion = enum { weekday_short, // %a weekday_long, // %A month_short, // %b, %h month_long, // %B date_and_time, // %c century, // %C day, // %d date_slashes, // %D, %x day_spaced, // %e date_dashes, // %F iso_year_short, // %g iso_year, // %G hour24, // %H hour12, // %I day_of_year, // %j hour24_spaced, // %k hour12_spaced, // %l month, // %m minute, // %M newline, // %n nanosecond, // %N, from date(1) meridiem_upper, // %p meridiem_lower, // %P time12, // %r hour_minute, // %R epoch_seconds, // %s second, // %S tab, // %t time24, // %T, %X iso_weekday, // %u week_from_sunday, // %U iso_week, // %V weekday_number, // %w week_from_monday, // %W year_short, // %y year, // %Y offset, // %z offset_colon, // %:z, from date(1) offset_colon_seconds, // %::z, from date(1) offset_minimal, // %:::z, from date(1) zone, // %Z percent, // %%};
/// What a flag asks for in place of a field's own padding.pub const Pad = enum { /// Whatever the field pads with by itself. default, /// `-`: do not pad at all, whatever width was asked for. none, /// `_`: pad with spaces. space, /// `0`: pad with zeros. zero,};
/// What a flag asks for in place of a field's own capitalization.pub const Case = enum { /// However the field writes itself. none, /// `^`: upper case. upper, /// `#`: the opposite case to the field's own, which is upper case for /// a name and lower case for `%p` and `%Z`. opposite,};
/// One conversion and the flags, width and modifier written with it.pub const Field = struct { which: Conversion, pad: Pad = .default, case: Case = .none, /// The minimum field width, or zero when none was asked for. It is a /// minimum: a value too wide for it is written in full. width: u8 = 0,};
/// A conversion this library does not know, kept so that it can be/// written out as text the way the C library writes one.////// Only a `Scanner` told `.unknown = .copy` yields these; the default/// refuses the format string instead.pub const Unknown = struct { /// The conversion as it was written, from its `%` through the byte /// that made it unknown, or through the end of the format string when /// that came first. text: []const u8, /// Whether a `0` flag was the last of the padding flags, which pads /// with zeros rather than spaces. A `-` pads with spaces here, as it /// does in glibc: there is no field of digits for it to unpad. zero: bool = false, /// Whether a `^` flag was written, which upper-cases the text. upper: bool = false, /// The minimum width, as for `Field.width`. width: u8 = 0,};
/// A format string is a run of these: text to copy through, a conversion/// to fill in, or a conversion this library does not know.pub const Chunk = union(enum) { literal: []const u8, conversion: Field, unknown: Unknown,};
/// What can be wrong with a format string.pub const FormatError = error{ /// The format string ended inside a conversion: a `%` with no letter /// after it, or with only flags, a width or a modifier. UnfinishedConversion, /// A letter that names no conversion, a modifier on a conversion that /// does not take that modifier, or colons before anything but `z`. UnknownConversion, /// A minimum field width wider than 255. WidthTooLarge,};
/// What a `Scanner` does with a conversion it does not know.pub const UnknownPolicy = enum { /// Return `error.UnknownConversion` or `error.UnfinishedConversion`. refuse, /// Yield a `Chunk.unknown`, which the formatter writes as the text of /// the conversion, padded and upper-cased as its flags ask. That is /// what glibc's `strftime` does, so a format string a user typed comes /// out the way their C library would have written it. copy,};
/// Which modifier, if any, came between the width and the letter.const Modifier = enum { none, E, O };
/// Walks a format string a chunk at a time.////// Written as a cursor rather than as a function returning a list so that/// the same code serves every entry point: `tokenize` runs it at comptime/// to build a list for `format` to unroll, and `formatRuntime` runs it as/// it writes. A caller with conversions of its own to handle -- `git log/// --date=format:` writes `%s` and `%z` itself, say -- can run it too,/// writing those chunks itself and passing the rest to `writeChunk`./// `format[start..index]`, with `start` the `index` before a call to/// `next`, is the text the returned chunk was read from.////// A conversion is `%`, then any number of the flags `-_0^#`, then an/// optional minimum field width, then an optional `E` or `O` modifier,/// then the conversion letter. Of `-`, `_` and `0` the last one written/// wins; `^` and `#` are independent, and when both are written `#` wins/// for `%p` and `%Z` and `^` for everything else, which is glibc's/// arithmetic. The `%:z` family is the one exception to that shape, and/// the colons come last because `date(1)` puts them there.////// `E` and `O` ask a locale for an alternative representation: era-based/// years, and the locale's own digits. No locale here has either, and/// POSIX says the plain conversion is used when the locale has no/// alternative, so they are read and dropped -- but only on the/// conversions glibc accepts them on. glibc copies `%Ed` through as text,/// and so this treats it as unknown.pub const Scanner = struct { format: []const u8, index: usize = 0, unknown: UnknownPolicy = .refuse,
/// Returns the next chunk, or null at the end of the format string. pub fn next(self: *Scanner) FormatError!?Chunk { const format_string = self.format; const start = self.index; if (start >= format_string.len) return null;
if (format_string[start] != '%') { const end = std.mem.findScalarPos(u8, format_string, start, '%') orelse format_string.len; self.index = end; return .{ .literal = format_string[start..end] }; }
var field: Field = .{ .which = .percent }; var upper = false; var opposite = false; var j = start + 1;
// The flags, in any order and as many as are written. glibc lets // the last padding flag win rather than refusing a contradiction, // so `%_0d` pads with zeros. flags: while (j < format_string.len) : (j += 1) { switch (format_string[j]) { '-' => field.pad = .none, '_' => field.pad = .space, '0' => field.pad = .zero, '^' => upper = true, '#' => opposite = true, else => break :flags, } }
// The width. The leading `0` of `%09d` has already been taken as // a flag above, which is what makes that mean a zero-padded width // of nine rather than a width of ninety. var width: usize = 0; while (j < format_string.len and std.ascii.isDigit(format_string[j])) : (j += 1) { width = width * 10 + (format_string[j] - '0'); if (width > std.math.maxInt(u8)) return error.WidthTooLarge; } field.width = @intCast(width);
var modifier: Modifier = .none; if (j < format_string.len) switch (format_string[j]) { 'E' => { modifier = .E; j += 1; }, 'O' => { modifier = .O; j += 1; }, else => {}, };
const first_colon = j; var colons: usize = 0; while (j < format_string.len and format_string[j] == ':') : (j += 1) colons += 1;
// glibc has no `%:z`, so to it the first colon is the letter that // made the conversion unknown, and what follows is text. Copying // the same span is what keeps `%5:q` padded the way glibc pads it. const unknown_end = if (colons > 0) first_colon + 1 else @min(j + 1, format_string.len); // `#` upper-cases `%Eb` and `%Eh`, which glibc refuses and copies // through, because it has already decided what `#` means for the // month name before it looks at the modifier. const month_name = j < format_string.len and (format_string[j] == 'b' or format_string[j] == 'h'); const unknown: Unknown = .{ .text = format_string[start..unknown_end], .zero = field.pad == .zero, .upper = upper or (opposite and modifier == .E and month_name), .width = field.width, };
if (j >= format_string.len and colons == 0) { if (self.unknown == .refuse) return error.UnfinishedConversion; self.index = format_string.len; return .{ .unknown = unknown }; }
const letter = if (j < format_string.len) format_string[j] else 0; const known = conversionOf(letter, colons); if (known == null or !modifierAllowed(modifier, letter) or (colons > 0 and modifier != .none)) { if (self.unknown == .refuse) return if (j >= format_string.len) error.UnfinishedConversion else error.UnknownConversion; self.index = unknown_end; return .{ .unknown = unknown }; } self.index = j + 1; const which = known.?;
field.which = which; field.case = if (upper and opposite) (if (oppositeOf(which) == .lower) .opposite else .upper) else if (opposite) .opposite else if (upper) .upper else .none; return .{ .conversion = field }; }};
test Scanner { var scanner: Scanner = .{ .format = "at %-5H!" }; try std.testing.expectEqualStrings("at ", (try scanner.next()).?.literal); const hour = (try scanner.next()).?.conversion; try std.testing.expectEqual(Conversion.hour24, hour.which); try std.testing.expectEqual(Pad.none, hour.pad); try std.testing.expectEqual(@as(u8, 5), hour.width); try std.testing.expectEqualStrings("!", (try scanner.next()).?.literal); try std.testing.expectEqual(@as(?Chunk, null), try scanner.next());
// Refused by default, the way `tokenize` refuses them at compile // time. var refusing: Scanner = .{ .format = "%Q" }; try std.testing.expectError(error.UnknownConversion, refusing.next()); var unfinished: Scanner = .{ .format = "100%" }; _ = try unfinished.next(); try std.testing.expectError(error.UnfinishedConversion, unfinished.next());
// Copied, the way glibc copies them: the text of the conversion, // with its flags and width kept for the writer to apply. var copying: Scanner = .{ .format = "%^5q%Ed", .unknown = .copy }; const q = (try copying.next()).?.unknown; try std.testing.expectEqualStrings("%^5q", q.text); try std.testing.expect(q.upper); try std.testing.expectEqual(@as(u8, 5), q.width); // `E` is not something `%d` takes. try std.testing.expectEqualStrings("%Ed", (try copying.next()).?.unknown.text);}
/// The conversion `letter` names after `colons` colons, or null when it/// names none.fn conversionOf(letter: u8, colons: usize) ?Conversion { if (colons > 0) return if (letter != 'z') null else switch (colons) { 1 => .offset_colon, 2 => .offset_colon_seconds, 3 => .offset_minimal, else => null, }; return switch (letter) { 'a' => .weekday_short, 'A' => .weekday_long, 'b', 'h' => .month_short, 'B' => .month_long, 'c' => .date_and_time, 'C' => .century, 'd' => .day, 'D', 'x' => .date_slashes, 'e' => .day_spaced, 'F' => .date_dashes, 'g' => .iso_year_short, 'G' => .iso_year, 'H' => .hour24, 'I' => .hour12, 'j' => .day_of_year, 'k' => .hour24_spaced, 'l' => .hour12_spaced, 'm' => .month, 'M' => .minute, 'n' => .newline, 'N' => .nanosecond, 'p' => .meridiem_upper, 'P' => .meridiem_lower, 'r' => .time12, 'R' => .hour_minute, 's' => .epoch_seconds, 'S' => .second, 't' => .tab, 'T', 'X' => .time24, 'u' => .iso_weekday, 'U' => .week_from_sunday, 'V' => .iso_week, 'w' => .weekday_number, 'W' => .week_from_monday, 'y' => .year_short, 'Y' => .year, 'z' => .offset, 'Z' => .zone, '%' => .percent, else => null, };}
/// Whether glibc accepts `modifier` on the conversion `letter` names.////// Found by asking glibc about every letter, since the C standard lists/// fewer than glibc takes and glibc is the specification here. It is the/// letter rather than the conversion that decides: `%Ex` is accepted and/// `%ED`, the same conversion, is not.fn modifierAllowed(modifier: Modifier, letter: u8) bool { const letters = switch (modifier) { .none => return true, .E => "cCnpPrRstTuxXyYzZ%", .O => "bBCdegGhHIjklmMnpPrRsStTuUVwWyzZ%", }; return std.mem.findScalar(u8, letters, letter) != null;}
/// Splits `format_string` into chunks at compile time, and refuses one/// that is not a format string.////// Anything that is not a conversion this module knows is a compile error,/// naming what was found. That is deliberately not what the C library/// does, which copies an unknown conversion through as text: a `%P`/// misremembered as `%p`, or a `%Q` that is a typo, is a bug that should/// be found while the program is compiled rather than read out of its/// output later. `formatRuntime` can be told to copy instead, for a format/// string a user typed.pub fn tokenize(comptime format_string: []const u8) []const Chunk { comptime { @setEvalBranchQuota(100000);
var chunks: []const Chunk = &.{}; var scanner: Scanner = .{ .format = format_string }; while (true) { const before = scanner.index; const chunk = scanner.next() catch |err| @compileError(switch (err) { error.UnfinishedConversion => "strftime: '" ++ format_string ++ "' ends in the middle of a conversion", error.UnknownConversion => "strftime: the conversion at the start of '" ++ format_string[before..] ++ "' in '" ++ format_string ++ "' is not one this library knows", error.WidthTooLarge => "strftime: field width in '" ++ format_string ++ "' is wider than 255", }) orelse break; chunks = chunks ++ &[_]Chunk{chunk}; } return chunks; }}
test tokenize { const date = comptime tokenize("%Y-%m-%d"); try std.testing.expectEqual(@as(usize, 5), date.len); try std.testing.expectEqual(Conversion.year, date[0].conversion.which); try std.testing.expectEqualStrings("-", date[1].literal); try std.testing.expectEqual(Conversion.month, date[2].conversion.which);
// Flags, then a width, then the conversion. const padded = comptime tokenize("%_10Y"); try std.testing.expectEqual(Pad.space, padded[0].conversion.pad); try std.testing.expectEqual(@as(u8, 10), padded[0].conversion.width);
// The leading zero of `%09d` is the flag, so nine is the width. const zeroed = comptime tokenize("%09d"); try std.testing.expectEqual(Pad.zero, zeroed[0].conversion.pad); try std.testing.expectEqual(@as(u8, 9), zeroed[0].conversion.width);
// `E` and `O` are read and dropped, so the conversion is the plain one. try std.testing.expectEqual(Conversion.day, (comptime tokenize("%Od"))[0].conversion.which);
// The colons belong to %z and count. try std.testing.expectEqual(Conversion.offset_colon, (comptime tokenize("%:z"))[0].conversion.which);
// Text with no conversion in it is one literal. const plain = comptime tokenize("hello"); try std.testing.expectEqual(@as(usize, 1), plain.len); try std.testing.expectEqualStrings("hello", plain[0].literal);}
/// The longest a single conversion writes before padding is applied./// `%c` in a language with long names is the widest thing here, and a/// field that overran would be a `Writer` failure rather than a silent/// truncation.const max_field = 256;
/// What a conversion expands to when it is a shorthand for several/// others, or null when it stands for a value of its own.////// These are the `C` locale's definitions, which is what glibc uses when/// nothing has set `LC_TIME`. A locale passed to `formatWith` changes the/// names inside them but not the arrangement, because a `Locale` here is/// moment.js's and carries no `d_t_fmt` to put in their place.fn expansion(which: Conversion) ?[]const u8 { return switch (which) { .date_and_time => "%a %b %e %H:%M:%S %Y", .date_slashes => "%m/%d/%y", .date_dashes => "%Y-%m-%d", .time12 => "%I:%M:%S %p", .hour_minute => "%H:%M", .time24 => "%H:%M:%S", else => null, };}
/// The chunks of `expansion(which)`, taken apart while this is compiled,/// so that a format string read at run time expands without scanning/// these again.fn expansionChunks(which: Conversion) ?[]const Chunk { return switch (which) { inline .date_and_time, .date_slashes, .date_dashes, .time12, .hour_minute, .time24 => |composite| comptime tokenize(expansion(composite).?), else => null, };}
/// How wide a conversion writes itself and what it pads with when no flag/// says otherwise.const Shape = struct { width: u8 = 0, pad: u8 = '0',};
/// The default shape of each conversion.////// The year-ish fields -- `%Y`, `%G`, `%C` and `%s` -- are the ones with/// no default width, so year 5 is `5` rather than `0005`. That is glibc's/// behavior rather than an oversight here: a width given explicitly/// still applies, so `%4Y` is `0005`.fn shapeOf(which: Conversion) Shape { return switch (which) { .day, .hour24, .hour12, .month, .minute, .second, .year_short, .iso_year_short, .week_from_sunday, .week_from_monday, .iso_week => .{ .width = 2, .pad = '0' }, .day_spaced, .hour24_spaced, .hour12_spaced => .{ .width = 2, .pad = ' ' }, .day_of_year => .{ .width = 3, .pad = '0' }, .iso_weekday, .weekday_number => .{ .width = 1, .pad = '0' }, // A name widened to a column is padded with spaces, because a // zero in front of a word is not a word. The same goes for the // composite conversions, which begin with a word or a digit // already padded, and for the three that are text. .weekday_short, .weekday_long, .month_short, .month_long, .meridiem_upper, .meridiem_lower, .zone => .{ .width = 0, .pad = ' ' }, .date_and_time, .date_slashes, .date_dashes, .time12, .hour_minute, .time24 => .{ .width = 0, .pad = ' ' }, .newline, .tab, .percent => .{ .width = 0, .pad = ' ' }, // glibc pads a count of seconds with spaces, unlike the year. .epoch_seconds => .{ .width = 0, .pad = ' ' }, else => .{ .width = 0, .pad = '0' }, };}
/// What to do to the bytes a conversion produced on the way out.const Transform = enum { none, upper, lower };
/// Which case the `#` flag asks for, which is the opposite of the case/// the conversion writes by itself.fn oppositeOf(which: Conversion) Transform { return switch (which) { .weekday_short, .weekday_long, .month_short, .month_long => .upper, .meridiem_upper, .zone => .lower, else => .none, };}
/// The case transform a field asks for.////// `%P` is always lower case, whatever flag is written with it, because/// lower case is the whole of what distinguishes it from `%p`. The/// composite conversions take no case from a flag either, with `%c` the/// single exception; both of those are glibc's behavior, checked by the/// oracle.fn transformOf(field: Field) Transform { if (field.which == .meridiem_lower) return .none; if (expansion(field.which) != null and field.which != .date_and_time) return .none;
return switch (field.case) { .none => .none, .upper => .upper, .opposite => oppositeOf(field.which), };}
/// Writes `value` under `format_string` in the `en` locale, which is the/// `C` locale's names.////// Flushes `writer` before returning, as `DateTime.format` does./// `bufFormat` writes into a slice instead, and `writeChunk` writes/// without flushing.////// ```zig/// try strftime.format(value, "%Y-%m-%dT%H:%M:%S%:z", writer);/// ```pub fn format( value: DateTime, comptime format_string: []const u8, writer: *std.Io.Writer,) !void { return formatWith(value, format_string, locale.en, writer);}
test format { var value: DateTime = .{ .year = 2024, .month = .Mar, .day = 15, .hour = 14, .minute = 30, .second = 5, .nanosecond = 123456789, .offset = -5 * std.time.s_per_hour, .designation = .from("CDT"), }; value.updateDayOfWeek();
var buffer: [64]u8 = undefined; try std.testing.expectEqualStrings("2024-03-15", try bufFormat(&buffer, value, "%Y-%m-%d")); try std.testing.expectEqualStrings("14:30:05", try bufFormat(&buffer, value, "%T")); try std.testing.expectEqualStrings( "Fri Mar 15 14:30:05 2024", try bufFormat(&buffer, value, "%c"), ); try std.testing.expectEqualStrings( "2024-03-15T14:30:05-05:00", try bufFormat(&buffer, value, pattern.rfc_3339), );
// The flags: `-` drops the padding, `_` pads with spaces, `^` shouts. try std.testing.expectEqualStrings("3", try bufFormat(&buffer, value, "%-m")); try std.testing.expectEqualStrings(" 3", try bufFormat(&buffer, value, "%_m")); try std.testing.expectEqualStrings("MARCH", try bufFormat(&buffer, value, "%^B")); try std.testing.expectEqualStrings("pm", try bufFormat(&buffer, value, "%#p"));
// A width written beside `-` still pads, with spaces, as glibc does: // the flag drops the field's own padding and not the one asked for. try std.testing.expectEqualStrings(" 3", try bufFormat(&buffer, value, "%-5m"));}
/// Writes `value` under `format_string`, taking the month names, day/// names and meridiem from `in`.////// The format string stays comptime because it decides which code runs;/// the locale does not, because it only decides which bytes come out, so/// it can be chosen at run time from a header or a configuration file./// This is the same split `DateTime.formatWith` makes.////// Only the names follow the locale. The arrangements behind `%c`, `%x`/// and `%X` stay the `C` locale's, because a `locale.Locale` is/// moment.js's and has no `d_t_fmt`, `d_fmt` or `t_fmt` to put in their/// place; `cldr.formatDateTime` is the entry point that does know how a/// language arranges a date.////// ```zig/// try strftime.formatWith(value, "%A %-d %B", locale.byName("fr").?, writer);/// ```pub fn formatWith( value: DateTime, comptime format_string: []const u8, in: locale.Locale, writer: *std.Io.Writer,) !void { try writeFormat(value, format_string, in, writer); try writer.flush();}
test formatWith { var value: DateTime = .{ .year = 2024, .month = .Mar, .day = 15 }; value.updateDayOfWeek();
var buffer: [64]u8 = undefined; var writer = std.Io.Writer.fixed(&buffer); try formatWith(value, "%A %-d %B %Y", locale.en, &writer); try std.testing.expectEqualStrings("Friday 15 March 2024", writer.buffered());}
/// Writes `value` under `format_string` into `buffer`, and returns the/// part of `buffer` that was written.////// Returns `error.WriteFailed` when `buffer` is too small for the result./// glibc's `strftime` refuses the same way, returning zero rather than/// writing part of a date.////// ```zig/// var buffer: [64]u8 = undefined;/// const text = try strftime.bufFormat(&buffer, value, "%F %T");/// ```pub fn bufFormat(buffer: []u8, value: DateTime, comptime format_string: []const u8) std.Io.Writer.Error![]u8 { var writer = std.Io.Writer.fixed(buffer); try writeFormat(value, format_string, locale.en, &writer); return writer.buffered();}
test bufFormat { const value: DateTime = .{ .year = 2024, .month = .Mar, .day = 15 };
var buffer: [16]u8 = undefined; try std.testing.expectEqualStrings("2024-03-15", try bufFormat(&buffer, value, "%F"));
var small: [4]u8 = undefined; try std.testing.expectError(error.WriteFailed, bufFormat(&small, value, "%F"));}
/// Formats into `writer` without flushing it, which is what the composite/// conversions and the public entry points are both built out of.fn writeFormat( value: DateTime, comptime format_string: []const u8, in: locale.Locale, writer: *std.Io.Writer,) std.Io.Writer.Error!void { const chunks = comptime tokenize(format_string); inline for (chunks) |chunk| try writeChunk(value, chunk, in, writer);}
/// What `formatRuntime` takes beside the format string.pub const RuntimeOptions = struct { /// Where the month names, day names and meridiem come from. in: locale.Locale = locale.en, /// What to do with a conversion this library does not know: refuse /// the format string, or copy the conversion through as text the way /// glibc does. unknown: UnknownPolicy = .refuse,};
/// Writes `value` under a format string that was not known until now: one/// read from a configuration file, or typed by a user.////// The whole format string is checked before anything is written, so a/// refused one leaves `writer` as it was. With `.unknown = .copy` nothing/// is refused but a width wider than 255, and a conversion this library/// does not know comes out as its own text, padded and cased as its flags/// ask, which is what glibc's `strftime` writes for one.////// Unlike `format`, this does not flush `writer`. A caller writing a date/// into a line of other output decides when that line goes out.////// ```zig/// try strftime.formatRuntime(value, user_format, .{ .unknown = .copy }, writer);/// ```pub fn formatRuntime( value: DateTime, format_string: []const u8, options: RuntimeOptions, writer: *std.Io.Writer,) (FormatError || std.Io.Writer.Error)!void { var checking: Scanner = .{ .format = format_string, .unknown = options.unknown }; while (try checking.next()) |_| {}
var scanner: Scanner = .{ .format = format_string, .unknown = options.unknown }; while (scanner.next() catch unreachable) |chunk| try writeChunk(value, chunk, options.in, writer);}
test formatRuntime { var value: DateTime = .{ .year = 2024, .month = .Mar, .day = 5, .hour = 14 }; value.updateDayOfWeek();
var buffer: [64]u8 = undefined; var written = std.Io.Writer.fixed(&buffer); try formatRuntime(value, "%a %_5d|%-H", .{}, &written); try std.testing.expectEqualStrings("Tue 5|14", written.buffered());
// The same mistakes `format` refuses to compile come back as errors, // and nothing is written before they are found. var refused = std.Io.Writer.fixed(&buffer); try std.testing.expectError(error.UnknownConversion, formatRuntime(value, "%Y %Q", .{}, &refused)); try std.testing.expectEqual(@as(usize, 0), refused.buffered().len);
// Or they are copied through, the way glibc copies them. var copied = std.Io.Writer.fixed(&buffer); try formatRuntime(value, "%Y %5Q %Ed 100%", .{ .unknown = .copy }, &copied); try std.testing.expectEqualStrings("2024 %5Q %Ed 100%", copied.buffered());}
/// `formatRuntime` into `buffer`, returning the part of `buffer` that was/// written. A buffer too small for the result is `error.WriteFailed`.pub fn bufFormatRuntime( buffer: []u8, value: DateTime, format_string: []const u8, options: RuntimeOptions,) (FormatError || std.Io.Writer.Error)![]u8 { var writer = std.Io.Writer.fixed(buffer); try formatRuntime(value, format_string, options, &writer); return writer.buffered();}
test bufFormatRuntime { const value: DateTime = .{ .year = 2024, .month = .Mar, .day = 15 }; var buffer: [16]u8 = undefined; try std.testing.expectEqualStrings("2024-03-15", try bufFormatRuntime(&buffer, value, "%F", .{}));}
/// Writes one chunk of a format string, without flushing `writer`.////// The piece every formatter here is built from, public for a caller/// that walks a `Scanner` itself so as to write some conversions its own/// way and leave the rest to this.pub fn writeChunk( value: DateTime, chunk: Chunk, in: locale.Locale, writer: *std.Io.Writer,) std.Io.Writer.Error!void { switch (chunk) { .literal => |text| try writer.writeAll(text), .conversion => |field| try writeConversion(value, field, in, writer), .unknown => |unknown| try emit( writer, unknown.text, unknown.width, if (unknown.zero) '0' else ' ', if (unknown.upper) .upper else .none, false, ), }}
test writeChunk { const value: DateTime = .{ .year = 2024, .month = .Mar, .day = 15 };
// Handling `%s` in a format string by hand and the rest here. var buffer: [64]u8 = undefined; var writer = std.Io.Writer.fixed(&buffer); var scanner: Scanner = .{ .format = "%F %s" }; while (try scanner.next()) |chunk| switch (chunk) { .conversion => |field| if (field.which == .epoch_seconds) try writer.writeAll("(seconds)") else try writeChunk(value, chunk, locale.en, &writer), else => try writeChunk(value, chunk, locale.en, &writer), }; try std.testing.expectEqualStrings("2024-03-15 (seconds)", writer.buffered());}
/// Writes one conversion, padded and cased the way its flags asked for.////// Everything is written into a buffer first rather than straight out,/// because a minimum field width and a case change are both properties of/// the whole field and neither can be applied until its length is known.fn writeConversion( value: DateTime, field: Field, in: locale.Locale, writer: *std.Io.Writer,) std.Io.Writer.Error!void { // A fraction's width is a number of digits rather than a minimum, // because that is what `date(1)` means by `%3N`, so it does its own // writing. if (field.which == .nanosecond) { return writeFraction(writer, value.nanosecond, if (field.width == 0) 9 else field.width); } if (field.which == .offset and (field.width != 0 or field.pad != .default)) { return writePaddedOffset(writer, value.offset, field); }
var buffer: [max_field]u8 = undefined; var field_writer = std.Io.Writer.fixed(&buffer); const into = &field_writer;
const date = value.asDate();
switch (field.which) { .newline => try into.writeAll("\n"), .tab => try into.writeAll("\t"), .percent => try into.writeAll("%"),
.weekday_short => try into.writeAll(in.weekdayName(value.weekday, .ddd)), .weekday_long => try into.writeAll(in.weekdayName(value.weekday, .dddd)), .month_short => try into.writeAll(in.monthName(value.month, .MMM, false)), .month_long => try into.writeAll(in.monthName(value.month, .MMMM, false)),
.century => try into.print("{d}", .{@divFloor(value.year, 100)}), .day, .day_spaced => try into.print("{d}", .{value.day}), .day_of_year => try into.print("{d}", .{value.dayOfYear()}), .hour24, .hour24_spaced => try into.print("{d}", .{value.hour}), .hour12, .hour12_spaced => try into.print("{d}", .{twelve(value.hour)}), .month => try into.print("{d}", .{value.month.monthNumber()}), .minute => try into.print("{d}", .{value.minute}), .second => try into.print("{d}", .{value.second}),
// The last two digits of the year, counted so that the year before // year 1 is 99 rather than 1. That is glibc's arithmetic, and it // differs from the `YY` sequence and Go's `06`, both of which take // the magnitude first and write 1. .year_short => try into.print("{d}", .{@mod(value.year, 100)}), .year => try into.print("{d}", .{value.year}), .iso_year => try into.print("{d}", .{date.isoWeek().year}), .iso_year_short => try into.print("{d}", .{@mod(date.isoWeek().year, 100)}), .iso_week => try into.print("{d}", .{date.isoWeek().week}), .iso_weekday => try into.print("{d}", .{value.weekday.isoWeekdayNumber()}), .weekday_number => try into.print("{d}", .{value.weekday.weekdayNumber()}), .week_from_sunday => try into.print("{d}", .{weekFrom(value, .Sun)}), .week_from_monday => try into.print("{d}", .{weekFrom(value, .Mon)}),
.meridiem_upper => try in.writeMeridiem(into, value.hour, value.minute, .upper), .meridiem_lower => try in.writeMeridiem(into, value.hour, value.minute, .lower),
// The instant this reading names, which honors the offset. glibc // cannot: a `struct tm` reaches it through `mktime`, so its `%s` // reads the fields against the process's own timezone and ignores // `tm_gmtoff` entirely. The oracle compares this one only at a // zero offset, where the two questions are the same question. .epoch_seconds => try into.print("{d}", .{@divFloor(value.toInstant().timestamp, std.time.ns_per_s)}),
.offset => try writeOffset(into, value.offset, .{}), .offset_colon => try writeOffset(into, value.offset, .{ .colons = true }), .offset_colon_seconds => try writeOffset(into, value.offset, .{ .colons = true, .seconds = true }), .offset_minimal => try writeOffset(into, value.offset, .{ .colons = true, .minimal = true }),
// What the zone calls itself, and nothing at all when that is not // known, which POSIX allows in so many words. glibc writes the // running process's zone name instead, because a `struct tm` that // carries no `tm_zone` still has `tzname` behind it; there is no // equivalent here, and inventing one would be a lie about where a // reading was made. .zone => try into.writeAll(value.designation.slice()),
.date_and_time, .date_slashes, .date_dashes, .time12, .hour_minute, .time24 => { for (expansionChunks(field.which).?) |chunk| try writeChunk(value, chunk, in, into); },
.nanosecond => unreachable, }
// `-` drops the field's own padding, but a width written beside it // still pads, with spaces: glibc writes `%-5d` of the 3rd as ` 3`. // Any other width can widen a field and never narrow it, so `%1d` is // still two digits. const shape = shapeOf(field.which); const width: usize = if (field.pad == .none) field.width else @max(field.width, shape.width); const pad: u8 = switch (field.pad) { .default => shape.pad, .none, .space => ' ', .zero => '0', };
try emit(writer, field_writer.buffered(), width, pad, transformOf(field), isNumber(field.which));}
/// Whether `which` writes a number, whose sign a zero pad goes after./// `%Z` can begin with a sign too, `+0545` for a zone with no name, and/// glibc pads that in front like any other text.fn isNumber(which: Conversion) bool { return switch (which) { .century, .year, .iso_year, .epoch_seconds => true, else => false, };}
/// Writes `%z` under a flag or a width, the way glibc does.////// glibc writes `%z` as a sign followed by the hours and minutes read as/// one number, `-0500` being -500 written four digits wide, and applies a/// flag or width to that number. It also pads in front of the sign, by one/// less than the width, so the width counts twice: `%7z` at -5 hours is/// six spaces, then `-0000500`. That is a quirk rather than a design, and/// it is followed anyway because a format string written for glibc should/// come out the way glibc wrote it; nobody writes `%7z` on purpose.////// Precisely, with `W` the width: in front, `W - 1` zeros under the `0`/// flag and spaces otherwise; then the sign; then the number, which `-`/// pads with spaces to `W` and the other flags pad to at least four,/// `_` with spaces and the rest with zeros. Seconds of the offset are/// dropped, as they are from a plain `%z`.fn writePaddedOffset(writer: *std.Io.Writer, offset: i32, field: Field) std.Io.Writer.Error!void { const parts = secondofday.splitHours(@abs(offset)); const number = @as(u32, parts.hours) * 100 + parts.minute; const width: usize = field.width;
if (width > 1) try writer.splatByteAll(if (field.pad == .zero) '0' else ' ', width - 1); try writer.writeByte(if (offset < 0) '-' else '+');
var digits: [16]u8 = undefined; const text = std.fmt.bufPrint(&digits, "{d}", .{number}) catch unreachable; const columns: usize = if (field.pad == .none) width else @max(width, 4); if (text.len < columns) try writer.splatByteAll(if (field.pad == .zero or field.pad == .default) '0' else ' ', columns - text.len); try writer.writeAll(text);}
test writePaddedOffset { var buffer: [32]u8 = undefined; const minus_five = -5 * std.time.s_per_hour;
var plain = std.Io.Writer.fixed(&buffer); try writePaddedOffset(&plain, minus_five, .{ .which = .offset, .pad = .none }); try std.testing.expectEqualStrings("-500", plain.buffered());
var spaced = std.Io.Writer.fixed(&buffer); try writePaddedOffset(&spaced, minus_five, .{ .which = .offset, .pad = .space }); try std.testing.expectEqualStrings("- 500", spaced.buffered());
// The width counts twice, once in front of the sign and once after. var wide = std.Io.Writer.fixed(&buffer); try writePaddedOffset(&wide, minus_five, .{ .which = .offset, .width = 7 }); try std.testing.expectEqualStrings(" -0000500", wide.buffered());}
/// Writes `bytes` padded out to `width` and cased as `transform` says.////// When `bytes` is a number, a zero pad goes after the sign rather than/// before it, so a year of -1 at a width of six is `-00001` and not/// `00000-1`. A space pad goes before it, which is what lines a column up.fn emit( writer: *std.Io.Writer, bytes: []const u8, width: usize, pad: u8, transform: Transform, number: bool,) std.Io.Writer.Error!void { var rest = bytes;
if (number and pad == '0' and rest.len > 0 and (rest[0] == '-' or rest[0] == '+')) { try writer.writeByte(rest[0]); rest = rest[1..]; }
if (bytes.len < width) try writer.splatByteAll(pad, width - bytes.len);
switch (transform) { .none => try writer.writeAll(rest), .upper => for (rest) |char| try writer.writeByte(std.ascii.toUpper(char)), .lower => for (rest) |char| try writer.writeByte(std.ascii.toLower(char)), }}
test emit { var buffer: [16]u8 = undefined;
var padded = std.Io.Writer.fixed(&buffer); try emit(&padded, "5", 3, '0', .none, true); try std.testing.expectEqualStrings("005", padded.buffered());
// The sign keeps its place in front of the zeros. var negative = std.Io.Writer.fixed(&buffer); try emit(&negative, "-1", 6, '0', .none, true); try std.testing.expectEqualStrings("-00001", negative.buffered());
// A value wider than the width is written in full: a width is a // minimum and not a truncation. var wide = std.Io.Writer.fixed(&buffer); try emit(&wide, "2024", 2, '0', .none, true); try std.testing.expectEqualStrings("2024", wide.buffered());
var shouted = std.Io.Writer.fixed(&buffer); try emit(&shouted, "Mar", 0, ' ', .upper, false); try std.testing.expectEqualStrings("MAR", shouted.buffered());}
/// The hour on a twelve hour clock, where midnight and noon are both 12.fn twelve(hour: u5) u5 { const wrapped = hour % 12; return if (wrapped == 0) 12 else wrapped;}
test twelve { try std.testing.expectEqual(@as(u5, 12), twelve(0)); try std.testing.expectEqual(@as(u5, 1), twelve(1)); try std.testing.expectEqual(@as(u5, 12), twelve(12)); try std.testing.expectEqual(@as(u5, 11), twelve(23));}
/// The week of the year that `%U` and `%W` count, where week 1 begins on/// the first `starts_on` of the year and the days before it are week 0.////// This is not `Date.weekOfYear`, and the difference matters. That/// function implements a week rule with a week-numbering year, where the/// first days of January can belong to the last week of the year before./// `%U` and `%W` have no such thing: they are the plain arithmetic/// `(day_of_year + 7 - weekday) / 7` that C has always used, they never/// mention another year, and they answer 0 for the days before the year's/// first Sunday or Monday. A date written with `%Y-%W` and read back/// under a week-numbering rule would land in the wrong year.fn weekFrom(value: DateTime, starts_on: DayOfWeek) u8 { const day_of_year: i16 = @as(i16, value.dayOfYear()) - 1; const weekday: i16 = @mod( @as(i16, value.weekday.weekdayNumber()) - @as(i16, starts_on.weekdayNumber()), 7, ); return @intCast(@divTrunc(day_of_year + 7 - weekday, 7));}
test weekFrom { // 1970 opened on a Thursday, so January 1st is in week 0 under both // rules: neither the first Sunday nor the first Monday has happened. var epoch: DateTime = .{ .year = 1970, .month = .Jan, .day = 1 }; epoch.updateDayOfWeek(); try std.testing.expectEqual(@as(u8, 0), weekFrom(epoch, .Sun)); try std.testing.expectEqual(@as(u8, 0), weekFrom(epoch, .Mon));
// 2024-03-15 is a Friday, day 75 of a leap year. var march: DateTime = .{ .year = 2024, .month = .Mar, .day = 15 }; march.updateDayOfWeek(); try std.testing.expectEqual(@as(u8, 10), weekFrom(march, .Sun)); try std.testing.expectEqual(@as(u8, 11), weekFrom(march, .Mon));}
/// How much of an offset to write and with what between the parts.const OffsetShape = struct { colons: bool = false, seconds: bool = false, /// `%:::z`: write only as many parts as are needed, so a whole number /// of hours is `-05` and nothing more. minimal: bool = false,};
/// Writes an offset as a sign and then hours, minutes and maybe seconds.////// Always signed, and always at least two digits of hours, which is what/// every one of these forms requires: an offset written `-5` is not one/// anything will read back.fn writeOffset(writer: *std.Io.Writer, offset: i32, shape: OffsetShape) !void { try writer.writeAll(if (offset < 0) "-" else "+");
const parts = secondofday.splitHours(@abs(offset)); const hours = parts.hours; const minutes = parts.minute; const seconds = parts.second;
try writer.print("{d:0>2}", .{hours}); if (shape.minimal and minutes == 0 and seconds == 0) return;
if (shape.colons) try writer.writeAll(":"); try writer.print("{d:0>2}", .{minutes}); if (shape.minimal and seconds == 0) return; if (!shape.seconds and !shape.minimal) return;
if (shape.colons) try writer.writeAll(":"); try writer.print("{d:0>2}", .{seconds});}
test writeOffset { var buffer: [16]u8 = undefined;
var plain = std.Io.Writer.fixed(&buffer); try writeOffset(&plain, -5 * std.time.s_per_hour, .{}); try std.testing.expectEqualStrings("-0500", plain.buffered());
var colons = std.Io.Writer.fixed(&buffer); try writeOffset(&colons, 5 * std.time.s_per_hour + 45 * std.time.s_per_min, .{ .colons = true }); try std.testing.expectEqualStrings("+05:45", colons.buffered());
// A zone whose offset is not a whole minute needs the seconds, which // is what the third form is for: Chicago's local mean time was // -5:50:36 before the railways. var full = std.Io.Writer.fixed(&buffer); try writeOffset(&full, -(5 * std.time.s_per_hour + 50 * std.time.s_per_min + 36), .{ .colons = true, .seconds = true }); try std.testing.expectEqualStrings("-05:50:36", full.buffered());
// The minimal form stops as soon as nothing is left to say. var minimal = std.Io.Writer.fixed(&buffer); try writeOffset(&minimal, -5 * std.time.s_per_hour, .{ .colons = true, .minimal = true }); try std.testing.expectEqualStrings("-05", minimal.buffered());}
/// Writes `digits` digits of a fractional second, without a separator in/// front of them.////// Truncating rather than rounding, and the leading zeros kept, so that/// `%3N` of 1,500,000 nanoseconds is `001` -- a millisecond and a half/// read as one millisecond. That is `date(1)`'s behavior, and it is the/// only reading that makes `%S.%3N` a time rather than an approximation.fn writeFraction(writer: *std.Io.Writer, nanosecond: Nanosecond, digits: u8) !void { var buffer: [9]u8 = undefined; _ = std.fmt.printInt(&buffer, nanosecond, 10, .lower, .{ .width = 9, .fill = '0' });
if (digits <= 9) return writer.writeAll(buffer[0..digits]);
// More digits than a nanosecond has: the rest are zeros, because the // value has no more precision to give. try writer.writeAll(&buffer); try writer.splatByteAll('0', digits - 9);}
test writeFraction { var buffer: [16]u8 = undefined;
var full = std.Io.Writer.fixed(&buffer); try writeFraction(&full, 123456789, 9); try std.testing.expectEqualStrings("123456789", full.buffered());
var milli = std.Io.Writer.fixed(&buffer); try writeFraction(&milli, 123456789, 3); try std.testing.expectEqualStrings("123", milli.buffered());
// Truncated, not rounded: 1.5 milliseconds is one millisecond. var truncated = std.Io.Writer.fixed(&buffer); try writeFraction(&truncated, 1_500_000, 3); try std.testing.expectEqualStrings("001", truncated.buffered());}
/// What a format string can fail to read.////// One error, because there is one question -- did this text match this/// format -- and a caller that wants to know where it stopped has/// `Result.str` to tell it.pub const ParseError = error{ParseError};
/// What `parse` read and what it made of it.pub const Result = struct { /// The prefix of the input that was read, so that a caller can carry /// on from `text[result.str.len..]`. `strptime` returns a pointer to /// the same place. str: []const u8, value: DateTime,};
/// What `parseWith` can be told.pub const Options = struct { /// Where the fields the format string does not mention come from. A /// format naming only a time gives that time on this date, which is /// the same idea as `DateTime.Options.relative_to`. relative_to: DateTime = .unix_epoch, /// The language the names are written in. locale: locale.Locale = locale.en,};
/// Reads `text` under `format_string`, leaving whatever follows.////// This is `strptime`'s own shape: the format has to match from the start/// of the input, and text after what the format asked for is left alone/// rather than refused. `Result.str` is what was read.////// ```zig/// const result = try strftime.parse("%Y-%m-%d", "2024-03-15 and more");/// // result.value is the 15th of March; result.str is "2024-03-15"./// ```pub fn parse(comptime format_string: []const u8, text: []const u8) ParseError!Result { return parseWith(format_string, text, .{});}
test parse { const result = try parse("%Y-%m-%d", "2024-03-15 and more"); try std.testing.expectEqual(@as(Year, 2024), result.value.year); try std.testing.expectEqual(Month.Mar, result.value.month); try std.testing.expectEqual(@as(u6, 15), result.value.day);
// What was read, so that a caller can carry on from the rest. try std.testing.expectEqualStrings("2024-03-15", result.str);
// The weekday is worked out rather than believed, which is why a // format naming one does not have to agree with the date. try std.testing.expectEqual(DayOfWeek.Fri, result.value.weekday);
// A field the format does not mention keeps the reference's value, // which by default is the epoch. try std.testing.expectEqual(@as(u5, 0), result.value.hour);}
/// Reads `text` under `format_string` and requires the whole of it.////// The common case, and the one `golayout.parse` and `DateTime.parseStrict`/// also take: a date with something after it is usually a mistake rather/// than a caller that meant to carry on.////// ```zig/// const value = try strftime.parseAll("%Y-%m-%dT%H:%M:%S%z", stamp);/// ```pub fn parseAll(comptime format_string: []const u8, text: []const u8) ParseError!DateTime { const result = try parseWith(format_string, text, .{}); if (result.str.len != text.len) return error.ParseError; return result.value;}
test parseAll { const value = try parseAll("%Y-%m-%dT%H:%M:%S%z", "2024-03-15T14:30:05-0500"); try std.testing.expectEqual(@as(u5, 14), value.hour); try std.testing.expectEqual(@as(i32, -5 * std.time.s_per_hour), value.offset);
// Anything left over is a failure here, where `parse` would allow it. try std.testing.expectError(error.ParseError, parseAll("%Y-%m-%d", "2024-03-15 and more"));
// A date that is not a date is refused, which the C library's // `strptime` does not do: it will happily hand back the 31st of June. try std.testing.expectError(error.ParseError, parseAll("%Y-%m-%d", "2024-06-31"));}
/// Reads `text` under `format_string`, from `options.relative_to` and in/// `options.locale`.////// What is accepted follows glibc's `strptime`, which is more forgiving/// than the formatting side is exact. Whitespace in the format matches/// any run of whitespace in the input, including none; a numeric field/// skips whitespace in front of it and takes one digit as readily as its/// full width; and a name is matched without regard to case, in either/// its long or its short form, whichever of `%a` and `%A` asked.////// Four things are read that glibc reads and throws away, because there/// is somewhere here to put them: `%Z` sets the designation, `%z` the/// offset, `%s` the whole reading, and `%G` with `%V` resolves an ISO/// week date. `%U` and `%W` are still dropped -- a week counted from the/// first Sunday of the year names no date without a weekday beside it,/// and nothing here asks for the pair.////// ```zig/// const result = try strftime.parseWith("%d %B", "15 mars", .{/// .locale = locale.byName("fr").?,/// .relative_to = base,/// });/// ```pub fn parseWith( comptime format_string: []const u8, text: []const u8, options: Options,) ParseError!Result { var state: State = .{ .value = options.relative_to }; var rest = text;
try readFormat(format_string, &rest, &state, options); try state.finish();
return .{ .str = text[0 .. text.len - rest.len], .value = state.value, };}
test parseWith { // The fields the format does not mention come from the reference // rather than from the epoch. var base: DateTime = .{ .year = 2019, .month = .Jul, .day = 4, .hour = 9 }; base.updateDayOfWeek();
const result = try parseWith("%H:%M", "14:30", .{ .relative_to = base }); try std.testing.expectEqual(@as(Year, 2019), result.value.year); try std.testing.expectEqual(Month.Jul, result.value.month); try std.testing.expectEqual(@as(u5, 14), result.value.hour);
// A name is read in the locale that was asked for, which is only // worth checking when there is another locale to ask: without // `-Dembed-locales` the library carries English alone. if (locale.byName("fr")) |french| { const read = try parseWith("%d %B %Y", "15 mars 2024", .{ .locale = french }); try std.testing.expectEqual(Month.Mar, read.value.month); }}
/// What has been read so far, and what could not be settled until the/// whole format string had been.////// A date arrives in pieces that mean nothing on their own: a century/// without a two digit year, a twelve hour clock without its meridiem, a/// day of the year that needs the year to become a month and a day. So/// each piece is put here as it is read and `finish` assembles them once.const State = struct { value: DateTime,
/// `%C`, which is a year only once `%y` has been read too. century: ?i32 = null, /// `%y`, which is a year only once it is known whether `%C` came with /// it. short_year: ?i32 = null,
month_given: bool = false, day_given: bool = false, /// `%j`, which names a date only with a year beside it. day_of_year: ?u16 = null,
/// `%G`, `%g` and `%V`, which name a date only together. iso_year: ?Year = null, iso_short_year: ?i32 = null, iso_week: ?u16 = null, /// `%u`, `%w`, `%a` or `%A`, kept because an ISO week date needs one. weekday: ?DayOfWeek = null,
/// `%I`, which is an hour only once the meridiem is known. hour12: ?u8 = null, /// `%p` or `%P`. half: ?locale.Half = null,
/// Turns what was read into the date it names, and refuses one that /// is not a date. /// /// The order matters. A year has to exist before a day of the year or /// an ISO week can be turned into a month and a day, and both of those /// only apply when the format did not name a month and a day outright: /// `%Y-%m-%d (%j)` means what it says, and the day of the year in it /// is a decoration rather than a second opinion. fn finish(self: *State) ParseError!void { if (self.century) |century| { // A century and a two digit year are one number written in // two halves, which is the only reason `%C` exists. self.value.year = century * 100 + (self.short_year orelse 0); } else if (self.short_year) |short| { // glibc's window, which is also Go's: 69 and up are the // twentieth century. It is not the one the `YY` sequence uses. self.value.year = if (short >= 69) 1900 + short else 2000 + short; }
if (!self.month_given or !self.day_given) { if (self.day_of_year) |day| { const date = Date.fromDayOfYear(self.value.year, day); self.value.year = date.year; self.value.month = date.month; self.value.day = date.day; } else if (self.iso_week) |week| { const week_year = self.iso_year orelse if (self.iso_short_year) |short| @as(Year, if (short >= 69) 1900 + short else 2000 + short) else self.value.year;
// Monday when the format named no weekday, because that is // the day an ISO week begins on and so the one the week // number alone points at. const date = Date.fromWeek(week_year, week, self.weekday orelse .Mon, .Mon, 4); self.value.year = date.year; self.value.month = date.month; self.value.day = date.day; } }
if (self.hour12) |hour| { // Twelve o'clock is hour zero of its half, which is what // makes noon and midnight fall out of the meridiem rather // than out of the number. It also settles what `%I` means // with no `%p` beside it: hour zero, because that is the // half nothing said otherwise about. glibc reduces the // number the same way and at the same moment. var settled: u8 = hour % 12; if (self.half) |half| { if (half == .pm) settled += 12; } self.value.hour = @intCast(settled); }
// A meridiem without a twelve hour clock beside it changes // nothing, which is glibc's behavior: `%H %p` of "13 AM" is one // o'clock in the afternoon and a contradiction nobody asked about.
if (!self.value.asDate().isRegular()) return error.ParseError; self.value.updateDayOfWeek(); }};
/// Reads one format string into `state`, which is what the public entry/// points and the composite conversions are both built out of.fn readFormat( comptime format_string: []const u8, rest: *[]const u8, state: *State, options: Options,) ParseError!void { const chunks = comptime tokenize(format_string);
inline for (chunks) |chunk| switch (chunk) { .literal => |text| try readLiteral(rest, text), .conversion => |field| try readConversion(field, rest, state, options), // `tokenize` refuses rather than copying, so there are none. .unknown => comptime unreachable, };}
/// Matches a literal run of the format string against the input.////// Whitespace in a format string matches any run of whitespace in the/// input, including none at all, which is why `%Y %m` reads "202403"./// Everything else has to be there exactly. Both are POSIX's rules for/// `strptime` rather than choices made here.fn readLiteral(rest: *[]const u8, text: []const u8) ParseError!void { var input = rest.*;
for (text) |char| { if (std.ascii.isWhitespace(char)) { while (input.len > 0 and std.ascii.isWhitespace(input[0])) input = input[1..]; continue; } if (input.len == 0 or input[0] != char) return error.ParseError; input = input[1..]; }
rest.* = input;}
test readLiteral { var exact: []const u8 = "-15"; try readLiteral(&exact, "-"); try std.testing.expectEqualStrings("15", exact);
// A space in the format matches a run of them, or none. var run: []const u8 = " 15"; try readLiteral(&run, " "); try std.testing.expectEqualStrings("15", run);
var missing: []const u8 = "15"; try readLiteral(&missing, " "); try std.testing.expectEqualStrings("15", missing);
var wrong: []const u8 = "/15"; try std.testing.expectError(error.ParseError, readLiteral(&wrong, "-"));}
/// Reads one conversion out of the input and records it.fn readConversion( comptime field: Field, rest: *[]const u8, state: *State, options: Options,) ParseError!void { switch (field.which) { // Whitespace in the input, or none: the same rule a literal space // follows. .newline, .tab => try readLiteral(rest, " "),
.percent => try readLiteral(rest, "%"),
.weekday_short, .weekday_long => { // Either form is accepted whichever was asked for, as glibc // does, and the long one is tried first so that "Tuesday" is // not read as "Tue" with "sday" left over. skipWhitespace(rest); const match = options.locale.matchWeekday(rest.*, .dddd) orelse options.locale.matchWeekday(rest.*, .ddd) orelse return error.ParseError; state.weekday = match.weekday; rest.* = rest.*[match.len..]; },
.month_short, .month_long => { skipWhitespace(rest); const match = options.locale.matchMonth(rest.*, .MMMM) orelse options.locale.matchMonth(rest.*, .MMM) orelse return error.ParseError; state.value.month = match.month; state.month_given = true; rest.* = rest.*[match.len..]; },
.meridiem_upper, .meridiem_lower => { skipWhitespace(rest); const match = options.locale.matchMeridiem(rest.*) orelse return error.ParseError; state.half = match.half; rest.* = rest.*[match.len..]; },
.century => state.century = try readNumber(rest, 0, 99, 2),
.day, .day_spaced => { state.value.day = @intCast(try readNumber(rest, 1, 31, 2)); state.day_given = true; },
.month => { state.value.month = @fromBackingInt(@intCast(try readNumber(rest, 1, 12, 2))); state.month_given = true; },
.day_of_year => state.day_of_year = @intCast(try readNumber(rest, 1, 366, 3)),
.hour24, .hour24_spaced => state.value.hour = @intCast(try readNumber(rest, 0, 23, 2)), .hour12, .hour12_spaced => state.hour12 = @intCast(try readNumber(rest, 1, 12, 2)), .minute => state.value.minute = @intCast(try readNumber(rest, 0, 59, 2)), // Up to 61, which is what a `struct tm` has always allowed for a // leap second and the second one that never came. .second => state.value.second = @intCast(try readNumber(rest, 0, 61, 2)),
.year => { state.value.year = try readNumber(rest, 0, 9999, 4); state.century = null; state.short_year = null; }, .year_short => state.short_year = try readNumber(rest, 0, 99, 2),
.iso_year => state.iso_year = try readNumber(rest, 0, 9999, 4), .iso_year_short => state.iso_short_year = try readNumber(rest, 0, 99, 2), .iso_week => state.iso_week = @intCast(try readNumber(rest, 1, 53, 2)),
// Read and dropped. A week counted from the year's first Sunday // or Monday names a date only with a weekday beside it, and // neither this nor glibc assembles that pair; `%V` with `%G` is // the one that does. .week_from_sunday, .week_from_monday => _ = try readNumber(rest, 0, 53, 2),
// The ISO numbering runs Monday to Sunday as 1 to 7 while // `DayOfWeek` counts Sunday as 0, so seven wraps to zero. .iso_weekday => state.weekday = @fromBackingInt(@intCast(@mod(try readNumber(rest, 1, 7, 1), 7))), .weekday_number => state.weekday = @fromBackingInt(@intCast(try readNumber(rest, 0, 6, 1))),
.nanosecond => state.value.nanosecond = try readFraction(rest, if (field.width == 0) 9 else field.width),
.epoch_seconds => { const seconds = try readSeconds(rest); const instant: Instant = .{ .timestamp = @as(i128, seconds) * std.time.ns_per_s }; const nanosecond = state.value.nanosecond; state.value = instant.asDateTime(); // An instant says nothing about the fraction of a second, so // whatever `%N` read stays where it was. state.value.nanosecond = nanosecond; state.month_given = true; state.day_given = true; state.century = null; state.short_year = null; },
.offset, .offset_colon, .offset_colon_seconds, .offset_minimal => { state.value.offset = try readOffset(rest); },
.zone => { // glibc steps over a run of anything that is not whitespace // and remembers none of it. The stepping over is the same // here; what is stepped over is kept, because a `DateTime` // has a field for it. skipWhitespace(rest); var length: usize = 0; while (length < rest.len and !std.ascii.isWhitespace(rest.*[length])) length += 1; if (length == 0) return error.ParseError; state.value.designation = .from(rest.*[0..length]); rest.* = rest.*[length..]; },
.date_and_time, .date_slashes, .date_dashes, .time12, .hour_minute, .time24 => try readFormat( comptime expansion(field.which).?, rest, state, options, ), }}
/// Steps over any whitespace at the start of the input.////// Every numeric and name conversion does this before reading, which is/// POSIX's rule and is what lets `%e` read both "15" and " 5".fn skipWhitespace(rest: *[]const u8) void { var input = rest.*; while (input.len > 0 and std.ascii.isWhitespace(input[0])) input = input[1..]; rest.* = input;}
/// Reads a number of at most `max_digits` digits and checks it against/// `from` and `to`.////// This is glibc's `get_number`, quirk and all, because the quirk is/// load-bearing: after the first digit it takes another only while the/// number so far could still be multiplied by ten and stay in range. So/// `%d` of "99" reads a single 9 and leaves the second, where a plain/// two digit read would take 99 and refuse it. That is what lets an/// unpadded field sit next to a digit.fn readNumber( rest: *[]const u8, comptime from: i32, comptime to: i32, comptime max_digits: usize,) ParseError!i32 { skipWhitespace(rest);
var input = rest.*; if (input.len == 0 or !std.ascii.isDigit(input[0])) return error.ParseError;
var value: i32 = input[0] - '0'; input = input[1..];
var left = max_digits - 1; while (left > 0 and value * 10 <= to and input.len > 0 and std.ascii.isDigit(input[0])) { value = value * 10 + (input[0] - '0'); input = input[1..]; left -= 1; }
if (value < from or value > to) return error.ParseError;
rest.* = input; return value;}
test readNumber { var padded: []const u8 = "05-"; try std.testing.expectEqual(@as(i32, 5), try readNumber(&padded, 1, 31, 2)); try std.testing.expectEqualStrings("-", padded);
// A field takes a second digit when one is there and it still fits. var wide: []const u8 = "15"; try std.testing.expectEqual(@as(i32, 15), try readNumber(&wide, 1, 31, 2));
// And stops when it would not: 99 is not a day of the month, so this // is day 9 with a 9 left over rather than a refusal. var greedy: []const u8 = "99"; try std.testing.expectEqual(@as(i32, 9), try readNumber(&greedy, 1, 31, 2)); try std.testing.expectEqualStrings("9", greedy);
// Out of range after all the digits are in is a refusal. var large: []const u8 = "32"; try std.testing.expectError(error.ParseError, readNumber(&large, 1, 31, 2));
// Leading whitespace belongs to the field, which is how " 5" reads. var spaced: []const u8 = " 5"; try std.testing.expectEqual(@as(i32, 5), try readNumber(&spaced, 1, 31, 2));
var empty: []const u8 = "x"; try std.testing.expectError(error.ParseError, readNumber(&empty, 1, 31, 2));}
/// Reads a count of seconds since the epoch, which is the one field here/// that can be negative.fn readSeconds(rest: *[]const u8) ParseError!i64 { skipWhitespace(rest);
var input = rest.*; const negative = input.len > 0 and input[0] == '-'; if (negative) input = input[1..];
if (input.len == 0 or !std.ascii.isDigit(input[0])) return error.ParseError;
var value: i64 = 0; var digits: usize = 0; while (input.len > 0 and std.ascii.isDigit(input[0])) { // Sixteen digits is half a billion years either side of the // epoch, far more than a `Year` can hold and comfortably inside // an `i64`, so a longer run is not a time. if (digits == 16) return error.ParseError; value = value * 10 + (input[0] - '0'); input = input[1..]; digits += 1; }
rest.* = input; return if (negative) -value else value;}
test readSeconds { var forward: []const u8 = "1710513005"; try std.testing.expectEqual(@as(i64, 1710513005), try readSeconds(&forward));
var backward: []const u8 = "-2208988800"; try std.testing.expectEqual(@as(i64, -2208988800), try readSeconds(&backward));
var nothing: []const u8 = "-"; try std.testing.expectError(error.ParseError, readSeconds(¬hing));}
/// Reads a fraction of a second of at most `digits` digits and scales it/// to nanoseconds, so "5" is half a second whatever width was asked for.fn readFraction(rest: *[]const u8, digits: u8) ParseError!Nanosecond { var input = rest.*; if (input.len == 0 or !std.ascii.isDigit(input[0])) return error.ParseError;
var value: Nanosecond = 0; var read: u8 = 0; while (read < digits and read < 9 and input.len > 0 and std.ascii.isDigit(input[0])) { value = value * 10 + (input[0] - '0'); input = input[1..]; read += 1; }
// Scaled by where the last digit read sat, not by how many were // asked for: a field cut short is a coarser number, not a wrong one. var scale: Nanosecond = 1; for (read..9) |_| scale *= 10;
rest.* = input; return value * scale;}
test readFraction { var half: []const u8 = "5"; try std.testing.expectEqual(@as(Nanosecond, 500_000_000), try readFraction(&half, 9));
var milli: []const u8 = "123"; try std.testing.expectEqual(@as(Nanosecond, 123_000_000), try readFraction(&milli, 3));
// The width is a maximum: what follows is left for the next field. var narrow: []const u8 = "123456"; try std.testing.expectEqual(@as(Nanosecond, 123_000_000), try readFraction(&narrow, 3)); try std.testing.expectEqualStrings("456", narrow);}
/// Reads an offset from UTC and returns it in seconds east.////// Every shape any of the `%z` forms writes is accepted whichever of them/// asked, along with the bare `Z` that means zero: a format string is/// usually being pointed at text somebody else wrote, and refusing/// `+05:45` because the format said `%z` rather than `%:z` would be a/// distinction without a purpose. glibc reads the same set.fn readOffset(rest: *[]const u8) ParseError!i32 { skipWhitespace(rest);
var input = rest.*; if (input.len == 0) return error.ParseError;
if (input[0] == 'Z' or input[0] == 'z') { rest.* = input[1..]; return 0; }
const sign: i32 = switch (input[0]) { '+' => 1, '-' => -1, else => return error.ParseError, }; input = input[1..];
const hours = try twoDigits(&input); var minutes: i32 = 0; var seconds: i32 = 0;
if (twoDigitsAfter(&input)) |value| { minutes = value; if (twoDigitsAfter(&input)) |more| seconds = more; }
if (hours > 23 or minutes > 59 or seconds > 59) return error.ParseError;
rest.* = input; return sign * (hours * std.time.s_per_hour + minutes * std.time.s_per_min + seconds);}
test readOffset { var basic: []const u8 = "-0500"; try std.testing.expectEqual(@as(i32, -5 * std.time.s_per_hour), try readOffset(&basic));
var extended: []const u8 = "+05:45"; try std.testing.expectEqual( @as(i32, 5 * std.time.s_per_hour + 45 * std.time.s_per_min), try readOffset(&extended), );
// Hours alone, which is what `%:::z` writes for a whole one. var hours: []const u8 = "-05"; try std.testing.expectEqual(@as(i32, -5 * std.time.s_per_hour), try readOffset(&hours));
var seconds: []const u8 = "-05:50:36"; try std.testing.expectEqual( @as(i32, -(5 * std.time.s_per_hour + 50 * std.time.s_per_min + 36)), try readOffset(&seconds), );
var zulu: []const u8 = "Z"; try std.testing.expectEqual(@as(i32, 0), try readOffset(&zulu));
var missing: []const u8 = "0500"; try std.testing.expectError(error.ParseError, readOffset(&missing));}
/// Reads exactly two digits, which every part of an offset is written as.fn twoDigits(rest: *[]const u8) ParseError!i32 { const input = rest.*; if (input.len < 2) return error.ParseError; if (!std.ascii.isDigit(input[0]) or !std.ascii.isDigit(input[1])) return error.ParseError; rest.* = input[2..]; return @as(i32, input[0] - '0') * 10 + (input[1] - '0');}
/// Reads the next two digits of an offset, over a colon if there is one,/// or returns null when the offset stops here.fn twoDigitsAfter(rest: *[]const u8) ?i32 { var input = rest.*; if (input.len > 0 and input[0] == ':') input = input[1..]; if (input.len < 2) return null; if (!std.ascii.isDigit(input[0]) or !std.ascii.isDigit(input[1])) return null;
rest.* = input[2..]; return @as(i32, input[0] - '0') * 10 + (input[1] - '0');}
test "a formatted date reads back as itself" { // The property the two halves owe each other, over the pieces that // carry enough to rebuild a date. var value: DateTime = .{ .year = 2024, .month = .Mar, .day = 15, .hour = 14, .minute = 30, .second = 5, .offset = 5 * std.time.s_per_hour + 45 * std.time.s_per_min, .designation = .from("+0545"), }; value.updateDayOfWeek();
var buffer: [128]u8 = undefined;
inline for (.{ "%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S%:z", "%A, %d %B %Y %I:%M:%S %p", "%F %T", "%C%y-%j %T", "%G-W%V-%u %T", }) |format_string| { const written = try bufFormat(&buffer, value, format_string); const back = try parseAll(format_string, written);
try std.testing.expectEqual(value.year, back.year); try std.testing.expectEqual(value.month, back.month); try std.testing.expectEqual(value.day, back.day); try std.testing.expectEqual(value.hour, back.hour); try std.testing.expectEqual(value.minute, back.minute); try std.testing.expectEqual(value.second, back.second); }
// `%s` round trips as well, but it is an instant rather than a // reading: what comes back is the same moment written in UTC, because // a count of seconds since the epoch says nothing about where the // clock was. const stamp = try bufFormat(&buffer, value, "%s"); const instant = try parseAll("%s", stamp); try std.testing.expectEqual(value.toInstant().timestamp, instant.toInstant().timestamp); try std.testing.expectEqual(@as(i32, 0), instant.offset);}