Support format strings for datetimes

This commit is contained in:
jaina heartles 2022-12-12 02:42:24 -08:00
parent 091146b617
commit 11bdaad6d7
1 changed files with 9 additions and 7 deletions

View File

@ -117,14 +117,16 @@ pub fn toCharArrayZ(value: DateTime) [array_len + 1:0]u8 {
return buf;
}
pub fn format(value: DateTime, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
return std.fmt.format(
writer,
"{: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() },
);
pub fn format(value: DateTime, comptime fmt: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
if (comptime std.ascii.eqlIgnoreCase(fmt, "rfc3339") or fmt.len == 0) {
return std.fmt.format(
writer,
"{: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 {
try std.fmt.format(writer, "\"{}\"", .{value});
try std.fmt.format(writer, "\"{rfc3339}\"", .{value});
}