diff --git a/licenses.txt b/licenses.txt index 830ca100462ce8e12dbcbaceb924f0edbd5eed3d..e2f2f1fdaf864b8edf7ce15bd34b87c9dd86504c 100644 --- a/licenses.txt +++ b/licenses.txt @@ -3,3 +3,7 @@ MIT: - This - git https://github.com/nektro/zig-expect - git https://github.com/nektro/zig-extras +- git https://github.com/nektro/zig-sys-linux + +Unspecified: +- system_lib c diff --git a/time.zig b/time.zig index 4fbc043a519ed0308de0476fd7c4baeb07b60c83..00567bb9d395926147dd66187743c6ea023b3530 100644 --- a/time.zig +++ b/time.zig @@ -1,8 +1,15 @@ const std = @import("std"); +const builtin = @import("builtin"); const string = []const u8; const extras = @import("extras"); +const sys_linux = @import("sys-linux"); const time = @This(); +const sys = switch (builtin.target.os.tag) { + .linux => sys_linux, + else => unreachable, // TODO: +}; + pub const DateTime = struct { ms: u16, seconds: u8, @@ -34,7 +41,7 @@ pub const DateTime = struct { } pub fn now() DateTime { - return initUnixMs(@intCast(std.time.milliTimestamp())); + return initUnixMs(@intCast(milliTimestamp())); } pub const epoch_unix = DateTime{ @@ -544,3 +551,16 @@ pub const s_per_min = 60; pub const s_per_hour = s_per_min * 60; pub const s_per_day = s_per_hour * 24; pub const s_per_week = s_per_day * 7; + +pub fn nanoTimestamp() i128 { + const ts = sys.clock_gettime(sys.CLOCK.REALTIME) catch return 0; + var result: i128 = 0; + result += ts.sec; + result *= ns_per_s; + result += ts.nsec; + return result; +} + +pub fn milliTimestamp() i64 { + return @as(i64, @intCast(@divFloor(nanoTimestamp(), ns_per_ms))); +} diff --git a/zig.mod b/zig.mod index e10d4c41310e98664514ef3cceffcfd6cc0b784c..7c26948a0776473010f4d90a4c77591d28d79998 100644 --- a/zig.mod +++ b/zig.mod @@ -6,5 +6,6 @@ description: A date and time parsing and formatting library for Zig. min_zig_version: 0.11.0-dev.1681+0bb178bbb dependencies: - src: git https://github.com/nektro/zig-extras + - src: git https://github.com/nektro/zig-sys-linux root_dependencies: - src: git https://github.com/nektro/zig-expect