// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! A calendar date: a year, a month and a day, with no time of day and no //! timezone attached. //! //! Conversion to and from a day number runs on Howard Hinnant's //! `civil_from_days` and `days_from_civil`, derived in *chrono-Compatible //! Low-Level Date Algorithms*, which the page itself dates 2021-09-01: //! . //! //! Two ideas carry the whole of it. The year is shifted to start in March, //! so that the leap day falls at the end of it and the month lengths become //! a repeating pattern that a single division can invert. And the calendar //! is cut into *eras* of 400 years, which is the period after which the //! proleptic Gregorian calendar repeats exactly: every era is 146097 days //! long, whichever one it is. So a conversion factors the era out first and //! then works with a day-of-era in `[0, 146096]` and a year-of-era in //! `[0, 399]` — which is why getting one era right gets all of time right, //! and why so many of the assertions in these functions name those two //! ranges. //! See `fromDaysSinceStartOfEra` and `toDaysSinceStartOfEra`. //! //! Hinnant dedicates the algorithms to the public domain ("Consider these //! donated to the public domain"), so no permission is needed to carry them //! into an MIT-licensed library; the citation is here because the //! derivation is worth having beside the code. const Date = @This(); const std = @import("std"); const Year = @import("year.zig").Year; const Month = @import("month.zig").Month; const Day = @import("day.zig").Day; const DayOfWeek = @import("dayofweek.zig").DayOfWeek; const leap = @import("leap.zig"); const iso8601 = @import("iso8601.zig"); const json = @import("json.zig"); year: Year, month: Month, day: Day, /// The Unix epoch, 1970-01-01. pub const init: Date = .{ .year = 1970, .month = .Jan, .day = 1, }; /// Returns true if `day` is a valid day number for this month and year. pub fn isRegular(self: Date) bool { return self.day >= 1 and self.day <= self.month.lastDay(self.year); } test isRegular { try std.testing.expect((Date{ .year = 2024, .month = .Feb, .day = 29 }).isRegular()); // 2025 is not a leap year, so the same date is not a day in it. try std.testing.expect(!(Date{ .year = 2025, .month = .Feb, .day = 29 }).isRegular()); try std.testing.expect(!(Date{ .year = 2024, .month = .Apr, .day = 31 }).isRegular()); try std.testing.expect(!(Date{ .year = 2024, .month = .Jan, .day = 0 }).isRegular()); } /// A count of days since 1970-01-01, wide enough for every date a `Year` /// can hold: the whole span of years at the longest a year can be. pub const DaysType = std.math.IntFittingRange( std.math.minInt(Year) * 366, std.math.maxInt(Year) * 366, ); /// The day numbers of the first and last dates a `Date` can hold. /// /// `DaysType` 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: DaysType = (Date{ .year = std.math.minInt(Year), .month = .Jan, .day = 1, }).toDaysSinceStartOfEra(); pub const max_days: DaysType = (Date{ .year = std.math.maxInt(Year), .month = .Dec, .day = 31, }).toDaysSinceStartOfEra(); test min_days { // The bounds are dates, and round trip like any other. try std.testing.expectEqual( Date{ .year = std.math.minInt(Year), .month = .Jan, .day = 1 }, fromDaysSinceStartOfEra(min_days), ); try std.testing.expectEqual( Date{ .year = std.math.maxInt(Year), .month = .Dec, .day = 31 }, fromDaysSinceStartOfEra(max_days), ); // And they are inside what `DaysType` 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(DaysType)); try std.testing.expect(max_days < std.math.maxInt(DaysType)); } /// The first and last instants that fall on a date a `Date` can hold, as /// seconds since the epoch. /// /// 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); 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)); // 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)); } /// Returns the day number `seconds` seconds after the epoch falls in, /// clamped to the range a `Date` can hold. /// /// An `i64` of seconds reaches far outside the calendar: a `Year` is an /// `i32`, so the furthest date there is sits around 6.8e16 seconds from /// the epoch and an `i64` goes to 9.2e18. Converting the excess would be /// an `@intCast` that cannot fit, which is a panic in a safe build and /// illegal behaviour in a fast one. /// /// Clamping rather than refusing keeps every caller total. The answer for /// an instant outside the calendar is the first or last date there is, /// which is wrong in the same way that the question was. pub fn daysFromSecondsSaturating(seconds: i64) DaysType { const days = @divFloor(seconds, std.time.s_per_day); if (days < min_days) return min_days; if (days > max_days) return max_days; return @intCast(days); } test daysFromSecondsSaturating { try std.testing.expectEqual(@as(DaysType, 0), daysFromSecondsSaturating(0)); try std.testing.expectEqual(@as(DaysType, 1), daysFromSecondsSaturating(std.time.s_per_day)); // Floor division, so a second before the epoch is the day before it. try std.testing.expectEqual(@as(DaysType, -1), daysFromSecondsSaturating(-1)); // 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))); _ = fromDaysSinceStartOfEra(daysFromSecondsSaturating(std.math.maxInt(i64))); _ = fromDaysSinceStartOfEra(daysFromSecondsSaturating(std.math.minInt(i64))); } /// Returns the date that is `days` days after 1970-01-01; negative values /// give dates before the epoch. /// /// This is Hinnant's `civil_from_days`. Adding /// 719468 moves the count off the Unix epoch and onto 0000-03-01, the first /// day of era 0, so that every day number in range is a non-negative /// distance into some era. Dividing by 146097 gives the era and the /// remainder the day-of-era. The year-of-era comes out of the day-of-era by /// `(doe - doe/1460 + doe/36524 - doe/146096) / 365`, whose three /// corrections are the three days on which a plain `doe / 365` steps a year /// early: day 1460, which is still year 3 because the first four years hold /// 1461 days; day 36524, which is already year 100 because the first /// century holds 36524 days; and day 146096, the last day of the era, which /// would otherwise come out as year 400. What is left is a day-of-year /// counted from 1 March, and `(5 * doy + 2) / 153` turns that into a month /// because March-based month lengths run 31, 30, 31, 30, 31, 31, 30, 31, /// 30, 31, 31, 28 — a line fits the first eleven of them exactly, and /// February's short month is at the end where nothing follows it to be /// thrown off. January and February are then carried forward into the next /// calendar year, which is where the March-based year and the civil one /// part company. /// /// Hinnant writes the era as `z >= 0 ? z : z - 146096` because C++ divides /// towards zero and the algorithm wants flooring; the same guard is kept /// here around `@divTrunc` rather than reaching for `@divFloor`, so that /// this reads as the published algorithm does. pub fn fromDaysSinceStartOfEra(days: DaysType) Date { const z = days + 719468; const era = @divTrunc(if (z >= 0) z else z - 146096, 146097); const day_of_era = (z - era * 146097); std.debug.assert(day_of_era >= 0 and day_of_era <= 146096); const year_of_era = @divTrunc(day_of_era - @divTrunc(day_of_era, 1460) + @divTrunc(day_of_era, 36524) - @divTrunc(day_of_era, 146096), 365); std.debug.assert(year_of_era >= 0 and year_of_era <= 399); const year_0 = year_of_era + era * 400; const day_of_year = day_of_era - (365 * year_of_era + @divTrunc(year_of_era, 4) - @divTrunc(year_of_era, 100)); std.debug.assert(day_of_year >= 0 and day_of_year <= 365); const month_0 = @divTrunc(5 * day_of_year + 2, 153); std.debug.assert(month_0 >= 0 and month_0 <= 11); const month = month_0 + (if (month_0 < 10) @as(DaysType, 3) else @as(DaysType, -9)); std.debug.assert(month >= 1 and month <= 12); const year = year_0 + (if (month <= 2) @as(Year, 1) else @as(Year, 0)); std.debug.assert(year >= std.math.minInt(Year) and year <= std.math.maxInt(Year)); const day = day_of_year - @divTrunc(153 * month_0 + 2, 5) + 1; std.debug.assert(day >= 1 and day <= @as(Month, @enumFromInt(month)).lastDay(@intCast(year))); return Date{ .year = @intCast(year), .month = @enumFromInt(month), .day = @intCast(day), }; } /// The year a day number falls in, together with the day number of that /// year's first of January. /// /// `fromDaysSinceStartOfEra` answers this too, by way of a month and a /// day that then have to be added back up. This stops short of them: it is /// the first half of Hinnant's `civil_from_days`, run only as far as the /// year-of-era and the March-based day-of-year. That algorithm counts /// years from March so that the leap day lands at the end of one, which is /// why there is a year to name before there is a month: the March-based /// day of year decides which side of the new year the date falls on, and /// the first of January is that many days back plus or minus the run from /// January to March. /// /// `posixtz` works out every switch of a rule as an offset from the first /// of January, and this is where that day comes from. pub fn yearAndFirstDay(days: DaysType) struct { year: Year, january_first: DaysType } { const z = days + 719468; const era = @divTrunc(if (z >= 0) z else z - 146096, 146097); const day_of_era = (z - era * 146097); std.debug.assert(day_of_era >= 0 and day_of_era <= 146096); const year_of_era = @divTrunc(day_of_era - @divTrunc(day_of_era, 1460) + @divTrunc(day_of_era, 36524) - @divTrunc(day_of_era, 146096), 365); std.debug.assert(year_of_era >= 0 and year_of_era <= 399); const year_0 = year_of_era + era * 400; const day_of_year = day_of_era - (365 * year_of_era + @divTrunc(year_of_era, 4) - @divTrunc(year_of_era, 100)); std.debug.assert(day_of_year >= 0 and day_of_year <= 365); // The last 59 or 60 days of a March-based year are the January and // February of the next calendar one, and 306 is the run from the // first of March to the end of December. if (day_of_year < 306) { const year: Year = @intCast(year_0); const before_march: DaysType = if (leap.is(year)) 60 else 59; return .{ .year = year, .january_first = days - day_of_year - before_march }; } return .{ .year = @intCast(year_0 + 1), .january_first = days - day_of_year + 306, }; } test yearAndFirstDay { const new_year = (Date{ .year = 2024, .month = .Jan, .day = 1 }).toDaysSinceStartOfEra(); // A day names its own year, and the first of January names itself. const opening = yearAndFirstDay(new_year); try std.testing.expectEqual(@as(Year, 2024), opening.year); try std.testing.expectEqual(new_year, opening.january_first); const summer = yearAndFirstDay((Date{ .year = 2024, .month = .Jul, .day = 4 }).toDaysSinceStartOfEra()); try std.testing.expectEqual(@as(Year, 2024), summer.year); try std.testing.expectEqual(new_year, summer.january_first); // And the day before belongs to the year before. try std.testing.expectEqual(@as(Year, 2023), yearAndFirstDay(new_year - 1).year); // It is the same answer the full conversion gives, all the way // through a leap year and the ones either side of it. var day = (Date{ .year = 2023, .month = .Jan, .day = 1 }).toDaysSinceStartOfEra(); const last = (Date{ .year = 2025, .month = .Dec, .day = 31 }).toDaysSinceStartOfEra(); while (day <= last) : (day += 1) { const date = fromDaysSinceStartOfEra(day); const first = day - (@as(DaysType, date.month.daysBefore(date.year)) + date.day - 1); const placed = yearAndFirstDay(day); try std.testing.expectEqual(date.year, placed.year); try std.testing.expectEqual(first, placed.january_first); } } test fromDaysSinceStartOfEra { try std.testing.expectEqual( Date{ .year = 1970, .month = .Jan, .day = 1 }, fromDaysSinceStartOfEra(0), ); try std.testing.expectEqual( Date{ .year = 1970, .month = .Jan, .day = 2 }, fromDaysSinceStartOfEra(1), ); // Negative counts reach dates before the epoch. try std.testing.expectEqual( Date{ .year = 1969, .month = .Dec, .day = 31 }, fromDaysSinceStartOfEra(-1), ); } test "civilFromDays" { const tests = [_]struct { days: DaysType, result: Date }{ .{ .days = 0, .result = Date{ .year = 1970, .month = .Jan, .day = 1, }, }, .{ .days = 1, .result = Date{ .year = 1970, .month = .Jan, .day = 2, }, }, .{ .days = -1, .result = Date{ .year = 1969, .month = .Dec, .day = 31, }, }, .{ .days = 19605, .result = Date{ .year = 2023, .month = .Sep, .day = 5, }, }, }; for (tests) |case| { const result = Date.fromDaysSinceStartOfEra(case.days); try std.testing.expectEqual(case.result, result); } } /// Returns the number of days from 1970-01-01 to this date; negative for /// dates before the epoch. Asserts that the date is valid (see `isRegular`). /// /// Hinnant's `days_from_civil`, which is `fromDaysSinceStartOfEra` read /// backwards and against the same 400-year era. January and February are /// moved into the previous March-based year, that year is split into an era /// and a year-of-era, `(153 * mp + 2) / 5` recovers the day-of-year from /// the March-based month index that `(5 * doy + 2) / 153` produced going /// the other way, and `era * 146097 + doe - 719468` puts the answer back /// on the Unix epoch. There is no leap-year test anywhere in it: the leap /// day is the last day of a March-based year, so `yoe/4 - yoe/100` counts /// the leap days of the era and the era boundary accounts for the /// four-hundreds. pub fn toDaysSinceStartOfEra(self: Date) DaysType { std.debug.assert(self.day >= 1 and self.day <= self.month.lastDay(self.year)); const month = self.month.monthNumber(); // Everything from here is in `DaysType` rather than `Year`. The // arithmetic leaves an `i32` twice over the range a `Year` claims: // January of the earliest year underflows the shift below, and the // era multiplication at the end overflows from about year 5.9 // million. Both were an overflow rather than a wrong answer, and both // are inside what the type says it holds. const year = @as(DaysType, self.year) - switch (self.month) { .Jan, .Feb => @as(DaysType, 1), else => @as(DaysType, 0), }; const era = @divTrunc( if (year >= 0) year else year - 399, 400, ); const year_of_era = year - era * 400; std.debug.assert(year_of_era >= 0 and year_of_era <= 399); const day_of_year = @divTrunc( 153 * (@as(DaysType, month) + switch (self.month) { .Jan, .Feb => @as(DaysType, 9), else => @as(DaysType, -3), }) + 2, 5, ) + self.day - 1; std.debug.assert(day_of_year >= 0 and day_of_year <= 365); const day_of_era = year_of_era * 365 + @divTrunc(year_of_era, 4) - @divTrunc(year_of_era, 100) + day_of_year; std.debug.assert(day_of_era >= 0 and day_of_era <= 146096); return era * 146097 + day_of_era - 719468; } test toDaysSinceStartOfEra { try std.testing.expectEqual( @as(DaysType, 0), (Date{ .year = 1970, .month = .Jan, .day = 1 }).toDaysSinceStartOfEra(), ); try std.testing.expectEqual( @as(DaysType, -1), (Date{ .year = 1969, .month = .Dec, .day = 31 }).toDaysSinceStartOfEra(), ); // It is the inverse of `fromDaysSinceStartOfEra`. const date: Date = .{ .year = 2024, .month = .Mar, .day = 15 }; try std.testing.expectEqual(date, fromDaysSinceStartOfEra(date.toDaysSinceStartOfEra())); } test "daysFromCivil" { const tests = [_]struct { date: Date, result: DaysType }{ .{ .date = .{ .year = 1970, .month = .Jan, .day = 1, }, .result = 0, }, .{ .date = .{ .year = 1970, .month = .Jan, .day = 2, }, .result = 1, }, .{ .date = .{ .year = 1969, .month = .Dec, .day = 31, }, .result = -1, }, }; for (tests) |case| { const result = Date.toDaysSinceStartOfEra(case.date); try std.testing.expectEqual(case.result, result); } } /// Returns the day of the week this date falls on. pub fn dayOfWeek(self: Date) DayOfWeek { const days = self.toDaysSinceStartOfEra(); return DayOfWeek.fromDaysSinceStartOfEra(days); } /// A week-numbering year: a `Year` and one more at each end. /// /// The last days of December can belong to week 1 of the year after, and /// the first days of January to the last week of the year before. At the /// two ends of the calendar that neighbouring year is one a `Year` cannot /// hold, so the week of 31 December of the last `Year` would have nowhere /// to say which year it is in. This is one bit wider, which is exactly /// enough, and means `weekOfYear` never has to answer with a year that is /// not the right one. pub const WeekYear = std.math.IntFittingRange( @as(i64, std.math.minInt(Year)) - 1, @as(i64, std.math.maxInt(Year)) + 1, ); test WeekYear { try std.testing.expect(std.math.maxInt(WeekYear) > std.math.maxInt(Year)); try std.testing.expect(std.math.minInt(WeekYear) < std.math.minInt(Year)); } /// A week of some year, and the year that week belongs to. pub const Week = struct { /// The week-numbering year, which is not always the calendar year: /// see `weekOfYear`. A `WeekYear` rather than a `Year`, because at the /// ends of the calendar it can be the year just past either end. year: WeekYear, /// The week within that year, 1 through 52, 53 or 54. week: u8, }; /// Returns the week of the year this date falls in, and the year that /// week belongs to, under a week rule given by its two parameters. /// /// A week rule needs exactly two things: which weekday a week begins on, /// and which day of January is always in week 1. ISO 8601 says Monday and /// the 4th; the English-language convention, which is what moment.js uses /// by default, says Sunday and the 1st. Everything else follows. /// /// Because a week belongs wholly to one year, the days at either end of a /// calendar year may fall in a week of its neighbour, which is why the /// year is returned alongside the week and why formatting a week beside /// the calendar year would be wrong. /// /// The offset of week 1 from the start of the year is found from the /// weekday of the anchoring January day, the day of the year is measured /// against it, and a result that falls off either end is carried into the /// neighbouring year. This is moment.js's formulation, followed /// deliberately so that the two cannot disagree; see `tools/oracle.js`. pub fn weekOfYear( self: Date, /// The weekday a week begins on: Monday for ISO 8601. week_starts_on: DayOfWeek, /// The day of January that is always in week 1: the 4th for ISO 8601. /// /// May be zero or negative, which names a day of the December before /// and is what a rule whose first week reaches back into the old year /// looks like. moment writes the same thing as `doy`, and eight of /// its locales use a `doy` that puts this at -5. january_day_in_first_week: i8, ) Week { const offset = firstWeekOffset(self.year, week_starts_on, january_day_in_first_week); const day_of_year = @as(i32, self.month.daysBefore(self.year)) + self.day; const week = @divFloor(day_of_year - offset - 1, 7) + 1; // The neighbouring years are `WeekYear`s, which reach one year past // either end of the calendar, so these never overflow and never have // to answer with a year other than the right one. if (week < 1) { const previous = @as(WeekYear, self.year) - 1; return .{ .year = previous, .week = @intCast(week + weeksInYear(previous, week_starts_on, january_day_in_first_week)), }; } const in_year = weeksInYear(self.year, week_starts_on, january_day_in_first_week); if (week > in_year) return .{ .year = @as(WeekYear, self.year) + 1, .week = @intCast(week - in_year) }; return .{ .year = self.year, .week = @intCast(week) }; } /// How far week 1 of `year` starts from January 1st, as a day-of-year /// offset that may be negative when week 1 begins in the previous year. fn firstWeekOffset(year: i64, week_starts_on: DayOfWeek, january_day_in_first_week: i8) i32 { // Counted from January 1st rather than built as a date, because the // anchoring day may be zero or negative -- a day of the December // before, which no `Date` can hold -- and stepping a weekday by a // day count works either way. const anchor_weekday = @mod( januaryFirstWeekday(year) + @as(i32, january_day_in_first_week) - 1, 7, ); // Where the anchoring day sits within its own week, counting from the // day the week begins on rather than from Sunday. const within_week = @mod(anchor_weekday - @as(i32, week_starts_on.weekdayNumber()), 7); return @as(i32, january_day_in_first_week) - 1 - within_week; } /// The number of weeks in `year` under this rule, which is 52 or 53, and /// is what a week falling off either end of a year is carried across. /// /// Public because parsing needs it to bound a week it was given: week 53 /// is a real week of some years and not of others. /// /// It takes a `WeekYear`, since `weekOfYear` asks it about the years either /// side of the calendar, and measures a year against the one after it in /// an `i64`, so even the last `WeekYear` has a real year after it rather /// than being measured against itself. pub fn weeksInYear(year: WeekYear, week_starts_on: DayOfWeek, january_day_in_first_week: i8) i32 { const offset = firstWeekOffset(year, week_starts_on, january_day_in_first_week); const next = firstWeekOffset(@as(i64, year) + 1, week_starts_on, january_day_in_first_week); const days: i32 = if (isLeapAnyYear(year)) 366 else 365; return @divTrunc(days - offset + next, 7); } /// The weekday of 1 January of any year at all, as a `weekdayNumber`, /// Sunday = 0. /// /// Gauss's formula for the first of January, which needs no date and so /// works for years a `Date` cannot hold. Each of the three terms steps the /// weekday by how far a span of years moves it: a common year is 365 days, /// one more than 52 weeks, so every year moves it by one and each fourth /// year by one more; a century leaves out one of those leap days, and /// every fourth century puts it back. Written as residues of the years /// already passed, that is `1 + 5·(y−1 mod 4) + 4·(y−1 mod 100) + /// 6·(y−1 mod 400)`, taken mod 7, and `@mod` keeps each residue /// non-negative so the same expression holds before year zero. fn januaryFirstWeekday(year: i64) i32 { const y = year - 1; return @intCast(@mod(1 + 5 * @mod(y, 4) + 4 * @mod(y, 100) + 6 * @mod(y, 400), 7)); } test januaryFirstWeekday { // It agrees with the day count wherever a `Date` can be built to ask, // including both ends of the calendar and either side of year zero. for ([_]Year{ 1970, 2024, 2025, 2000, 1900, 1, 0, -1, -400, -401, std.math.minInt(Year), std.math.maxInt(Year) }) |year| { const january_first: Date = .{ .year = year, .month = .Jan, .day = 1 }; try std.testing.expectEqual(@as(i32, january_first.dayOfWeek().weekdayNumber()), januaryFirstWeekday(year)); } // And a year past the end is a year's length on from the last one. const last: Date = .{ .year = std.math.maxInt(Year), .month = .Jan, .day = 1 }; const length: i32 = if (leap.is(std.math.maxInt(Year))) 366 else 365; try std.testing.expectEqual( @mod(@as(i32, last.dayOfWeek().weekdayNumber()) + length, 7), januaryFirstWeekday(@as(i64, std.math.maxInt(Year)) + 1), ); } /// `leap.is`, for any year at all rather than only a `Year`. fn isLeapAnyYear(year: i64) bool { return @mod(year, 4) == 0 and (@mod(year, 100) != 0 or @mod(year, 400) == 0); } test isLeapAnyYear { for ([_]Year{ 2000, 1900, 2024, 2025, 0, -4, -100, std.math.maxInt(Year), std.math.minInt(Year) }) |year| { try std.testing.expectEqual(leap.is(year), isLeapAnyYear(year)); } } test "a week at either end of the calendar names its real year" { // The last days of the last `Year` can be in week 1 of the year after // it, and the first days of the first `Year` in the last week of the // year before. Both are `WeekYear`s, so the answer is exact, and it // comes back to the same date. This used to saturate: 31 December of // the last year was reported as week 1 of that same year, a week in // its January. const last = std.math.maxInt(Year); const first = std.math.minInt(Year); for ([_]struct { DayOfWeek, i8 }{ .{ .Mon, 4 }, .{ .Sun, 1 } }) |rule| { var crossed_forwards = false; var crossed_backwards = false; for (25..32) |day| { const date: Date = .{ .year = last, .month = .Dec, .day = @intCast(day) }; const week = date.weekOfYear(rule[0], rule[1]); if (week.year > last) crossed_forwards = true; try std.testing.expectEqual(date, fromWeek(week.year, week.week, date.dayOfWeek(), rule[0], rule[1])); } for (1..8) |day| { const date: Date = .{ .year = first, .month = .Jan, .day = @intCast(day) }; const week = date.weekOfYear(rule[0], rule[1]); if (week.year < first) crossed_backwards = true; try std.testing.expectEqual(date, fromWeek(week.year, week.week, date.dayOfWeek(), rule[0], rule[1])); } // With these two rules the calendar's own ends do cross over, so // the test is not passing by never reaching the case it is for. try std.testing.expect(crossed_forwards or crossed_backwards); } } test "the week rules answer at the ends of the calendar" { // Carrying a week into the year before or after has no year to carry // into at the first and last a `Year` can hold. Fuzzing found this // as an integer overflow reached from `DateTime.parseWith`, whose // `relative_to` is the caller's and can hold any year at all. const first = std.math.minInt(Year); const last = std.math.maxInt(Year); for ([_]Year{ first, first + 1, last - 1, last }) |year| { for ([_]DayOfWeek{ .Sun, .Mon, .Sat }) |starts_on| { // Including an anchor that puts week one in the December // before, which is what a `doy` of 12 comes to, and one that // puts it late enough that the first days of January fall in // the year before's last week. for ([_]i8{ 1, 4, 7, -5 }) |anchor| { const weeks = weeksInYear(year, starts_on, anchor); try std.testing.expect(weeks >= 1); // The first and last day of the year, which are the two // that can fall into a neighbouring year's week. for ([_]Date{ .{ .year = year, .month = .Jan, .day = 1 }, .{ .year = year, .month = .Jan, .day = 2 }, .{ .year = year, .month = .Dec, .day = 30 }, .{ .year = year, .month = .Dec, .day = 31 }, }) |date| { const found = date.weekOfYear(starts_on, anchor); try std.testing.expect(found.week >= 1); } // And back the other way, which is where the day of the // year runs off the end. _ = fromWeek(year, 1, starts_on, starts_on, anchor); _ = fromWeek(year, @intCast(weeks), starts_on, starts_on, anchor); } } } // A day of the year outside the year is carried into its neighbour, // and clamped rather than carried when there is no neighbour. try std.testing.expectEqual( Date{ .year = first, .month = .Jan, .day = 1 }, fromDayOfYear(first, -1000), ); try std.testing.expectEqual( Date{ .year = last, .month = .Dec, .day = 31 }, fromDayOfYear(last, 1000), ); } test weeksInYear { // A year has 53 weeks when it starts on the day the week starts on, or // when a leap year starts the day before. try std.testing.expectEqual(@as(i32, 52), weeksInYear(2024, .Mon, 4)); try std.testing.expectEqual(@as(i32, 53), weeksInYear(2026, .Mon, 4)); try std.testing.expectEqual(@as(i32, 53), weeksInYear(2020, .Mon, 4)); // The English-language rule puts January 1st in week 1 and so needs a // 53rd less often; 2020 and 2026 both have one by the ISO rule and // not by this one. try std.testing.expectEqual(@as(i32, 52), weeksInYear(2024, .Sun, 1)); try std.testing.expectEqual(@as(i32, 52), weeksInYear(2020, .Sun, 1)); try std.testing.expectEqual(@as(i32, 52), weeksInYear(2026, .Sun, 1)); } test weekOfYear { // The two rules disagree about 2024-01-07: ISO weeks begin on Monday, // so it is still week 1, while a week beginning on Sunday has already // turned over into week 2. const january: Date = .{ .year = 2024, .month = .Jan, .day = 7 }; try std.testing.expectEqual(@as(u8, 1), january.weekOfYear(.Mon, 4).week); try std.testing.expectEqual(@as(u8, 2), january.weekOfYear(.Sun, 1).week); } /// Returns the ISO 8601 week of this date, and the year that week belongs /// to. Weeks run Monday to Sunday and week 1 is the one containing /// January 4th, equivalently the one holding the year's first Thursday. /// /// That is the week calendar of ISO 8601-1:2019, 4.2.2, whose weeks begin /// on Monday (Table 2) and number `01` to `52` or `53` (4.3.4). As its NOTE /// says, a week's year and the calendar year "do not always match": the /// first day of 2019's week 1 is 2018-12-31. pub fn isoWeek(self: Date) Week { return self.weekOfYear(.Mon, 4); } test isoWeek { // A date in the middle of a year, where the week-numbering year and // the calendar year agree. const march = (Date{ .year = 2024, .month = .Mar, .day = 15 }).isoWeek(); try std.testing.expectEqual(@as(Year, 2024), march.year); try std.testing.expectEqual(@as(u8, 11), march.week); // January 4th is in week 1 by definition, whatever weekday it is. try std.testing.expectEqual(@as(u8, 1), (Date{ .year = 2024, .month = .Jan, .day = 4 }).isoWeek().week); // The first days of a year can belong to the last week of the one // before: 2027 opens on a Friday, so it is still 2026's week 53. const newyear = (Date{ .year = 2027, .month = .Jan, .day = 1 }).isoWeek(); try std.testing.expectEqual(@as(Year, 2026), newyear.year); try std.testing.expectEqual(@as(u8, 53), newyear.week); // And the last days of a year can belong to week 1 of the next. const yearend = (Date{ .year = 2029, .month = .Dec, .day = 31 }).isoWeek(); try std.testing.expectEqual(@as(Year, 2030), yearend.year); try std.testing.expectEqual(@as(u8, 1), yearend.week); } /// Returns the date `day_of_year` days into `year`, counting from 1. /// /// Values outside the year are carried into its neighbour rather than /// refused, so day 0 is the last day of the year before and day 366 of an /// ordinary year is the first of the year after. `weekOfYear`'s inverse /// needs that, because a week can begin in one year and end in another. pub fn fromDayOfYear(year: Year, day_of_year: i32) Date { const length: i32 = if (leap.is(year)) 366 else 365; // Saturating, because carrying into a neighbour has no neighbour to // carry into at the first and last year a `Year` can hold. Stepping // to the same year again would recur for ever, so the day is clamped // into the year instead and the answer stays inside the calendar. if (day_of_year <= 0) { const previous = year -| 1; if (previous == year) return .{ .year = year, .month = .Jan, .day = 1 }; const before: i32 = if (leap.is(previous)) 366 else 365; return fromDayOfYear(previous, before + day_of_year); } if (day_of_year > length) { const next = year +| 1; if (next == year) return .{ .year = year, .month = .Dec, .day = 31 }; return fromDayOfYear(next, day_of_year - length); } var month: Month = .Jan; var remaining = day_of_year; while (remaining > month.lastDay(year)) { remaining -= month.lastDay(year); month = month.next(); } return .{ .year = year, .month = month, .day = @intCast(remaining) }; } test fromDayOfYear { try std.testing.expectEqual(Date{ .year = 2024, .month = .Jan, .day = 1 }, fromDayOfYear(2024, 1)); try std.testing.expectEqual(Date{ .year = 2024, .month = .Mar, .day = 15 }, fromDayOfYear(2024, 75)); try std.testing.expectEqual(Date{ .year = 2024, .month = .Dec, .day = 31 }, fromDayOfYear(2024, 366)); // Out of range carries into the neighbouring year rather than failing. try std.testing.expectEqual(Date{ .year = 2023, .month = .Dec, .day = 31 }, fromDayOfYear(2024, 0)); try std.testing.expectEqual(Date{ .year = 2025, .month = .Jan, .day = 1 }, fromDayOfYear(2024, 367)); try std.testing.expectEqual(Date{ .year = 2026, .month = .Jan, .day = 1 }, fromDayOfYear(2025, 366)); } /// Returns the date of `weekday` in `week` of `week_year`, under a week /// rule given the way `weekOfYear` takes one. This is that function's /// inverse. /// /// The week may reach outside its numbering year at either end, which is /// the whole point of a week-numbering year, so the result can land in the /// year before or after: week 1 of 2027 begins on 2026-12-27. pub fn fromWeek( week_year: WeekYear, week: u16, weekday: DayOfWeek, week_starts_on: DayOfWeek, january_day_in_first_week: i8, ) Date { // Where the weekday sits within its week, counting from the day the // week begins on rather than from Sunday. const within_week = @mod( @as(i32, weekday.weekdayNumber()) - @as(i32, week_starts_on.weekdayNumber()), 7, ); const offset = firstWeekOffset(week_year, week_starts_on, january_day_in_first_week); const day_of_year = 1 + 7 * (@as(i64, week) - 1) + within_week + offset; // Counted as a day number from the first of January of `week_year`, // which for the year either side of the calendar is found from the // year inside it, since no `Date` of its own can be built to ask. A // day number outside the calendar is clamped to its first or last day, // as `fromDayOfYear` clamps. const january_first: i64 = if (week_year > std.math.maxInt(Year)) @as(i64, (Date{ .year = std.math.maxInt(Year), .month = .Jan, .day = 1 }).toDaysSinceStartOfEra()) + (if (leap.is(std.math.maxInt(Year))) @as(i64, 366) else 365) else if (week_year < std.math.minInt(Year)) @as(i64, (Date{ .year = std.math.minInt(Year), .month = .Jan, .day = 1 }).toDaysSinceStartOfEra()) - (if (isLeapAnyYear(week_year)) @as(i64, 366) else 365) else (Date{ .year = @intCast(week_year), .month = .Jan, .day = 1 }).toDaysSinceStartOfEra(); const days = std.math.clamp(january_first + day_of_year - 1, min_days, max_days); return fromDaysSinceStartOfEra(@intCast(days)); } test fromWeek { // ISO: Monday starts the week and week 1 holds January 4th. try std.testing.expectEqual( Date{ .year = 2001, .month = .Jan, .day = 4 }, fromWeek(2001, 1, .Thu, .Mon, 4), ); try std.testing.expectEqual( Date{ .year = 2024, .month = .Mar, .day = 15 }, fromWeek(2024, 11, .Fri, .Mon, 4), ); // A week can reach outside its numbering year: 2026's week 53 runs to // the Sunday after New Year's Day 2027. try std.testing.expectEqual( Date{ .year = 2027, .month = .Jan, .day = 3 }, fromWeek(2026, 53, .Sun, .Mon, 4), ); try std.testing.expectEqual( Date{ .year = 2026, .month = .Dec, .day = 28 }, fromWeek(2026, 53, .Mon, .Mon, 4), ); // It is the inverse of `weekOfYear`, under either rule. for ([_]Date{ .{ .year = 2024, .month = .Mar, .day = 15 }, .{ .year = 2027, .month = .Jan, .day = 1 }, .{ .year = 2029, .month = .Dec, .day = 31 }, }) |date| { const iso = date.isoWeek(); try std.testing.expectEqual(date, fromWeek(iso.year, iso.week, date.dayOfWeek(), .Mon, 4)); const locale = date.localeWeek(); try std.testing.expectEqual(date, fromWeek(locale.year, locale.week, date.dayOfWeek(), .Sun, 1)); } } /// Returns the week of this date under the English-language convention, /// where weeks run Sunday to Saturday and week 1 is the one containing /// January 1st, and the year that week belongs to. /// /// This is what the `w` and `gg` format sequences write, because it is /// what moment.js's default locale means by a week. `isoWeek` is the /// other rule, and the `W` and `GG` sequences. pub fn localeWeek(self: Date) Week { return self.weekOfYear(.Sun, 1); } test localeWeek { // January 1st is always in week 1, whatever weekday it falls on. try std.testing.expectEqual(@as(u8, 1), (Date{ .year = 2024, .month = .Jan, .day = 1 }).localeWeek().week); try std.testing.expectEqual(@as(u8, 1), (Date{ .year = 2027, .month = .Jan, .day = 1 }).localeWeek().week); // 2026-12-31 is a Thursday, in the week that holds 2027's January // 1st, so it is already week 1 of 2027 by this rule while ISO still // calls it week 53 of 2026. const yearend = (Date{ .year = 2026, .month = .Dec, .day = 31 }).localeWeek(); try std.testing.expectEqual(@as(Year, 2027), yearend.year); try std.testing.expectEqual(@as(u8, 1), yearend.week); try std.testing.expectEqual(@as(Year, 2026), (Date{ .year = 2026, .month = .Dec, .day = 31 }).isoWeek().year); } test dayOfWeek { // The epoch was a Thursday. try std.testing.expectEqual(DayOfWeek.Thu, (Date{ .year = 1970, .month = .Jan, .day = 1 }).dayOfWeek()); try std.testing.expectEqual(DayOfWeek.Fri, (Date{ .year = 2024, .month = .Mar, .day = 15 }).dayOfWeek()); } /// Writes this date as a JSON string of a calendar date, `"2024-03-15"`, which is /// what `std.json.Stringify` calls when it meets one, in a field or on its /// own. /// /// Reading refuses a time of day, which a `Date` has nowhere to keep, and a /// date reduced to a month or a year, which does not name a day; see /// `json.readDate`. pub fn jsonStringify(self: Date, jw: anytype) !void { return json.stringify(jw, self, iso8601.writeDate); } test jsonStringify { const text = try std.json.Stringify.valueAlloc(std.testing.allocator, @as(Date, .{ .year = 2024, .month = .Mar, .day = 15 }), .{}); defer std.testing.allocator.free(text); try std.testing.expectEqualStrings("\"2024-03-15\"", text); } /// Reads one of these from the next token of a JSON document, which has to /// be a string; `std.json.parseFromSlice` and its relatives call this when /// they meet the type. See `jsonStringify` for the text, and `json.parse` /// for what happens to the token. /// /// A string that is not the representation is `error.InvalidCharacter`, and /// one whose components are out of range is `error.Overflow`, the errors /// `std.json` gives for a malformed and an oversized number. pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Date { return json.parse(Date, allocator, source, options, json.readDate); } test jsonParse { const Record = struct { value: Date }; const parsed = try std.json.parseFromSlice(Record, std.testing.allocator, "{\"value\":\"2024-03-15\"}", .{}); defer parsed.deinit(); try std.testing.expectEqual(Date{ .year = 2024, .month = .Mar, .day = 15 }, parsed.value.value); try std.testing.expectError( error.InvalidCharacter, std.json.parseFromSlice(Date, std.testing.allocator, "\"not a date\"", .{}), ); } /// Reads one of these from a `std.json.Value` that has already been /// parsed, which has to be a string; `std.json.parseFromValue` calls this /// when it meets the type. See `jsonParse`. pub fn jsonParseFromValue(allocator: std.mem.Allocator, source: std.json.Value, options: std.json.ParseOptions) !Date { _ = allocator; _ = options; return json.parseFromValue(Date, source, json.readDate); } test jsonParseFromValue { const parsed = try std.json.parseFromValue(Date, std.testing.allocator, .{ .string = "2024-03-15" }, .{}); defer parsed.deinit(); try std.testing.expectEqual(Date{ .year = 2024, .month = .Mar, .day = 15 }, parsed.value); }