| ... | @@ -19,7 +19,7 @@ pub const DateTime = struct { | ... | @@ -19,7 +19,7 @@ pub const DateTime = struct { |
| 19 | days: u8, | 19 | days: u8, |
| 20 | months: u8, | 20 | months: u8, |
| 21 | years: u16, | 21 | years: u16, |
| 22 | timezone: TimeZone, | 22 | z_offset: i8, |
| 23 | | 23 | |
| 24 | pub fn initUnixMs(unix: u64) DateTime { | 24 | pub fn initUnixMs(unix: u64) DateTime { |
| 25 | return epoch_unix.addMs(unix); | 25 | return epoch_unix.addMs(unix); |
| ... | @@ -53,7 +53,7 @@ pub const DateTime = struct { | ... | @@ -53,7 +53,7 @@ pub const DateTime = struct { |
| 53 | .days = 0, | 53 | .days = 0, |
| 54 | .months = 0, | 54 | .months = 0, |
| 55 | .years = 1970, | 55 | .years = 1970, |
| 56 | .timezone = .UTC, | 56 | .z_offset = 0, |
| 57 | }; | 57 | }; |
| 58 | | 58 | |
| 59 | pub fn toISOString(self: DateTime) [20]u8 { | 59 | pub fn toISOString(self: DateTime) [20]u8 { |
| ... | @@ -220,8 +220,7 @@ pub const DateTime = struct { | ... | @@ -220,8 +220,7 @@ pub const DateTime = struct { |
| 220 | } | 220 | } |
| 221 | | 221 | |
| 222 | pub fn toUnix(self: DateTime) u64 { | 222 | pub fn toUnix(self: DateTime) u64 { |
| 223 | const x = self.toUnixMilli(); | 223 | return self.toUnixMilli() / 1000; |
| 224 | return x / 1000; | | |
| 225 | } | 224 | } |
| 226 | | 225 | |
| 227 | pub fn toUnixMilli(self: DateTime) u64 { | 226 | pub fn toUnixMilli(self: DateTime) u64 { |
| ... | @@ -231,6 +230,7 @@ pub const DateTime = struct { | ... | @@ -231,6 +230,7 @@ pub const DateTime = struct { |
| 231 | res += @as(u64, self.minutes) * ms_per_min; | 230 | res += @as(u64, self.minutes) * ms_per_min; |
| 232 | res += @as(u64, self.hours) * ms_per_hour; | 231 | res += @as(u64, self.hours) * ms_per_hour; |
| 233 | res += self.daysSinceEpoch() * ms_per_day; | 232 | res += self.daysSinceEpoch() * ms_per_day; |
| | 233 | res = extras.safeAdd(res, @as(i32, -self.z_offset) * 15 * ms_per_min); |
| 234 | return res; | 234 | return res; |
| 235 | } | 235 | } |
| 236 | | 236 | |
| ... | @@ -249,6 +249,19 @@ pub const DateTime = struct { | ... | @@ -249,6 +249,19 @@ pub const DateTime = struct { |
| 249 | return res; | 249 | return res; |
| 250 | } | 250 | } |
| 251 | | 251 | |
| | 252 | /// the timezone offset is stored as a signed amount of 15-min increments. |
| | 253 | /// the structure of the return value is .{ sign, hrs, mins }. |
| | 254 | /// 'sign' will be either '+' or '-' |
| | 255 | pub fn tz_parts(self: DateTime) [3]u8 { |
| | 256 | const o = self.z_offset; |
| | 257 | const a = @abs(o); |
| | 258 | return .{ |
| | 259 | if (o < 0) '-' else '+', |
| | 260 | a / 4, |
| | 261 | (a % 4) * 15, |
| | 262 | }; |
| | 263 | } |
| | 264 | |
| 252 | /// fmt is based on https://momentjs.com/docs/#/displaying/format/ | 265 | /// fmt is based on https://momentjs.com/docs/#/displaying/format/ |
| 253 | pub fn formatFmt(self: DateTime, comptime fmt: string, writer: anytype) !void { | 266 | pub fn formatFmt(self: DateTime, comptime fmt: string, writer: anytype) !void { |
| 254 | if (fmt.len == 0) @compileError("DateTime: format string can't be empty"); | 267 | if (fmt.len == 0) @compileError("DateTime: format string can't be empty"); |
| ... | @@ -325,9 +338,8 @@ pub const DateTime = struct { | ... | @@ -325,9 +338,8 @@ pub const DateTime = struct { |
| 325 | .SS => try writer.print("{:0>2}", .{self.ms / 10}), | 338 | .SS => try writer.print("{:0>2}", .{self.ms / 10}), |
| 326 | .SSS => try writer.print("{:0>3}", .{self.ms}), | 339 | .SSS => try writer.print("{:0>3}", .{self.ms}), |
| 327 | | 340 | |
| 328 | .z => try writer.writeAll(@tagName(self.timezone)), | 341 | .Z => try writer.print("{c}{:0>2}:{:0>2}", toTuple(self.tz_parts())), |
| 329 | .Z => try writer.writeAll("+00:00"), | 342 | .ZZ => try writer.print("{c}{:0>2}{:0>2}", toTuple(self.tz_parts())), |
| 330 | .ZZ => try writer.writeAll("+0000"), | | |
| 331 | | 343 | |
| 332 | .x => try writer.print("{}", .{self.toUnixMilli()}), | 344 | .x => try writer.print("{}", .{self.toUnixMilli()}), |
| 333 | .X => try writer.print("{}", .{self.toUnix()}), | 345 | .X => try writer.print("{}", .{self.toUnix()}), |
| ... | @@ -358,7 +370,6 @@ pub const DateTime = struct { | ... | @@ -358,7 +370,6 @@ pub const DateTime = struct { |
| 358 | pub fn formatAlloc(self: DateTime, alloc: std.mem.Allocator, comptime fmt: string) !string { | 370 | pub fn formatAlloc(self: DateTime, alloc: std.mem.Allocator, comptime fmt: string) !string { |
| 359 | var list = std.Io.Writer.Allocating.init(alloc); | 371 | var list = std.Io.Writer.Allocating.init(alloc); |
| 360 | defer list.deinit(); | 372 | defer list.deinit(); |
| 361 | | | |
| 362 | try self.formatFmt(fmt, &list.writer); | 373 | try self.formatFmt(fmt, &list.writer); |
| 363 | return list.toOwnedSlice(); | 374 | return list.toOwnedSlice(); |
| 364 | } | 375 | } |
| ... | @@ -408,7 +419,6 @@ pub const DateTime = struct { | ... | @@ -408,7 +419,6 @@ pub const DateTime = struct { |
| 408 | S, // 0 1 ... 8 9 (second fraction) | 419 | S, // 0 1 ... 8 9 (second fraction) |
| 409 | SS, // 00 01 ... 98 99 | 420 | SS, // 00 01 ... 98 99 |
| 410 | SSS, // 000 001 ... 998 999 | 421 | SSS, // 000 001 ... 998 999 |
| 411 | z, // EST CST ... MST PST | | |
| 412 | Z, // -07:00 -06:00 ... +06:00 +07:00 | 422 | Z, // -07:00 -06:00 ... +06:00 +07:00 |
| 413 | ZZ, // -0700 -0600 ... +0600 +0700 | 423 | ZZ, // -0700 -0600 ... +0600 +0700 |
| 414 | x, // unix milli | 424 | x, // unix milli |
| ... | @@ -530,6 +540,12 @@ fn wrap(val: u16, at: u16) u16 { | ... | @@ -530,6 +540,12 @@ fn wrap(val: u16, at: u16) u16 { |
| 530 | return if (tmp == 0) at else tmp; | 540 | return if (tmp == 0) at else tmp; |
| 531 | } | 541 | } |
| 532 | | 542 | |
| | 543 | fn toTuple(array: anytype) @Tuple(&@as([array.len]type, @splat(std.meta.Child(@TypeOf(array))))) { |
| | 544 | var result: @Tuple(&@as([array.len]type, @splat(std.meta.Child(@TypeOf(array))))) = undefined; |
| | 545 | inline for (array, 0..) |v, i| result[i] = v; |
| | 546 | return result; |
| | 547 | } |
| | 548 | |
| 533 | pub const Duration = struct { | 549 | pub const Duration = struct { |
| 534 | ms: u64, | 550 | ms: u64, |
| 535 | }; | 551 | }; |