authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-10-19 10:46:08 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-10-19 10:53:21 -07:00
logcd783ccfc1f17a23f96512d337b2d5d7b10787c7
treea545d28f6e4ae35605542f86bcc5f37380698590
parentdcf876ca97b391153bdaa72b21601f76b80e00e1

more tests


4 files changed, 94 insertions(+), 12 deletions(-)

licenses.txt+1
......@@ -1,4 +1,5 @@
11MIT:
22= https://spdx.org/licenses/MIT
33- This
4- git https://github.com/nektro/zig-expect
45- git https://github.com/nektro/zig-extras
test.zig+57
......@@ -1,6 +1,7 @@
11const std = @import("std");
22const string = []const u8;
33const time = @import("time");
4const expect = @import("expect").expect;
45
56pub fn main() !void {
67 std.log.info("All your codebase are belong to us.", .{});
......@@ -158,3 +159,59 @@ test {
158159 t = t.addYears(1);
159160 try expectFmt(t, "YYYY-MM-DD hh:mm:ss A z", "2013-03-01 08:09:22 AM UTC");
160161}
162
163// src/test/moment/add_subtract.js
164test {
165 // var a = moment(),
166 // b,
167 // c,
168 // d;
169 // a.year(2011);
170 // a.month(9);
171 // a.date(12);
172 // a.hours(6);
173 // a.minutes(7);
174 // a.seconds(8);
175 // a.milliseconds(500);
176
177 // assert.equal(a.add({ ms: 50 }).milliseconds(), 550, 'Add milliseconds');
178 try expect(time.DateTime.init(2011, 9, 11, 6, 7, 8, 500).addMs(50).ms).toEqual(550);
179 // assert.equal(a.add({ s: 1 }).seconds(), 9, 'Add seconds');
180 try expect(time.DateTime.init(2011, 9, 11, 6, 7, 8, 500).addSecs(1).seconds).toEqual(9);
181 // assert.equal(a.add({ m: 1 }).minutes(), 8, 'Add minutes');
182 try expect(time.DateTime.init(2011, 9, 11, 6, 7, 8, 500).addMins(1).minutes).toEqual(8);
183 // assert.equal(a.add({ h: 1 }).hours(), 7, 'Add hours');
184 try expect(time.DateTime.init(2011, 9, 11, 6, 7, 8, 500).addHours(1).hours).toEqual(7);
185 // assert.equal(a.add({ d: 1 }).date(), 13, 'Add date');
186 try expect(time.DateTime.init(2011, 9, 11, 6, 7, 8, 500).addDays(1).days).toEqual(12);
187 // assert.equal(a.add({ w: 1 }).date(), 20, 'Add week');
188 try expect(time.DateTime.init(2011, 9, 11, 6, 7, 8, 500).addWeeks(1).days).toEqual(18); //TODO:
189 // assert.equal(a.add({ M: 1 }).month(), 10, 'Add month');
190 try expect(time.DateTime.init(2011, 9, 11, 6, 7, 8, 500).addMonths(1).months).toEqual(10);
191 // assert.equal(a.add({ y: 1 }).year(), 2012, 'Add year');
192 try expect(time.DateTime.init(2011, 9, 11, 6, 7, 8, 500).addYears(1).years).toEqual(2012);
193 // assert.equal(a.add({ Q: 1 }).month(), 1, 'Add quarter');
194 try expect(time.DateTime.init(2011, 9, 11, 6, 7, 8, 500).addQuarters(1).months).toEqual(0); //TODO:
195
196 // b = moment([2010, 0, 31]).add({ M: 1 });
197 // c = moment([2010, 1, 28]).subtract({ M: 1 });
198 // d = moment([2010, 1, 28]).subtract({ Q: 1 });
199
200 // assert.equal(b.month(), 1, 'add month, jan 31st to feb 28th');
201 try expect(time.DateTime.init(2010, 0, 30, 0, 0, 0, 0).addMonths(1).toISOString()).toEqualString("2010-02-28T00:00:00Z");
202 try expect(time.DateTime.init(2010, 0, 30, 0, 0, 0, 0).addMonths(1).months).toEqual(1);
203 try expect(time.DateTime.init(2009, 11, 30, 0, 0, 0, 0).addMonths(2).toISOString()).toEqualString("2010-02-28T00:00:00Z");
204 try expect(time.DateTime.init(2009, 11, 30, 0, 0, 0, 0).addMonths(2).months).toEqual(1);
205 // assert.equal(b.date(), 28, 'add month, jan 31st to feb 28th');
206 try expect(time.DateTime.init(2010, 0, 30, 0, 0, 0, 0).addMonths(1).days).toEqual(27);
207 // assert.equal(c.month(), 0, 'subtract month, feb 28th to jan 28th');
208 // try expect(time.DateTime.init(2010, 1, 27, 0, 0, 0, 0).subMonths(1).months).toEqual(0);
209 // assert.equal(c.date(), 28, 'subtract month, feb 28th to jan 28th');
210 // try expect(time.DateTime.init(2010, 1, 27, 0, 0, 0, 0).subMonths(1).days).toEqual(28);
211 // assert.equal(d.month(), 10, 'subtract quarter, feb 28th 2010 to nov 28th 2009');
212 // try expect(time.DateTime.init(2010, 1, 27, 0, 0, 0, 0).subQuarters(1).months).toEqual(10);
213 // assert.equal(d.date(), 28, 'subtract quarter, feb 28th 2010 to nov 28th 2009');
214 // try expect(time.DateTime.init(2010, 1, 27, 0, 0, 0, 0).subQuarters(1).days).toEqual(28);
215 // assert.equal(d.year(), 2009, 'subtract quarter, feb 28th 2010 to nov 28th 2009');
216 // try expect(time.DateTime.init(2010, 1, 27, 0, 0, 0, 0).subQuarters(1).years).toEqual(2009);
217}
time.zig+34-12
......@@ -24,14 +24,15 @@ pub const DateTime = struct {
2424 }
2525
2626 /// Caller asserts that this is > epoch
27 pub fn init(year: u16, month: u16, day: u16, hr: u16, min: u16, sec: u16) Self {
27 pub fn init(year: u16, month: u16, day: u16, hr: u16, min: u16, sec: u16, ms: u16) Self {
2828 return epoch_unix
2929 .addYears(year - epoch_unix.years)
3030 .addMonths(month)
3131 .addDays(day)
3232 .addHours(hr)
3333 .addMins(min)
34 .addSecs(sec);
34 .addSecs(sec)
35 .addMs(ms);
3536 }
3637
3738 pub fn now() Self {
......@@ -49,6 +50,16 @@ pub const DateTime = struct {
4950 .timezone = .UTC,
5051 };
5152
53 pub fn toISOString(self: Self) [20]u8 {
54 // "2021-10-21T23:20:30Z"
55 var result: [20]u8 = @splat(0);
56 var fbs = std.io.fixedBufferStream(&result);
57 self.format("YYYY-MM-DD HH:mm:ss", .{}, fbs.writer()) catch unreachable;
58 result[10] = 'T';
59 result[19] = 'Z';
60 return result;
61 }
62
5263 pub fn eql(self: Self, other: Self) bool {
5364 return self.ms == other.ms and
5465 self.seconds == other.seconds and
......@@ -120,7 +131,7 @@ pub const DateTime = struct {
120131 {
121132 const month_len = result.daysThisMonth();
122133 if (result.days + input > month_len) {
123 const left = month_len - result.days;
134 const left = month_len -| result.days;
124135 input -= left;
125136 result.months += 1;
126137 result.days = 0;
......@@ -146,15 +157,21 @@ pub const DateTime = struct {
146157 return result;
147158 }
148159
160 pub fn addWeeks(self: Self, count: u64) Self {
161 if (count == 0) return self;
162 return self.addDays(7).addWeeks(count - 1);
163 }
164
149165 pub fn addMonths(self: Self, count: u64) Self {
150166 if (count == 0) return self;
151167 var result = self;
152168 var input = count;
153 while (input > 0) {
154 const new = result.addDays(result.daysThisMonth());
155 result = new;
156 input -= 1;
157 }
169 result.years += @intCast(input / 12);
170 input %= 12;
171 result.months += @intCast(input);
172 if (result.months >= 12) result.years += 1;
173 result.months %= 12;
174 result.days = @min(result.days, result.daysInMonth(result.months) - 1);
158175 return result;
159176 }
160177
......@@ -166,6 +183,11 @@ pub const DateTime = struct {
166183 return result;
167184 }
168185
186 pub fn addQuarters(self: Self, count: u64) Self {
187 if (count == 0) return self;
188 return self.addMonths(3).addQuarters(count - 1);
189 }
190
169191 pub fn isLeapYear(self: Self) bool {
170192 return time.isLeapYear(self.years);
171193 }
......@@ -199,10 +221,10 @@ pub const DateTime = struct {
199221 pub fn toUnixMilli(self: Self) u64 {
200222 var res: u64 = 0;
201223 res += self.ms;
202 res += @as(u64, self.seconds) * std.time.ms_per_s;
203 res += @as(u64, self.minutes) * std.time.ms_per_min;
204 res += @as(u64, self.hours) * std.time.ms_per_hour;
205 res += self.daysSinceEpoch() * std.time.ms_per_day;
224 res += @as(u64, self.seconds) * ms_per_s;
225 res += @as(u64, self.minutes) * ms_per_min;
226 res += @as(u64, self.hours) * ms_per_hour;
227 res += self.daysSinceEpoch() * ms_per_day;
206228 return res;
207229 }
208230
zig.mod+2
......@@ -6,3 +6,5 @@ description: A date and time parsing and formatting library for Zig.
66min_zig_version: 0.11.0-dev.1681+0bb178bbb
77dependencies:
88 - src: git https://github.com/nektro/zig-extras
9root_dependencies:
10 - src: git https://github.com/nektro/zig-expect