various tools I have been using throughout the years
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142const std = @import("std");
pub const PausableTimer = struct { start_time: std.Io.Timestamp, elapsed_before_pause: u64, paused: bool,
pub fn init(io: std.Io) PausableTimer { return .{ .start_time = std.Io.Timestamp.now(io, .awake), .elapsed_before_pause = 0, .paused = true, }; }
pub fn togglePause(self: *PausableTimer, io: std.Io) void { if (self.paused) { self.start_time = std.Io.Timestamp.now(io, .awake); self.paused = false; } else { const now = std.Io.Timestamp.now(io, .awake); self.elapsed_before_pause += @as(u64, @intCast(self.start_time.durationTo(now).nanoseconds)); self.paused = true; } }
pub fn reset(self: *PausableTimer, io: std.Io) void { self.elapsed_before_pause = 0; self.start_time = std.Io.Timestamp.now(io, .awake); self.paused = true; }
pub fn read(self: *PausableTimer, io: std.Io) u64 { if (self.paused) { return self.elapsed_before_pause; } else { const now = std.Io.Timestamp.now(io, .awake); return self.elapsed_before_pause + @as(u64, @intCast(self.start_time.durationTo(now).nanoseconds)); } }};