authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-12-17 18:21:48 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-12-17 18:21:48 -08:00
log8e356970bdf5b36e4b7d533deb1eb468ab542e8f
tree0582cd21676b71fd92145a16123a2fd4765e46fe
parentcd783ccfc1f17a23f96512d337b2d5d7b10787c7

DateTime: remove Self decl when type is not generic


1 files changed, 29 insertions(+), 31 deletions(-)

time.zig+29-31
......@@ -13,18 +13,16 @@ pub const DateTime = struct {
1313 years: u16,
1414 timezone: TimeZone,
1515
16 const Self = @This();
17
18 pub fn initUnixMs(unix: u64) Self {
16 pub fn initUnixMs(unix: u64) DateTime {
1917 return epoch_unix.addMs(unix);
2018 }
2119
22 pub fn initUnix(unix: u64) Self {
20 pub fn initUnix(unix: u64) DateTime {
2321 return epoch_unix.addSecs(unix);
2422 }
2523
2624 /// Caller asserts that this is > epoch
27 pub fn init(year: u16, month: u16, day: u16, hr: u16, min: u16, sec: u16, ms: u16) Self {
25 pub fn init(year: u16, month: u16, day: u16, hr: u16, min: u16, sec: u16, ms: u16) DateTime {
2826 return epoch_unix
2927 .addYears(year - epoch_unix.years)
3028 .addMonths(month)
......@@ -35,11 +33,11 @@ pub const DateTime = struct {
3533 .addMs(ms);
3634 }
3735
38 pub fn now() Self {
36 pub fn now() DateTime {
3937 return initUnixMs(@intCast(std.time.milliTimestamp()));
4038 }
4139
42 pub const epoch_unix = Self{
40 pub const epoch_unix = DateTime{
4341 .ms = 0,
4442 .seconds = 0,
4543 .minutes = 0,
......@@ -50,7 +48,7 @@ pub const DateTime = struct {
5048 .timezone = .UTC,
5149 };
5250
53 pub fn toISOString(self: Self) [20]u8 {
51 pub fn toISOString(self: DateTime) [20]u8 {
5452 // "2021-10-21T23:20:30Z"
5553 var result: [20]u8 = @splat(0);
5654 var fbs = std.io.fixedBufferStream(&result);
......@@ -60,7 +58,7 @@ pub const DateTime = struct {
6058 return result;
6159 }
6260
63 pub fn eql(self: Self, other: Self) bool {
61 pub fn eql(self: DateTime, other: DateTime) bool {
6462 return self.ms == other.ms and
6563 self.seconds == other.seconds and
6664 self.minutes == other.minutes and
......@@ -72,35 +70,35 @@ pub const DateTime = struct {
7270 self.weekday == other.weekday;
7371 }
7472
75 pub fn addMs(self: Self, count: u64) Self {
73 pub fn addMs(self: DateTime, count: u64) DateTime {
7674 if (count == 0) return self;
7775 var result = self;
7876 result.ms += @intCast(count % 1000);
7977 return result.addSecs(count / 1000);
8078 }
8179
82 pub fn addSecs(self: Self, count: u64) Self {
80 pub fn addSecs(self: DateTime, count: u64) DateTime {
8381 if (count == 0) return self;
8482 var result = self;
8583 result.seconds += @intCast(count % 60);
8684 return result.addMins(count / 60);
8785 }
8886
89 pub fn addMins(self: Self, count: u64) Self {
87 pub fn addMins(self: DateTime, count: u64) DateTime {
9088 if (count == 0) return self;
9189 var result = self;
9290 result.minutes += @intCast(count % 60);
9391 return result.addHours(count / 60);
9492 }
9593
96 pub fn addHours(self: Self, count: u64) Self {
94 pub fn addHours(self: DateTime, count: u64) DateTime {
9795 if (count == 0) return self;
9896 var result = self;
9997 result.hours += @intCast(count % 24);
10098 return result.addDays(count / 24);
10199 }
102100
103 pub fn addDays(self: Self, count: u64) Self {
101 pub fn addDays(self: DateTime, count: u64) DateTime {
104102 if (count == 0) return self;
105103 var result = self;
106104 var input = count;
......@@ -157,12 +155,12 @@ pub const DateTime = struct {
157155 return result;
158156 }
159157
160 pub fn addWeeks(self: Self, count: u64) Self {
158 pub fn addWeeks(self: DateTime, count: u64) DateTime {
161159 if (count == 0) return self;
162160 return self.addDays(7).addWeeks(count - 1);
163161 }
164162
165 pub fn addMonths(self: Self, count: u64) Self {
163 pub fn addMonths(self: DateTime, count: u64) DateTime {
166164 if (count == 0) return self;
167165 var result = self;
168166 var input = count;
......@@ -175,7 +173,7 @@ pub const DateTime = struct {
175173 return result;
176174 }
177175
178 pub fn addYears(self: Self, count: u64) Self {
176 pub fn addYears(self: DateTime, count: u64) DateTime {
179177 var result = self;
180178 for (0..count) |_| {
181179 result = result.addDays(result.daysThisYear());
......@@ -183,28 +181,28 @@ pub const DateTime = struct {
183181 return result;
184182 }
185183
186 pub fn addQuarters(self: Self, count: u64) Self {
184 pub fn addQuarters(self: DateTime, count: u64) DateTime {
187185 if (count == 0) return self;
188186 return self.addMonths(3).addQuarters(count - 1);
189187 }
190188
191 pub fn isLeapYear(self: Self) bool {
189 pub fn isLeapYear(self: DateTime) bool {
192190 return time.isLeapYear(self.years);
193191 }
194192
195 pub fn daysThisYear(self: Self) u16 {
193 pub fn daysThisYear(self: DateTime) u16 {
196194 return time.daysInYear(self.years);
197195 }
198196
199 pub fn daysThisMonth(self: Self) u16 {
197 pub fn daysThisMonth(self: DateTime) u16 {
200198 return self.daysInMonth(self.months);
201199 }
202200
203 fn daysInMonth(self: Self, month: u16) u16 {
201 fn daysInMonth(self: DateTime, month: u16) u16 {
204202 return time.daysInMonth(self.years, month);
205203 }
206204
207 pub fn dayOfThisYear(self: Self) u16 {
205 pub fn dayOfThisYear(self: DateTime) u16 {
208206 var ret: u16 = 0;
209207 for (0..self.months) |item| {
210208 ret += self.daysInMonth(@intCast(item));
......@@ -213,12 +211,12 @@ pub const DateTime = struct {
213211 return ret;
214212 }
215213
216 pub fn toUnix(self: Self) u64 {
214 pub fn toUnix(self: DateTime) u64 {
217215 const x = self.toUnixMilli();
218216 return x / 1000;
219217 }
220218
221 pub fn toUnixMilli(self: Self) u64 {
219 pub fn toUnixMilli(self: DateTime) u64 {
222220 var res: u64 = 0;
223221 res += self.ms;
224222 res += @as(u64, self.seconds) * ms_per_s;
......@@ -228,7 +226,7 @@ pub const DateTime = struct {
228226 return res;
229227 }
230228
231 fn daysSinceEpoch(self: Self) u64 {
229 fn daysSinceEpoch(self: DateTime) u64 {
232230 var res: u64 = 0;
233231 res += self.days;
234232 for (0..self.years - epoch_unix.years) |i| res += time.daysInYear(@intCast(i));
......@@ -237,7 +235,7 @@ pub const DateTime = struct {
237235 }
238236
239237 /// fmt is based on https://momentjs.com/docs/#/displaying/format/
240 pub fn format(self: Self, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) !void {
238 pub fn format(self: DateTime, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) !void {
241239 _ = options;
242240
243241 if (fmt.len == 0) @compileError("DateTime: format string can't be empty");
......@@ -344,7 +342,7 @@ pub const DateTime = struct {
344342 }
345343 }
346344
347 pub fn formatAlloc(self: Self, alloc: std.mem.Allocator, comptime fmt: string) !string {
345 pub fn formatAlloc(self: DateTime, alloc: std.mem.Allocator, comptime fmt: string) !string {
348346 var list = std.ArrayList(u8).init(alloc);
349347 defer list.deinit();
350348
......@@ -404,18 +402,18 @@ pub const DateTime = struct {
404402 X, // unix
405403 };
406404
407 pub fn since(self: Self, other_in_the_past: Self) Duration {
405 pub fn since(self: DateTime, other_in_the_past: DateTime) Duration {
408406 return Duration{
409407 .ms = self.toUnixMilli() - other_in_the_past.toUnixMilli(),
410408 };
411409 }
412410
413 pub fn era(self: Self) Era {
411 pub fn era(self: DateTime) Era {
414412 if (self.years >= 0) return .AD;
415413 @compileError("TODO");
416414 }
417415
418 pub fn weekday(self: Self) WeekDay {
416 pub fn weekday(self: DateTime) WeekDay {
419417 var i = self.daysSinceEpoch() % 7;
420418 var result = WeekDay.Thu; // weekday of epoch_unix
421419 while (i > 0) : (i -= 1) {