diff --git a/lib/std/date_time.zig b/lib/std/date_time.zig index 97acb9277ab1..9dac8c1225b9 100644 --- a/lib/std/date_time.zig +++ b/lib/std/date_time.zig @@ -12,24 +12,24 @@ pub fn DateTime(comptime DateT: type, comptime TimeT: type) type { /// 9 = nanoseconds pub const EpochSubseconds = std.meta.Int( @typeInfo(Date.EpochDays).Int.signedness, - @typeInfo(Date.EpochDays).Int.bits + std.math.log2_int_ceil(usize, Time.fs_per_day), + @typeInfo(Date.EpochDays).Int.bits + std.math.log2_int_ceil(usize, Time.subseconds_per_day), ); const Self = @This(); - /// New date time from fractional epoch seconds. + /// New date time from fractional seconds since `Date.epoch`. pub fn fromEpoch(subseconds: EpochSubseconds, time_opts: Time.Options) Self { - const days = @divFloor(subseconds, s_per_day * Time.fs_per_s); + const days = @divFloor(subseconds, s_per_day * Time.subseconds_per_s); const new_date = Date.fromEpoch(@intCast(days)); - const day_seconds = std.math.comptimeMod(subseconds, s_per_day * Time.fs_per_s); + const day_seconds = std.math.comptimeMod(subseconds, s_per_day * Time.subseconds_per_s); const new_time = Time.fromDaySeconds(day_seconds, time_opts); return .{ .date = new_date, .time = new_time }; } - /// Returns fractional epoch seconds. + /// Returns fractional seconds since `Date.epoch`. pub fn toEpoch(self: Self) EpochSubseconds { var res: EpochSubseconds = 0; - res += @as(EpochSubseconds, self.date.toEpoch()) * s_per_day * Time.fs_per_s; + res += @as(EpochSubseconds, self.date.toEpoch()) * s_per_day * Time.subseconds_per_s; res += self.time.toDaySeconds(); return res; } diff --git a/lib/std/time.zig b/lib/std/time.zig index 817a6af8562c..99e2e4562baa 100644 --- a/lib/std/time.zig +++ b/lib/std/time.zig @@ -367,27 +367,27 @@ pub fn Time(precision_: comptime_int, comptime has_offset: bool) type { const Self = @This(); pub const precision = precision_; - pub const fs_per_s = multiplier; - pub const fs_per_min = 60 * fs_per_s; - pub const fs_per_hour = 60 * fs_per_min; - pub const fs_per_day = 24 * fs_per_hour; + pub const subseconds_per_s = multiplier; + pub const subseconds_per_min = 60 * subseconds_per_s; + pub const subseconds_per_hour = 60 * subseconds_per_min; + pub const subseconds_per_day = 24 * subseconds_per_hour; pub const Options = struct { offset: IMinutes = 0, }; pub fn fromDaySeconds(seconds: DaySubseconds, options: Options) Self { - const fs_offset = @as(IDaySubseconds, options.offset) * fs_per_min + seconds; - var fs = std.math.comptimeMod(fs_offset, fs_per_day); + const fs_offset = @as(IDaySubseconds, options.offset) * subseconds_per_min + seconds; + var fs = std.math.comptimeMod(fs_offset, subseconds_per_day); - const hour: Hour = @intCast(@divFloor(fs, fs_per_hour)); - fs -= @as(DaySubseconds, @intCast(hour)) * fs_per_hour; + const hour: Hour = @intCast(@divFloor(fs, subseconds_per_hour)); + fs -= @as(DaySubseconds, @intCast(hour)) * subseconds_per_hour; - const minute: Minute = @intCast(@divFloor(fs, fs_per_min)); - fs -= @as(DaySubseconds, @intCast(minute)) * fs_per_min; + const minute: Minute = @intCast(@divFloor(fs, subseconds_per_min)); + fs -= @as(DaySubseconds, @intCast(minute)) * subseconds_per_min; - const second: Second = @intCast(@divFloor(fs, fs_per_s)); - fs -= @as(DaySubseconds, @intCast(second)) * fs_per_s; + const second: Second = @intCast(@divFloor(fs, subseconds_per_s)); + fs -= @as(DaySubseconds, @intCast(second)) * subseconds_per_s; return .{ .hour = hour, @@ -404,9 +404,9 @@ pub fn Time(precision_: comptime_int, comptime has_offset: bool) type { pub fn toDaySecondsZone(self: Self, zone: IMinutes) DaySubseconds { var sec = @as(IDaySubseconds, zone) * 60 * multiplier; - sec += @as(IDaySubseconds, self.hour) * fs_per_hour; - sec += @as(IDaySubseconds, self.minute) * fs_per_min; - sec += @as(IDaySubseconds, self.second) * fs_per_s; + sec += @as(IDaySubseconds, self.hour) * subseconds_per_hour; + sec += @as(IDaySubseconds, self.minute) * subseconds_per_min; + sec += @as(IDaySubseconds, self.second) * subseconds_per_s; sec += @as(IDaySubseconds, self.subsecond); return std.math.comptimeMod(sec, s_per_day * multiplier);