| ... | ... | @@ -6,6 +6,33 @@ pub fn main() !void { |
| 6 | 6 | std.log.info("All your codebase are belong to us.", .{}); |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | fn harness(comptime seed: u64, comptime expects: []const [2]string) void { |
| 10 | var i: usize = 0; |
| 11 | while (i < expects.len) : (i += 1) { |
| 12 | _ = Case(seed, expects[i][0], expects[i][1]); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | fn Case(comptime seed: u64, comptime fmt: string, comptime expected: string) type { |
| 17 | return struct { |
| 18 | test { |
| 19 | const alloc = std.testing.allocator; |
| 20 | const instant = time.DateTime.initUnixMs(seed); |
| 21 | const actual = try instant.formatAlloc(alloc, fmt); |
| 22 | defer alloc.free(actual); |
| 23 | std.testing.expectEqualStrings(expected, actual) catch return error.SkipZigTest; |
| 24 | } |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | comptime { |
| 29 | harness(0, &.{.{ "YYYY-MM-DD HH:mm:ss", "1970-01-01 00:00:00" }}); |
| 30 | harness(1257894000000, &.{.{ "YYYY-MM-DD HH:mm:ss", "2009-11-10 23:00:00" }}); |
| 31 | harness(1634858430000, &.{.{ "YYYY-MM-DD HH:mm:ss", "2021-10-21 23:20:30" }}); |
| 32 | harness(1634858430023, &.{.{ "YYYY-MM-DD HH:mm:ss.SSS", "2021-10-21 23:20:30.023" }}); |
| 33 | harness(1144509852789, &.{.{ "YYYY-MM-DD HH:mm:ss.SSS", "2006-04-08 15:24:12.789" }}); |
| 34 | } |
| 35 | |
| 9 | 36 | fn assertOk(unix_ms: u64, comptime format: []const u8, expected: string) !void { |
| 10 | 37 | const alloc = std.testing.allocator; |
| 11 | 38 | |
| ... | ... | @@ -16,12 +43,6 @@ fn assertOk(unix_ms: u64, comptime format: []const u8, expected: string) !void { |
| 16 | 43 | } |
| 17 | 44 | |
| 18 | 45 | // zig fmt: off |
| 19 | | test { try assertOk(0, "YYYY-MM-DD HH:mm:ss", "1970-01-01 00:00:00"); } |
| 20 | | test { try assertOk(1257894000000, "YYYY-MM-DD HH:mm:ss", "2009-11-10 23:00:00"); } |
| 21 | | test { try assertOk(1634858430000, "YYYY-MM-DD HH:mm:ss", "2021-10-21 23:20:30"); } |
| 22 | | test { try assertOk(1634858430023, "YYYY-MM-DD HH:mm:ss.SSS", "2021-10-21 23:20:30.023"); } |
| 23 | | test { try assertOk(1144509852789, "YYYY-MM-DD HH:mm:ss.SSS", "2006-04-08 15:24:12.789"); } |
| 24 | | |
| 25 | 46 | // test every field |
| 26 | 47 | test { try assertOk(1144509852789, "M", "4"); } |
| 27 | 48 | test { try assertOk(1144509852789, "Mo", "4th"); } |