1const std = @import("std");
2const string = []const u8;
3const time = @import("time");
4const expect = @import("expect").expect;
5
6pub fn main() !void {
7 std.log.info("All your codebase are belong to us.", .{});
8}
9
10fn harness(comptime seed: u64, comptime expects: []const [2]string) void {
11 for (0..expects.len) |i| {
12 _ = Case(seed, expects[i][0], expects[i][1]);
13 }
14}
15
16fn 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
28fn expectFmt(instant: time.DateTime, comptime fmt: string, comptime expected: string) !void {
29 const alloc = std.testing.allocator;
30 const actual = try instant.formatAlloc(alloc, fmt);
31 defer alloc.free(actual);
32 std.testing.expectEqualStrings(expected, actual) catch return error.SkipZigTest;
33}
34
35comptime {
36 harness(0, &.{.{ "YYYY-MM-DD HH:mm:ss", "1970-01-01 00:00:00" }});
37 harness(1257894000000, &.{.{ "YYYY-MM-DD HH:mm:ss", "2009-11-10 23:00:00" }});
38 harness(1634858430000, &.{.{ "YYYY-MM-DD HH:mm:ss", "2021-10-21 23:20:30" }});
39 harness(1634858430023, &.{.{ "YYYY-MM-DD HH:mm:ss.SSS", "2021-10-21 23:20:30.023" }});
40 harness(1144509852789, &.{.{ "YYYY-MM-DD HH:mm:ss.SSS", "2006-04-08 15:24:12.789" }});
41
42 harness(1635033600000, &.{
43 .{ "H", "0" }, .{ "HH", "00" },
44 .{ "h", "12" }, .{ "hh", "12" },
45 .{ "k", "24" }, .{ "kk", "24" },
46 });
47
48 harness(1635037200000, &.{
49 .{ "H", "1" }, .{ "HH", "01" },
50 .{ "h", "1" }, .{ "hh", "01" },
51 .{ "k", "1" }, .{ "kk", "01" },
52 });
53
54 harness(1635076800000, &.{
55 .{ "H", "12" }, .{ "HH", "12" },
56 .{ "h", "12" }, .{ "hh", "12" },
57 .{ "k", "12" }, .{ "kk", "12" },
58 });
59 harness(1635080400000, &.{
60 .{ "H", "13" }, .{ "HH", "13" },
61 .{ "h", "1" }, .{ "hh", "01" },
62 .{ "k", "13" }, .{ "kk", "13" },
63 });
64
65 harness(1144509852789, &.{
66 .{ "M", "4" },
67 .{ "Mo", "4th" },
68 .{ "MM", "04" },
69 .{ "MMM", "Apr" },
70 .{ "MMMM", "April" },
71
72 .{ "Q", "2" },
73 .{ "Qo", "2nd" },
74
75 .{ "D", "8" },
76 .{ "Do", "8th" },
77 .{ "DD", "08" },
78
79 .{ "DDD", "98" },
80 .{ "DDDo", "98th" },
81 .{ "DDDD", "098" },
82
83 .{ "d", "6" },
84 .{ "do", "6th" },
85 .{ "dd", "Sa" },
86 .{ "ddd", "Sat" },
87 .{ "dddd", "Saturday" },
88 .{ "e", "6" },
89 .{ "E", "7" },
90
91 .{ "w", "14" },
92 .{ "wo", "14th" },
93 .{ "ww", "14" },
94
95 .{ "Y", "12006" },
96 .{ "YY", "06" },
97 .{ "YYY", "2006" },
98 .{ "YYYY", "2006" },
99
100 .{ "N", "AD" },
101 .{ "NN", "Anno Domini" },
102
103 .{ "A", "PM" },
104 .{ "a", "pm" },
105
106 .{ "H", "15" },
107 .{ "HH", "15" },
108 .{ "h", "3" },
109 .{ "hh", "03" },
110 .{ "k", "15" },
111 .{ "kk", "15" },
112
113 .{ "m", "24" },
114 .{ "mm", "24" },
115
116 .{ "s", "12" },
117 .{ "ss", "12" },
118
119 .{ "S", "7" },
120 .{ "SS", "78" },
121 .{ "SSS", "789" },
122
123 .{ "Z", "+00:00" },
124 .{ "ZZ", "+0000" },
125
126 .{ "x", "1144509852789" },
127 .{ "X", "1144509852" },
128
129 .{ time.format.LT, "3:24 PM" },
130
131 .{ time.format.LTS, "3:24:12 PM" },
132
133 .{ time.format.L, "04/08/2006" },
134
135 .{ time.format.l, "4/8/2006" },
136
137 .{ time.format.LL, "April 8, 2006" },
138
139 .{ time.format.ll, "Apr 8, 2006" },
140
141 .{ time.format.LLL, "April 8, 2006 3:24 PM" },
142
143 .{ time.format.lll, "Apr 8, 2006 3:24 PM" },
144
145 .{ time.format.LLLL, "Saturday, April 8, 2006 3:24 PM" },
146
147 .{ time.format.llll, "Sat, Apr 8, 2006 3:24 PM" },
148 });
149
150 // https://github.com/nektro/zig-time/issues/3
151 harness(1144509852789, &.{.{ "YYYYMM", "200604" }});
152}
153
154// https://github.com/nektro/zig-time/issues/9
155test {
156 var t = time.DateTime.initUnix(1330502962);
157 try expectFmt(t, "YYYY-MM-DD hh:mm:ss A", "2012-02-29 08:09:22 AM");
158 t = t.addYears(1);
159 try expectFmt(t, "YYYY-MM-DD hh:mm:ss A", "2013-03-01 08:09:22 AM");
160}
161
162test {
163 var t = time.DateTime.initUnix(1330502962);
164 try expectFmt(t, "Z", "+00:00");
165 t.z_offset += 31;
166 try expectFmt(t, "Z", "+07:45");
167 t.z_offset -= 65;
168 try expectFmt(t, "Z", "-08:30");
169}
170test {
171 var t1 = time.DateTime.init(2026, 7, 10, 11, 14, 45, 0);
172 t1.z_offset -= 28;
173 try expectFmt(t1, "Z", "-07:00");
174 var t2 = time.DateTime.init(2026, 7, 10, 18, 14, 45, 0);
175 try expectFmt(t2, "Z", "+00:00");
176 try expect(t1.toUnix()).toEqual(t2.toUnix());
177}