mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 23:23:28 +00:00
Improve clocks
There's a flaky performance test that I wanted to fix (1). This led to a couple
changes.
1 - Add timestamp() and milliTimestamp() to datetime.zig. Reduce some code
duplication and use better clock_ids where available
2 - Change Performance API to use milliTimestamp and store a u64 instead of a
f64. While the spec says a float, Firefox deals with u64 and implicit
conversion is always available. Makes our APIs simpler.
(1) - https://github.com/lightpanda-io/browser/actions/runs/17313296490/job/49151366798#step:4:131
This commit is contained in:
@@ -502,6 +502,28 @@ pub const DateTime = struct {
|
||||
}
|
||||
};
|
||||
|
||||
pub fn timestamp() u32 {
|
||||
const ts = timespec();
|
||||
return @intCast(ts.sec);
|
||||
}
|
||||
|
||||
pub fn milliTimestamp() u64 {
|
||||
const ts = timespec();
|
||||
return @as(u64, @intCast(ts.sec)) * 1000 + @as(u64, @intCast(@divTrunc(ts.nsec, 1_000_000)));
|
||||
}
|
||||
|
||||
fn timespec() std.posix.timespec {
|
||||
const clock_id = switch (@import("builtin").os.tag) {
|
||||
.freebsd, .dragonfly => std.posix.CLOCK.MONOTONIC_FAST,
|
||||
.macos, .ios, .tvos, .watchos, .visionos => std.posix.CLOCK.UPTIME_RAW, // continues counting while suspended
|
||||
.linux => std.posix.CLOCK.BOOTTIME, // continues counting while suspended
|
||||
else => std.posix.CLOCK.MONOTONIC,
|
||||
};
|
||||
// we don't currently support platforms where, at the very least,
|
||||
// posix.CLOCK.MONOTONIC wouldn't be available.
|
||||
return std.posix.clock_gettime(clock_id) catch unreachable;
|
||||
}
|
||||
|
||||
fn writeDate(into: []u8, date: Date) u8 {
|
||||
var buf: []u8 = undefined;
|
||||
// cast year to a u16 so it doesn't insert a sign
|
||||
|
||||
Reference in New Issue
Block a user