| ... | ... | @@ -12,7 +12,6 @@ pub const DateTime = struct { |
| 12 | 12 | months: u16, |
| 13 | 13 | years: u16, |
| 14 | 14 | timezone: TimeZone, |
| 15 | | weekday: WeekDay, |
| 16 | 15 | |
| 17 | 16 | const Self = @This(); |
| 18 | 17 | |
| ... | ... | @@ -48,7 +47,6 @@ pub const DateTime = struct { |
| 48 | 47 | .months = 0, |
| 49 | 48 | .years = 1970, |
| 50 | 49 | .timezone = .UTC, |
| 51 | | .weekday = .Thu, |
| 52 | 50 | }; |
| 53 | 51 | |
| 54 | 52 | pub fn eql(self: Self, other: Self) bool { |
| ... | ... | @@ -101,7 +99,6 @@ pub const DateTime = struct { |
| 101 | 99 | if (input >= year_len) { |
| 102 | 100 | result.years += 1; |
| 103 | 101 | input -= year_len; |
| 104 | | result.incrementWeekday(year_len); |
| 105 | 102 | continue; |
| 106 | 103 | } |
| 107 | 104 | break; |
| ... | ... | @@ -111,7 +108,6 @@ pub const DateTime = struct { |
| 111 | 108 | if (input >= month_len) { |
| 112 | 109 | result.months += 1; |
| 113 | 110 | input -= month_len; |
| 114 | | result.incrementWeekday(month_len); |
| 115 | 111 | |
| 116 | 112 | if (result.months == 12) { |
| 117 | 113 | result.years += 1; |
| ... | ... | @@ -128,10 +124,8 @@ pub const DateTime = struct { |
| 128 | 124 | input -= left; |
| 129 | 125 | result.months += 1; |
| 130 | 126 | result.days = 0; |
| 131 | | result.incrementWeekday(left); |
| 132 | 127 | } |
| 133 | 128 | result.days += @intCast(input); |
| 134 | | result.incrementWeekday(input); |
| 135 | 129 | |
| 136 | 130 | if (result.months == 12) { |
| 137 | 131 | result.years += 1; |
| ... | ... | @@ -175,13 +169,6 @@ pub const DateTime = struct { |
| 175 | 169 | return time.daysInMonth(self.years, month); |
| 176 | 170 | } |
| 177 | 171 | |
| 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 | 172 | pub fn dayOfThisYear(self: Self) u16 { |
| 186 | 173 | var ret: u16 = 0; |
| 187 | 174 | for (0..self.months) |item| { |
| ... | ... | @@ -252,13 +239,13 @@ pub const DateTime = struct { |
| 252 | 239 | .DDDo => try printOrdinal(writer, self.dayOfThisYear() + 1), |
| 253 | 240 | .DDDD => try writer.print("{:0>3}", .{self.dayOfThisYear() + 1}), |
| 254 | 241 | |
| 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}), |
| 262 | 249 | |
| 263 | 250 | .w => try writer.print("{}", .{self.dayOfThisYear() / 7 + 1}), |
| 264 | 251 | .wo => try printOrdinal(writer, self.dayOfThisYear() / 7 + 1), |
| ... | ... | @@ -391,6 +378,15 @@ pub const DateTime = struct { |
| 391 | 378 | if (self.years >= 0) return .AD; |
| 392 | 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 | }; |
| 395 | 391 | |
| 396 | 392 | pub const format = struct { |