| ... | @@ -1,8 +1,15 @@ | ... | @@ -1,8 +1,15 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| | 2 | const builtin = @import("builtin"); |
| 2 | const string = []const u8; | 3 | const string = []const u8; |
| 3 | const extras = @import("extras"); | 4 | const extras = @import("extras"); |
| | 5 | const sys_linux = @import("sys-linux"); |
| 4 | const time = @This(); | 6 | const time = @This(); |
| 5 | | 7 | |
| | 8 | const sys = switch (builtin.target.os.tag) { |
| | 9 | .linux => sys_linux, |
| | 10 | else => unreachable, // TODO: |
| | 11 | }; |
| | 12 | |
| 6 | pub const DateTime = struct { | 13 | pub const DateTime = struct { |
| 7 | ms: u16, | 14 | ms: u16, |
| 8 | seconds: u8, | 15 | seconds: u8, |
| ... | @@ -34,7 +41,7 @@ pub const DateTime = struct { | ... | @@ -34,7 +41,7 @@ pub const DateTime = struct { |
| 34 | } | 41 | } |
| 35 | | 42 | |
| 36 | pub fn now() DateTime { | 43 | pub fn now() DateTime { |
| 37 | return initUnixMs(@intCast(std.time.milliTimestamp())); | 44 | return initUnixMs(@intCast(milliTimestamp())); |
| 38 | } | 45 | } |
| 39 | | 46 | |
| 40 | pub const epoch_unix = DateTime{ | 47 | pub const epoch_unix = DateTime{ |
| ... | @@ -544,3 +551,16 @@ pub const s_per_min = 60; | ... | @@ -544,3 +551,16 @@ pub const s_per_min = 60; |
| 544 | pub const s_per_hour = s_per_min * 60; | 551 | pub const s_per_hour = s_per_min * 60; |
| 545 | pub const s_per_day = s_per_hour * 24; | 552 | pub const s_per_day = s_per_hour * 24; |
| 546 | pub const s_per_week = s_per_day * 7; | 553 | pub const s_per_week = s_per_day * 7; |
| | 554 | |
| | 555 | pub fn nanoTimestamp() i128 { |
| | 556 | const ts = sys.clock_gettime(sys.CLOCK.REALTIME) catch return 0; |
| | 557 | var result: i128 = 0; |
| | 558 | result += ts.sec; |
| | 559 | result *= ns_per_s; |
| | 560 | result += ts.nsec; |
| | 561 | return result; |
| | 562 | } |
| | 563 | |
| | 564 | pub fn milliTimestamp() i64 { |
| | 565 | return @as(i64, @intCast(@divFloor(nanoTimestamp(), ns_per_ms))); |
| | 566 | } |