Skip to content

Commit

Permalink
address FObersteiner feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
clickingbuttons committed Apr 9, 2024
1 parent 423cb31 commit 25dbcb5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
12 changes: 6 additions & 6 deletions lib/std/date_time.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
30 changes: 15 additions & 15 deletions lib/std/time.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 25dbcb5

Please sign in to comment.