authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2021-10-23 14:07:01 -07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-10-23 14:07:01 -07:00
log73c55102612e55eb8f07eadb51cfa610efc9f3eb
tree77fbded31dfe5d883d1852e1fdcb50601bd3f0ea
parent64bc6c77e8189c60a5223877827b4753eb2b483d
parent3f9e83381841c403418b418c67b3c9ecf6c9c0eb
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2 from hnakamur/support_millisecond_format

Support millisecond format SSS

2 files changed, 12 insertions(+), 5 deletions(-)

main.zig+10-5
...@@ -6,16 +6,21 @@ pub fn main() !void {...@@ -6,16 +6,21 @@ pub fn main() !void {
6 std.log.info("All your codebase are belong to us.", .{});6 std.log.info("All your codebase are belong to us.", .{});
7}7}
88
9fn assertOk(input: u64, expected: string) !void {9fn assertOk(dt: time.DateTime, comptime format: []const u8, expected: string) !void {
10 const alloc = std.testing.allocator;10 const alloc = std.testing.allocator;
1111
12 const actual = try time.DateTime.initUnix(input).formatAlloc(alloc, "YYYY-MM-DD HH:mm:ss");12 const actual = try dt.formatAlloc(alloc, format);
13 defer alloc.free(actual);13 defer alloc.free(actual);
1414
15 try std.testing.expectEqualStrings(expected, actual);15 try std.testing.expectEqualStrings(expected, actual);
16}16}
1717
18// zig fmt: off18// zig fmt: off
19test { try assertOk(0, "1970-01-01 00:00:00"); }19const initUnix = time.DateTime.initUnix;
20test { try assertOk(1257894000, "2009-11-10 23:00:00"); }20test { try assertOk(initUnix(0), "YYY-MM-DD HH:mm:ss", "1970-01-01 00:00:00"); }
21test { try assertOk(1634858430, "2021-10-21 23:20:30"); }21test { try assertOk(initUnix(1257894000), "YYY-MM-DD HH:mm:ss", "2009-11-10 23:00:00"); }
22test { try assertOk(initUnix(1634858430), "YYY-MM-DD HH:mm:ss", "2021-10-21 23:20:30"); }
23
24const initUnixMs = time.DateTime.initUnixMs;
25test { try assertOk(initUnixMs(1634858430023), "YYY-MM-DD HH:mm:ss.SSS", "2021-10-21 23:20:30.023"); }
26
time.zig+2
...@@ -212,6 +212,7 @@ pub const DateTime = struct {...@@ -212,6 +212,7 @@ pub const DateTime = struct {
212 .HH => try writer.print("{:0>2}", .{self.hours}),212 .HH => try writer.print("{:0>2}", .{self.hours}),
213 .mm => try writer.print("{:0>2}", .{self.minutes}),213 .mm => try writer.print("{:0>2}", .{self.minutes}),
214 .ss => try writer.print("{:0>2}", .{self.seconds}),214 .ss => try writer.print("{:0>2}", .{self.seconds}),
215 .SSS => try writer.print("{:0>3}", .{self.ms}),
215 .MM => try writer.print("{:0>2}", .{self.months + 1}),216 .MM => try writer.print("{:0>2}", .{self.months + 1}),
216 .z => try writer.writeAll(@tagName(self.timezone)),217 .z => try writer.writeAll(@tagName(self.timezone)),
217218
...@@ -231,6 +232,7 @@ pub const DateTime = struct {...@@ -231,6 +232,7 @@ pub const DateTime = struct {
231 ' ',232 ' ',
232 ':',233 ':',
233 '-',234 '-',
235 '.',
234 => {236 => {
235 try writer.writeAll(&.{c});237 try writer.writeAll(&.{c});
236 s = i + 1;238 s = i + 1;