30 lines
723 B
Zig
30 lines
723 B
Zig
// zig fmt: off
|
|
const std = @import("std");
|
|
const Clock = @This();
|
|
|
|
timestamp: i128 = 0,
|
|
timestamp_limit: ?i128 = null,
|
|
|
|
pub fn sleep(self: *Clock, nanoseconds: u64) void {
|
|
const nanoseconds_i128: i128 = @intCast(nanoseconds);
|
|
const new_timestamp = self.timestamp + nanoseconds_i128;
|
|
|
|
if (self.timestamp_limit != null) {
|
|
while (true) {
|
|
self.timestamp = @min(self.timestamp_limit.?, new_timestamp);
|
|
|
|
if (self.timestamp < self.timestamp_limit.?) {
|
|
break;
|
|
}
|
|
|
|
std.time.sleep(std.time.ms_per_s * 100);
|
|
}
|
|
} else {
|
|
self.timestamp = new_timestamp;
|
|
}
|
|
}
|
|
|
|
pub fn nanoTimestamp(self: Clock) i128 {
|
|
return self.timestamp;
|
|
}
|