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:
33- This
44- git https://github.com/nektro/zig-expect
55- 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 @@
11const std = @import("std");
2const builtin = @import("builtin");
23const string = []const u8;
34const extras = @import("extras");
5const sys_linux = @import("sys-linux");
46const time = @This();
57
8const sys = switch (builtin.target.os.tag) {
9 .linux => sys_linux,
10 else => unreachable, // TODO:
11};
12
613pub const DateTime = struct {
714 ms: u16,
815 seconds: u8,
......@@ -34,7 +41,7 @@ pub const DateTime = struct {
3441 }
3542
3643 pub fn now() DateTime {
37 return initUnixMs(@intCast(std.time.milliTimestamp()));
44 return initUnixMs(@intCast(milliTimestamp()));
3845 }
3946
4047 pub const epoch_unix = DateTime{
......@@ -544,3 +551,16 @@ pub const s_per_min = 60;
544551pub const s_per_hour = s_per_min * 60;
545552pub const s_per_day = s_per_hour * 24;
546553pub 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.
66min_zig_version: 0.11.0-dev.1681+0bb178bbb
77dependencies:
88 - src: git https://github.com/nektro/zig-extras
9 - src: git https://github.com/nektro/zig-sys-linux
910root_dependencies:
1011 - src: git https://github.com/nektro/zig-expect