Support format strings for datetimes

This commit is contained in:
jaina heartles 2022-12-12 02:42:24 -08:00
parent 091146b617
commit 11bdaad6d7

View file

@ -117,14 +117,16 @@ pub fn toCharArrayZ(value: DateTime) [array_len + 1:0]u8 {
return buf; return buf;
} }
pub fn format(value: DateTime, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { pub fn format(value: DateTime, comptime fmt: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
return std.fmt.format( if (comptime std.ascii.eqlIgnoreCase(fmt, "rfc3339") or fmt.len == 0) {
writer, return std.fmt.format(
"{:0>4}-{:0>2}-{:0>2}T{:0>2}:{:0>2}:{:0>2}Z", writer,
.{ value.year(), value.month().numeric(), value.day(), value.hour(), value.minute(), value.second() }, "{:0>4}-{:0>2}-{:0>2}T{:0>2}:{:0>2}:{:0>2}Z",
); .{ value.year(), value.month().numeric(), value.day(), value.hour(), value.minute(), value.second() },
);
} else @compileError("Unknown DateTime format " ++ fmt);
} }
pub fn jsonStringify(value: DateTime, _: std.json.StringifyOptions, writer: anytype) !void { pub fn jsonStringify(value: DateTime, _: std.json.StringifyOptions, writer: anytype) !void {
try std.fmt.format(writer, "\"{}\"", .{value}); try std.fmt.format(writer, "\"{rfc3339}\"", .{value});
} }