pub const Timestamp = struct nanoseconds: i96pub fn now(io: Io, clock: Clock) Io.Timestamppub fn durationTo(from: Timestamp, to: Timestamp) Durationpub fn addDuration(from: Timestamp, duration: Duration) Timestamppub fn subDuration(from: Timestamp, duration: Duration) Timestamppub fn withClock(t: Timestamp, clock: Clock) Clock.Timestamppub fn fromNanoseconds(x: i96) Timestamppub fn toMicroseconds(t: Timestamp) i64pub fn toMilliseconds(t: Timestamp) i64pub fn toSeconds(t: Timestamp) i64pub fn toNanoseconds(t: Timestamp) i96pub fn formatNumber(t: Timestamp, w: *std.Io.Writer, n: std.fmt.Number) std.Io.Writer.Error!voidpub fn untilNow(t: Timestamp, io: Io, clock: Clock) DurationResolution is determined by Clock.resolution which may be 0 if the clock is unsupported.
pub fn compare(lhs: Timestamp, op: math.CompareOperator, rhs: Timestamp) bool1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079pub const Timestamp = struct {
nanoseconds: i96,
pub fn now(io: Io, clock: Clock) Io.Timestamp {
return io.vtable.now(io.userdata, clock);
}
pub const zero: Timestamp = .{ .nanoseconds = 0 };
pub fn durationTo(from: Timestamp, to: Timestamp) Duration {
return .{ .nanoseconds = to.nanoseconds - from.nanoseconds };
}
pub fn addDuration(from: Timestamp, duration: Duration) Timestamp {
return .{ .nanoseconds = from.nanoseconds + duration.nanoseconds };
}
pub fn subDuration(from: Timestamp, duration: Duration) Timestamp {
return .{ .nanoseconds = from.nanoseconds - duration.nanoseconds };
}
pub fn withClock(t: Timestamp, clock: Clock) Clock.Timestamp {
return .{ .raw = t, .clock = clock };
}
pub fn fromNanoseconds(x: i96) Timestamp {
return .{ .nanoseconds = x };
}
pub fn toMicroseconds(t: Timestamp) i64 {
return @intCast(@divTrunc(t.nanoseconds, std.time.ns_per_us));
}
pub fn toMilliseconds(t: Timestamp) i64 {
return @intCast(@divTrunc(t.nanoseconds, std.time.ns_per_ms));
}
pub fn toSeconds(t: Timestamp) i64 {
return @intCast(@divTrunc(t.nanoseconds, std.time.ns_per_s));
}
pub fn toNanoseconds(t: Timestamp) i96 {
return t.nanoseconds;
}
pub fn formatNumber(t: Timestamp, w: *std.Io.Writer, n: std.fmt.Number) std.Io.Writer.Error!void {
return w.printInt(t.nanoseconds, n.mode.base() orelse 10, n.case, .{
.precision = n.precision,
.width = n.width,
.alignment = n.alignment,
.fill = n.fill,
});
}
/// Resolution is determined by `Clock.resolution` which may be 0 if
/// the clock is unsupported.
pub fn untilNow(t: Timestamp, io: Io, clock: Clock) Duration {
const now_ts = clock.now(io);
return t.durationTo(now_ts);
}
pub fn compare(lhs: Timestamp, op: math.CompareOperator, rhs: Timestamp) bool {
return math.compare(lhs.nanoseconds, op, rhs.nanoseconds);
}
}