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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Writing the pieces of a formatted date that are more than a padded//! number: English ordinal suffixes and UTC offsets.
const std = @import("std");
const normal = [_][]const u8{ "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",};
test "normal" { try std.testing.expectEqual(10, normal.len);}
/// Writes `num` followed by its English ordinal suffix ("1st", "2nd",/// "3rd", "4th", ...). `num` must be an unsigned integer.pub fn ordinal(writer: anytype, num: anytype) !void { const info = @typeInfo(@TypeOf(num)); if (info != .int) @compileError("ordinal can only print integers"); if (info.int.signedness != .unsigned) @compileError("ordinal can only print unsigned integers");
const table = normal;
// The suffix is chosen from the last two digits, so the arithmetic // needs a type that can represent 10. Several callers pass something // narrower than that -- a quarter and a weekday number are both `u3` -- // where dividing by 10 would not compile, so those widen first. const Wide = if (info.int.bits < 4) u8 else @TypeOf(num); const wide: Wide = num;
try writer.print("{}", .{num}); try writer.writeAll( switch (wide / 10 % 10) { 1 => table[0], else => table[wide % 10], }, );}
test ordinal { var buf: [8]u8 = undefined;
// A type too narrow to hold 10 is widened rather than rejected: a // quarter and a weekday number both arrive as `u3`. var narrow = std.Io.Writer.fixed(&buf); try ordinal(&narrow, @as(u3, 3)); try std.testing.expectEqualStrings("3rd", narrow.buffered());
var first = std.Io.Writer.fixed(&buf); try ordinal(&first, @as(u16, 1)); try std.testing.expectEqualStrings("1st", first.buffered());
// The teens are the exception: 11 through 13 take "th" even though // their last digit says otherwise. var teen = std.Io.Writer.fixed(&buf); try ordinal(&teen, @as(u16, 11)); try std.testing.expectEqualStrings("11th", teen.buffered());}
test "print ordinal normal" { const cases = [_]struct { number: u16, result: []const u8 }{ .{ .number = 0, .result = "0th", }, .{ .number = 1, .result = "1st", }, .{ .number = 2, .result = "2nd", }, .{ .number = 3, .result = "3rd", }, .{ .number = 4, .result = "4th", }, .{ .number = 11, .result = "11th", }, .{ .number = 12, .result = "12th", }, .{ .number = 13, .result = "13th", }, .{ .number = 14, .result = "14th", }, .{ .number = 31, .result = "31st", }, .{ .number = 112, .result = "112th", }, .{ .number = 9311, .result = "9311th", }, };
inline for (cases) |case| { var buf: std.Io.Writer.Allocating = .init(std.testing.allocator); defer buf.deinit();
try ordinal(&buf.writer, case.number); try std.testing.expectEqualStrings(case.result, buf.written()); }}
/// Writes a UTC offset given in `seconds` east of UTC in the form `+HH:MM`/// or `+HHMM`, depending on `separator`. The sign is always written, and/// zero is written as `+00:00` rather than as `Z`.////// Neither RFC 822 nor ISO 8601 can express an offset finer than a minute,/// so an offset that is not a whole number of minutes is truncated towards/// zero. That only arises for the local mean time offsets that timezones/// used before standard time, such as America/Chicago's -5:50:36.pub fn offset(writer: anytype, seconds: i32, separator: enum { none, colon }) !void { try writer.writeAll(if (seconds < 0) "-" else "+");
const magnitude = @abs(seconds); try writer.print("{d:0>2}", .{magnitude / std.time.s_per_hour}); switch (separator) { .none => {}, .colon => try writer.writeAll(":"), } try writer.print("{d:0>2}", .{magnitude % std.time.s_per_hour / std.time.s_per_min});}
test offset { const cases = [_]struct { seconds: i32, colon: []const u8, none: []const u8 }{ .{ .seconds = 0, .colon = "+00:00", .none = "+0000" }, .{ .seconds = -5 * std.time.s_per_hour, .colon = "-05:00", .none = "-0500" }, .{ .seconds = 5 * std.time.s_per_hour + 45 * std.time.s_per_min, .colon = "+05:45", .none = "+0545" }, .{ .seconds = -(3 * std.time.s_per_hour + 30 * std.time.s_per_min), .colon = "-03:30", .none = "-0330" }, .{ .seconds = 14 * std.time.s_per_hour, .colon = "+14:00", .none = "+1400" }, // America/Chicago's local mean time, truncated towards zero. .{ .seconds = -21036, .colon = "-05:50", .none = "-0550" }, };
for (cases) |case| { var buf: std.Io.Writer.Allocating = .init(std.testing.allocator); defer buf.deinit();
try offset(&buf.writer, case.seconds, .colon); try std.testing.expectEqualStrings(case.colon, buf.written());
buf.clearRetainingCapacity(); try offset(&buf.writer, case.seconds, .none); try std.testing.expectEqualStrings(case.none, buf.written()); }}