authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-10 18:41:38 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-10 18:41:38 -08:00
log7e0739b5d4393b8c3322acda18d005eff182fc88
treec0c4cd7d87f2f0d0dff3b8c3ba56b2b8736f5969
parent2beae6fbe1baf4e563767dbe37c51acdae74894e

add nanoTimestamp() and milliTimestamp()


3 files changed, 26 insertions(+), 1 deletions(-)

licenses.txt+4
...@@ -3,3 +3,7 @@ MIT:...@@ -3,3 +3,7 @@ MIT:
3- This3- This
4- git https://github.com/nektro/zig-expect4- git https://github.com/nektro/zig-expect
5- git https://github.com/nektro/zig-extras5- git https://github.com/nektro/zig-extras
6- git https://github.com/nektro/zig-sys-linux
7
8Unspecified:
9- system_lib c
time.zig+21-1
...@@ -1,8 +1,15 @@...@@ -1,8 +1,15 @@
1const std = @import("std");1const std = @import("std");
2const builtin = @import("builtin");
2const string = []const u8;3const string = []const u8;
3const extras = @import("extras");4const extras = @import("extras");
5const sys_linux = @import("sys-linux");
4const time = @This();6const time = @This();
57
8const sys = switch (builtin.target.os.tag) {
9 .linux => sys_linux,
10 else => unreachable, // TODO:
11};
12
6pub const DateTime = struct {13pub 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 }
3542
36 pub fn now() DateTime {43 pub fn now() DateTime {
37 return initUnixMs(@intCast(std.time.milliTimestamp()));44 return initUnixMs(@intCast(milliTimestamp()));
38 }45 }
3946
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;
544pub const s_per_hour = s_per_min * 60;551pub const s_per_hour = s_per_min * 60;
545pub const s_per_day = s_per_hour * 24;552pub const s_per_day = s_per_hour * 24;
546pub const s_per_week = s_per_day * 7;553pub const s_per_week = s_per_day * 7;
554
555pub 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
564pub fn milliTimestamp() i64 {
565 return @as(i64, @intCast(@divFloor(nanoTimestamp(), ns_per_ms)));
566}
zig.mod+1
...@@ -6,5 +6,6 @@ description: A date and time parsing and formatting library for Zig....@@ -6,5 +6,6 @@ description: A date and time parsing and formatting library for Zig.
6min_zig_version: 0.11.0-dev.1681+0bb178bbb6min_zig_version: 0.11.0-dev.1681+0bb178bbb
7dependencies:7dependencies:
8 - src: git https://github.com/nektro/zig-extras8 - src: git https://github.com/nektro/zig-extras
9 - src: git https://github.com/nektro/zig-sys-linux
9root_dependencies:10root_dependencies:
10 - src: git https://github.com/nektro/zig-expect11 - src: git https://github.com/nektro/zig-expect