From aa87fce593fd09c3f50358653d9079e1ee09b1ef Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Fri, 16 Oct 2026 13:57:21 -0500 Subject: [PATCH] Name the day-number bounds min_day_number and max_day_number Date.min_days and Date.max_days, the day numbers of the first and last dates a Date can hold, are now min_day_number and max_day_number, to match the DayNumber type they are values of. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LhqkVWKsh2rjnPEJ64ZUke --- README.md | 4 ++-- src/Date.zig | 56 +++++++++++++++++++++---------------------- src/Duration.zig | 2 +- src/fuzz.zig | 10 ++++---- tools/verify_days.zig | 26 ++++++++++---------- 5 files changed, 49 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index fd685d2..61eb2bf 100644 --- a/README.md +++ b/README.md @@ -1466,8 +1466,8 @@ against the seventeen the paper reports for the same sweep in C++ in The seed is the test runner's, so a failure replays exactly. -`zig build verify-days` converts every day number from `Date.min_days` to -`Date.max_days` — about 1.57 trillion, every day of every year a `Year` +`zig build verify-days` converts every day number from `Date.min_day_number` to +`Date.max_day_number` — about 1.57 trillion, every day of every year a `Year` holds — with both Joffe's algorithm and Hinnant's, converts each date back to a day number, and stops at the first disagreement or the first date that does not come back to where it started. It is always ReleaseFast and uses diff --git a/src/Date.zig b/src/Date.zig index ece665e..8f74908 100644 --- a/src/Date.zig +++ b/src/Date.zig @@ -94,33 +94,33 @@ pub const DayNumber = std.math.IntFittingRange( /// `DayNumber` is sized for 366 days a year, which is more than a year /// actually has, so its own extremes name years a `Year` cannot. These /// are the real bounds, and what a saturating conversion saturates to. -pub const min_days: DayNumber = (Date{ +pub const min_day_number: DayNumber = (Date{ .year = std.math.minInt(Year), .month = .Jan, .day = 1, }).toDayNumber(); -pub const max_days: DayNumber = (Date{ +pub const max_day_number: DayNumber = (Date{ .year = std.math.maxInt(Year), .month = .Dec, .day = 31, }).toDayNumber(); -test min_days { +test min_day_number { // The bounds are dates, and round trip like any other. try std.testing.expectEqual( Date{ .year = std.math.minInt(Year), .month = .Jan, .day = 1 }, - fromDayNumber(min_days), + fromDayNumber(min_day_number), ); try std.testing.expectEqual( Date{ .year = std.math.maxInt(Year), .month = .Dec, .day = 31 }, - fromDayNumber(max_days), + fromDayNumber(max_day_number), ); // And they are inside what `DayNumber` can hold, with room to spare, // because it is sized for a longer year than there is. - try std.testing.expect(min_days > std.math.minInt(DayNumber)); - try std.testing.expect(max_days < std.math.maxInt(DayNumber)); + try std.testing.expect(min_day_number > std.math.minInt(DayNumber)); + try std.testing.expect(max_day_number < std.math.maxInt(DayNumber)); } /// The first and last instants that fall on a date a `Date` can hold, as @@ -129,17 +129,17 @@ test min_days { /// Outside these an instant has no calendar date at all, so anything that /// converts one saturates rather than trapping. That keeps the conversion /// total, and makes the answer wrong in the same way the question was. -pub const min_seconds: i64 = @as(i64, min_days) * std.time.s_per_day; -pub const max_seconds: i64 = @as(i64, max_days) * std.time.s_per_day + (std.time.s_per_day - 1); +pub const min_seconds: i64 = @as(i64, min_day_number) * std.time.s_per_day; +pub const max_seconds: i64 = @as(i64, max_day_number) * std.time.s_per_day + (std.time.s_per_day - 1); test min_seconds { // The bounds are instants on the bounding dates. - try std.testing.expectEqual(min_days, daysFromSecondsSaturating(min_seconds)); - try std.testing.expectEqual(max_days, daysFromSecondsSaturating(max_seconds)); + try std.testing.expectEqual(min_day_number, daysFromSecondsSaturating(min_seconds)); + try std.testing.expectEqual(max_day_number, daysFromSecondsSaturating(max_seconds)); // And a second further out saturates rather than moving. - try std.testing.expectEqual(min_days, daysFromSecondsSaturating(min_seconds - 1)); - try std.testing.expectEqual(max_days, daysFromSecondsSaturating(max_seconds + 1)); + try std.testing.expectEqual(min_day_number, daysFromSecondsSaturating(min_seconds - 1)); + try std.testing.expectEqual(max_day_number, daysFromSecondsSaturating(max_seconds + 1)); } /// Returns the day number `seconds` seconds after the epoch falls in, @@ -156,8 +156,8 @@ test min_seconds { /// which is wrong in the same way that the question was. pub fn daysFromSecondsSaturating(seconds: i64) DayNumber { const days = @divFloor(seconds, std.time.s_per_day); - if (days < min_days) return min_days; - if (days > max_days) return max_days; + if (days < min_day_number) return min_day_number; + if (days > max_day_number) return max_day_number; return @intCast(days); } @@ -170,15 +170,15 @@ test daysFromSecondsSaturating { // Past the end of the calendar the answer stops moving rather than // wrapping or trapping, and what it stops at is a date. - try std.testing.expectEqual(max_days, daysFromSecondsSaturating(std.math.maxInt(i64))); - try std.testing.expectEqual(min_days, daysFromSecondsSaturating(std.math.minInt(i64))); + try std.testing.expectEqual(max_day_number, daysFromSecondsSaturating(std.math.maxInt(i64))); + try std.testing.expectEqual(min_day_number, daysFromSecondsSaturating(std.math.minInt(i64))); _ = fromDayNumber(daysFromSecondsSaturating(std.math.maxInt(i64))); _ = fromDayNumber(daysFromSecondsSaturating(std.math.minInt(i64))); } /// Returns the date that is `days` days after 1970-01-01; negative values /// give dates before the epoch. Asserts that `days` is in -/// `[min_days, max_days]`. +/// `[min_day_number, max_day_number]`. /// /// This is Ben Joffe's 64-bit algorithm. Like Hinnant's it works in /// March-based years, which put the leap day at the end of a year, but it @@ -255,14 +255,14 @@ test daysFromSecondsSaturating { /// take, so the whole conversion is four multiplications and no division. /// /// Joffe gives the range as ±1.89 trillion years, from testing rather than -/// proof; `tools/verify_days.zig` checks every day number from `min_days` -/// to `max_days` against Hinnant's `civil_from_days` in `hinnant.zig`. On +/// proof; `tools/verify_days.zig` checks every day number from `min_day_number` +/// to `max_day_number` against Hinnant's `civil_from_days` in `hinnant.zig`. On /// 2026-10-16 all 1,568,704,592,610 of them agreed. The /// article also gives smaller constants for ARM, where large immediates /// are slow to load; this is the x86-64 form, which is correct everywhere /// and has not been measured on ARM. pub fn fromDayNumber(days: DayNumber) Date { - std.debug.assert(days >= min_days and days <= max_days); + std.debug.assert(days >= min_day_number and days <= max_day_number); const eras: u64 = 4726498270; const d_shift: u64 = 146097 * eras - 719469; @@ -354,8 +354,8 @@ test yearAndFirstDay { // And so do the days at either end of the calendar. for (0..800) |i| { - try expectFirstDay(min_days + @as(DayNumber, @intCast(i))); - try expectFirstDay(max_days - @as(DayNumber, @intCast(i))); + try expectFirstDay(min_day_number + @as(DayNumber, @intCast(i))); + try expectFirstDay(max_day_number - @as(DayNumber, @intCast(i))); } } @@ -399,8 +399,8 @@ fn expectHinnant(days: DayNumber) !void { test expectHinnant { try expectHinnant(0); - try expectHinnant(min_days); - try expectHinnant(max_days); + try expectHinnant(min_day_number); + try expectHinnant(max_day_number); } // Joffe's algorithms against Hinnant's, which are slower and easy to check @@ -434,8 +434,8 @@ test "fromDayNumber and toDayNumber agree with Hinnant" { // A thousand years at each end. for (0..366_000) |i| { - try expectHinnant(min_days + @as(DayNumber, @intCast(i))); - try expectHinnant(max_days - @as(DayNumber, @intCast(i))); + try expectHinnant(min_day_number + @as(DayNumber, @intCast(i))); + try expectHinnant(max_day_number - @as(DayNumber, @intCast(i))); } } @@ -1005,7 +1005,7 @@ pub fn fromWeek( else (Date{ .year = @intCast(week_year), .month = .Jan, .day = 1 }).toDayNumber(); - const days = std.math.clamp(january_first + day_of_year - 1, min_days, max_days); + const days = std.math.clamp(january_first + day_of_year - 1, min_day_number, max_day_number); return fromDayNumber(@intCast(days)); } diff --git a/src/Duration.zig b/src/Duration.zig index 15cedb9..3e3a807 100644 --- a/src/Duration.zig +++ b/src/Duration.zig @@ -297,7 +297,7 @@ pub fn addToDateCheckedWith(self: Duration, date: Date, arithmetic: Arithmetic) break :blk @as(i128, first) + (date.day - 1) + self.days; }, }; - if (days < Date.min_days or days > Date.max_days) return error.OutOfRange; + if (days < Date.min_day_number or days > Date.max_day_number) return error.OutOfRange; return Date.fromDayNumber(@intCast(days)); } diff --git a/src/fuzz.zig b/src/fuzz.zig index 17cc420..5dda6e5 100644 --- a/src/fuzz.zig +++ b/src/fuzz.zig @@ -1581,7 +1581,7 @@ fn checkArithmetic(start: DateTime, duration: Duration, arithmetic: Duration.Ari @as(i256, (Date{ .year = year, .month = month, .day = 1 }).toDayNumber()) + (start.day - 1) + moved else @as(i256, (Date{ .year = year, .month = month, .day = @min(start.day, month.lastDay(year)) }).toDayNumber()) + moved; - if (days < Date.min_days or days > Date.max_days) break :blk null; + if (days < Date.min_day_number or days > Date.max_day_number) break :blk null; break :blk Date.fromDayNumber(@intCast(days)); }; @@ -1715,7 +1715,7 @@ test "random calendar round trips" { test "fuzz Date day numbers against Hinnant" { const driver = struct { fn one(_: void, smith: *std.testing.Smith) anyerror!void { - try checkAgainstHinnant(smith.valueRangeAtMost(Date.DayNumber, Date.min_days, Date.max_days)); + try checkAgainstHinnant(smith.valueRangeAtMost(Date.DayNumber, Date.min_day_number, Date.max_day_number)); } }; try std.testing.fuzz({}, driver.one, .{}); @@ -1726,9 +1726,9 @@ test "fuzz Date day numbers against Hinnant" { /// at one of the two ends. fn hinnantProperty(random: std.Random) !void { const days: Date.DayNumber = switch (random.uintLessThan(u8, 8)) { - 0 => Date.min_days + random.intRangeAtMost(Date.DayNumber, 0, 146097), - 1 => Date.max_days - random.intRangeAtMost(Date.DayNumber, 0, 146097), - 2, 3 => random.intRangeAtMost(Date.DayNumber, Date.min_days, Date.max_days), + 0 => Date.min_day_number + random.intRangeAtMost(Date.DayNumber, 0, 146097), + 1 => Date.max_day_number - random.intRangeAtMost(Date.DayNumber, 0, 146097), + 2, 3 => random.intRangeAtMost(Date.DayNumber, Date.min_day_number, Date.max_day_number), else => random.intRangeAtMost(Date.DayNumber, -4_000_000, 4_000_000), }; try checkAgainstHinnant(days); diff --git a/tools/verify_days.zig b/tools/verify_days.zig index 0a69368..03eff96 100644 --- a/tools/verify_days.zig +++ b/tools/verify_days.zig @@ -1,11 +1,11 @@ // SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT -//! Checks `Date.fromDayNumber` against Hinnant's -//! `civil_from_days` for every day number a `Date` can hold, from -//! `Date.min_days` to `Date.max_days`: about 1.57 trillion of them, and -//! checks that `Date.toDayNumber` takes each date it gives back -//! to the day number it came from. +//! Checks `Date.fromDayNumber` against Hinnant's `civil_from_days` for +//! every day number a `Date` can hold, from `Date.min_day_number` to +//! `Date.max_day_number`: about 1.57 trillion of them, and checks that +//! `Date.toDayNumber` takes each date it gives back to the day number it +//! came from. //! //! The library converts in both directions with Ben Joffe's fixed-point //! algorithms, whose author established their range by testing rather than @@ -56,14 +56,14 @@ pub fn firstDisagreement(first: DayNumber, last: DayNumber) ?DayNumber { test firstDisagreement { try std.testing.expectEqual(@as(?DayNumber, null), firstDisagreement(-100_000, 100_000)); - try std.testing.expectEqual(@as(?DayNumber, null), firstDisagreement(Date.min_days, Date.min_days + 1000)); - try std.testing.expectEqual(@as(?DayNumber, null), firstDisagreement(Date.max_days - 1000, Date.max_days)); + try std.testing.expectEqual(@as(?DayNumber, null), firstDisagreement(Date.min_day_number, Date.min_day_number + 1000)); + try std.testing.expectEqual(@as(?DayNumber, null), firstDisagreement(Date.max_day_number - 1000, Date.max_day_number)); } /// What the tasks share: where the next chunk starts, how many are done, /// and the earliest disagreement any of them has found. const Shared = struct { - next: std.atomic.Value(i64) = .init(Date.min_days), + next: std.atomic.Value(i64) = .init(Date.min_day_number), done: std.atomic.Value(u64) = .init(0), /// `maxInt(i64)` until something disagrees. disagreement: std.atomic.Value(i64) = .init(std.math.maxInt(i64)), @@ -76,8 +76,8 @@ const Shared = struct { fn worker(shared: *Shared) void { while (shared.disagreement.load(.monotonic) == std.math.maxInt(i64)) { const start = shared.next.fetchAdd(chunk_len, .monotonic); - if (start > Date.max_days) return; - const end = @min(start + chunk_len - 1, Date.max_days); + if (start > Date.max_day_number) return; + const end = @min(start + chunk_len - 1, Date.max_day_number); if (firstDisagreement(@intCast(start), @intCast(end))) |day| { _ = shared.disagreement.fetchMin(day, .monotonic); @@ -107,7 +107,7 @@ pub fn main(init: std.process.Init.Minimal) !void { defer threaded.deinit(); const io = threaded.io(); - const span: u64 = @intCast(@as(i64, Date.max_days) - Date.min_days + 1); + const span: u64 = @intCast(@as(i64, Date.max_day_number) - Date.min_day_number + 1); var shared: Shared = .{ .chunks = std.math.divCeil(u64, span, chunk_len) catch unreachable, .io = io, @@ -117,8 +117,8 @@ pub fn main(init: std.process.Init.Minimal) !void { const tasks = std.Thread.getCpuCount() catch 1; std.debug.print("checking {d} day numbers, {d} to {d}, on {d} tasks\n", .{ span, - Date.min_days, - Date.max_days, + Date.min_day_number, + Date.max_day_number, tasks, }); -- knotmirror 0.0.1