authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-03-09 11:57:54 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-03-09 11:57:54 -08:00
logde4ebc667a64df55f6e288f54c056d5ce15d3546
tree4f5f85b148d62c23a9d29d7a4de7d8228180eff7
parent16dd344b261d3aef0a7adc90200d5d656bb03a16

DateTime: make weekday calculated at runtime, closes #8


1 files changed, 16 insertions(+), 20 deletions(-)

time.zig+16-20
......@@ -12,7 +12,6 @@ pub const DateTime = struct {
1212 months: u16,
1313 years: u16,
1414 timezone: TimeZone,
15 weekday: WeekDay,
1615
1716 const Self = @This();
1817
......@@ -48,7 +47,6 @@ pub const DateTime = struct {
4847 .months = 0,
4948 .years = 1970,
5049 .timezone = .UTC,
51 .weekday = .Thu,
5250 };
5351
5452 pub fn eql(self: Self, other: Self) bool {
......@@ -101,7 +99,6 @@ pub const DateTime = struct {
10199 if (input >= year_len) {
102100 result.years += 1;
103101 input -= year_len;
104 result.incrementWeekday(year_len);
105102 continue;
106103 }
107104 break;
......@@ -111,7 +108,6 @@ pub const DateTime = struct {
111108 if (input >= month_len) {
112109 result.months += 1;
113110 input -= month_len;
114 result.incrementWeekday(month_len);
115111
116112 if (result.months == 12) {
117113 result.years += 1;
......@@ -128,10 +124,8 @@ pub const DateTime = struct {
128124 input -= left;
129125 result.months += 1;
130126 result.days = 0;
131 result.incrementWeekday(left);
132127 }
133128 result.days += @intCast(input);
134 result.incrementWeekday(input);
135129
136130 if (result.months == 12) {
137131 result.years += 1;
......@@ -175,13 +169,6 @@ pub const DateTime = struct {
175169 return time.daysInMonth(self.years, month);
176170 }
177171
178 fn incrementWeekday(self: *Self, count: u64) void {
179 var i = count % 7;
180 while (i > 0) : (i -= 1) {
181 self.weekday = self.weekday.next();
182 }
183 }
184
185172 pub fn dayOfThisYear(self: Self) u16 {
186173 var ret: u16 = 0;
187174 for (0..self.months) |item| {
......@@ -252,13 +239,13 @@ pub const DateTime = struct {
252239 .DDDo => try printOrdinal(writer, self.dayOfThisYear() + 1),
253240 .DDDD => try writer.print("{:0>3}", .{self.dayOfThisYear() + 1}),
254241
255 .d => try writer.print("{}", .{@intFromEnum(self.weekday)}),
256 .do => try printOrdinal(writer, @intFromEnum(self.weekday)),
257 .dd => try writer.writeAll(@tagName(self.weekday)[0..2]),
258 .ddd => try writer.writeAll(@tagName(self.weekday)),
259 .dddd => try printLongName(writer, @intFromEnum(self.weekday), &[_]string{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }),
260 .e => try writer.print("{}", .{@intFromEnum(self.weekday)}),
261 .E => try writer.print("{}", .{@intFromEnum(self.weekday) + 1}),
242 .d => try writer.print("{}", .{@intFromEnum(self.weekday())}),
243 .do => try printOrdinal(writer, @intFromEnum(self.weekday())),
244 .dd => try writer.writeAll(@tagName(self.weekday())[0..2]),
245 .ddd => try writer.writeAll(@tagName(self.weekday())),
246 .dddd => try printLongName(writer, @intFromEnum(self.weekday()), &[_]string{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }),
247 .e => try writer.print("{}", .{@intFromEnum(self.weekday())}),
248 .E => try writer.print("{}", .{@intFromEnum(self.weekday()) + 1}),
262249
263250 .w => try writer.print("{}", .{self.dayOfThisYear() / 7 + 1}),
264251 .wo => try printOrdinal(writer, self.dayOfThisYear() / 7 + 1),
......@@ -391,6 +378,15 @@ pub const DateTime = struct {
391378 if (self.years >= 0) return .AD;
392379 @compileError("TODO");
393380 }
381
382 pub fn weekday(self: Self) WeekDay {
383 var i = self.daysSinceEpoch() % 7;
384 var result = WeekDay.Thu; // weekday of epoch_unix
385 while (i > 0) : (i -= 1) {
386 result = result.next();
387 }
388 return result;
389 }
394390};
395391
396392pub const format = struct {