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 {...@@ -12,7 +12,6 @@ pub const DateTime = struct {
12 months: u16,12 months: u16,
13 years: u16,13 years: u16,
14 timezone: TimeZone,14 timezone: TimeZone,
15 weekday: WeekDay,
1615
17 const Self = @This();16 const Self = @This();
1817
...@@ -48,7 +47,6 @@ pub const DateTime = struct {...@@ -48,7 +47,6 @@ pub const DateTime = struct {
48 .months = 0,47 .months = 0,
49 .years = 1970,48 .years = 1970,
50 .timezone = .UTC,49 .timezone = .UTC,
51 .weekday = .Thu,
52 };50 };
5351
54 pub fn eql(self: Self, other: Self) bool {52 pub fn eql(self: Self, other: Self) bool {
...@@ -101,7 +99,6 @@ pub const DateTime = struct {...@@ -101,7 +99,6 @@ pub const DateTime = struct {
101 if (input >= year_len) {99 if (input >= year_len) {
102 result.years += 1;100 result.years += 1;
103 input -= year_len;101 input -= year_len;
104 result.incrementWeekday(year_len);
105 continue;102 continue;
106 }103 }
107 break;104 break;
...@@ -111,7 +108,6 @@ pub const DateTime = struct {...@@ -111,7 +108,6 @@ pub const DateTime = struct {
111 if (input >= month_len) {108 if (input >= month_len) {
112 result.months += 1;109 result.months += 1;
113 input -= month_len;110 input -= month_len;
114 result.incrementWeekday(month_len);
115111
116 if (result.months == 12) {112 if (result.months == 12) {
117 result.years += 1;113 result.years += 1;
...@@ -128,10 +124,8 @@ pub const DateTime = struct {...@@ -128,10 +124,8 @@ pub const DateTime = struct {
128 input -= left;124 input -= left;
129 result.months += 1;125 result.months += 1;
130 result.days = 0;126 result.days = 0;
131 result.incrementWeekday(left);
132 }127 }
133 result.days += @intCast(input);128 result.days += @intCast(input);
134 result.incrementWeekday(input);
135129
136 if (result.months == 12) {130 if (result.months == 12) {
137 result.years += 1;131 result.years += 1;
...@@ -175,13 +169,6 @@ pub const DateTime = struct {...@@ -175,13 +169,6 @@ pub const DateTime = struct {
175 return time.daysInMonth(self.years, month);169 return time.daysInMonth(self.years, month);
176 }170 }
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
185 pub fn dayOfThisYear(self: Self) u16 {172 pub fn dayOfThisYear(self: Self) u16 {
186 var ret: u16 = 0;173 var ret: u16 = 0;
187 for (0..self.months) |item| {174 for (0..self.months) |item| {
...@@ -252,13 +239,13 @@ pub const DateTime = struct {...@@ -252,13 +239,13 @@ pub const DateTime = struct {
252 .DDDo => try printOrdinal(writer, self.dayOfThisYear() + 1),239 .DDDo => try printOrdinal(writer, self.dayOfThisYear() + 1),
253 .DDDD => try writer.print("{:0>3}", .{self.dayOfThisYear() + 1}),240 .DDDD => try writer.print("{:0>3}", .{self.dayOfThisYear() + 1}),
254241
255 .d => try writer.print("{}", .{@intFromEnum(self.weekday)}),242 .d => try writer.print("{}", .{@intFromEnum(self.weekday())}),
256 .do => try printOrdinal(writer, @intFromEnum(self.weekday)),243 .do => try printOrdinal(writer, @intFromEnum(self.weekday())),
257 .dd => try writer.writeAll(@tagName(self.weekday)[0..2]),244 .dd => try writer.writeAll(@tagName(self.weekday())[0..2]),
258 .ddd => try writer.writeAll(@tagName(self.weekday)),245 .ddd => try writer.writeAll(@tagName(self.weekday())),
259 .dddd => try printLongName(writer, @intFromEnum(self.weekday), &[_]string{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }),246 .dddd => try printLongName(writer, @intFromEnum(self.weekday()), &[_]string{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }),
260 .e => try writer.print("{}", .{@intFromEnum(self.weekday)}),247 .e => try writer.print("{}", .{@intFromEnum(self.weekday())}),
261 .E => try writer.print("{}", .{@intFromEnum(self.weekday) + 1}),248 .E => try writer.print("{}", .{@intFromEnum(self.weekday()) + 1}),
262249
263 .w => try writer.print("{}", .{self.dayOfThisYear() / 7 + 1}),250 .w => try writer.print("{}", .{self.dayOfThisYear() / 7 + 1}),
264 .wo => try printOrdinal(writer, self.dayOfThisYear() / 7 + 1),251 .wo => try printOrdinal(writer, self.dayOfThisYear() / 7 + 1),
...@@ -391,6 +378,15 @@ pub const DateTime = struct {...@@ -391,6 +378,15 @@ pub const DateTime = struct {
391 if (self.years >= 0) return .AD;378 if (self.years >= 0) return .AD;
392 @compileError("TODO");379 @compileError("TODO");
393 }380 }
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 }
394};390};
395391
396pub const format = struct {392pub const format = struct {