# datekit: calendar dates and the arithmetic on them. # Pure: it needs no permissions, so it can be installed with no `allow` line. # # A date is a list [year, month, day], like [2026, 12, 5]. Weekdays count from Monday = 0 to Sunday = 6. # It uses the Gregorian calendar throughout, including for years before it was adopted. # # use datekit from "cresset_packages/datekit.crs" # print(datekit.add_days([2026, 12, 30], 3)) # [2027, 1, 2] fn is_leap(year): return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0 fn days_in_month(year, month): if month == 2: if is_leap(year): return 29 return 28 if month in [4, 6, 9, 11]: return 30 return 31 fn is_valid(year, month, day): return month >= 1 and month <= 12 and day >= 1 and day <= days_in_month(year, month) # whole days since 1970-01-01 (negative before it) fn to_days(date): y = date[0] m = date[1] d = date[2] if m <= 2: y -= 1 era = floor(y / 400) yoe = y - era * 400 shifted = m - 3 if m <= 2: shifted = m + 9 doy = floor((153 * shifted + 2) / 5) + d - 1 doe = yoe * 365 + floor(yoe / 4) - floor(yoe / 100) + doy return era * 146097 + doe - 719468 # the date that many days since 1970-01-01 fn from_days(days): z = days + 719468 era = floor(z / 146097) doe = z - era * 146097 yoe = floor((doe - floor(doe / 1460) + floor(doe / 36524) - floor(doe / 146096)) / 365) doy = doe - (365 * yoe + floor(yoe / 4) - floor(yoe / 100)) mp = floor((5 * doy + 2) / 153) day = doy - floor((153 * mp + 2) / 5) + 1 month = mp - 9 if mp < 10: month = mp + 3 year = yoe + era * 400 if month <= 2: year += 1 return [year, month, day] fn add_days(date, n): return from_days(to_days(date) + n) # how many days from `a` to `b` (negative when b is earlier) fn days_between(a, b): return to_days(b) - to_days(a) # 0 for Monday up to 6 for Sunday fn weekday(date): return (to_days(date) + 3) % 7 fn weekday_name(date): return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"][weekday(date)] fn is_weekend(date): return weekday(date) >= 5 # add months, keeping the day when it fits and otherwise using the last day of that month fn add_months(date, n): total = date[0] * 12 + (date[1] - 1) + n year = floor(total / 12) month = total % 12 + 1 day = date[2] last = days_in_month(year, month) if day > last: day = last return [year, month, day] # 2026-12-05 fn format_date(date): return "{date[0]}-{str(date[1]).pad_left(2, "0")}-{str(date[2]).pad_left(2, "0")}" # "2026-12-05" becomes ok([2026, 12, 5]); anything else is an err fn parse_date(text): parts = text.trim().split("-") if parts.len() != 3: return err("a date looks like 2026-12-05, not '{text}'") y = to_int(parts[0])? m = to_int(parts[1])? d = to_int(parts[2])? if not is_valid(y, m, d): return err("{text} is not a real date") return ok([y, m, d]) test "leap years and month lengths": assert_eq([is_leap(2000), is_leap(1900), is_leap(2024), is_leap(2026)], [true, false, true, false]) assert_eq(days_in_month(2024, 2), 29) assert_eq(days_in_month(2026, 2), 28) assert_eq([days_in_month(2026, 4), days_in_month(2026, 12)], [30, 31]) test "days since 1970 agree with known dates": assert_eq(to_days([1970, 1, 1]), 0) assert_eq(to_days([2026, 12, 5]), 20792) assert_eq(to_days([2000, 2, 29]), 11016) assert_eq(to_days([1969, 7, 20]), -165) assert_eq(to_days([1900, 3, 1]), -25508) test "to_days and from_days undo each other": for days in [-100000, -25508, -165, 0, 1, 59, 60, 11016, 20792, 50000]: assert_eq(to_days(from_days(days)), days) assert_eq(from_days(20792), [2026, 12, 5]) assert_eq(from_days(0), [1970, 1, 1]) test "adding days crosses months and years": assert_eq(add_days([2026, 12, 30], 3), [2027, 1, 2]) assert_eq(add_days([2024, 2, 28], 2), [2024, 3, 1]) assert_eq(add_days([2026, 3, 1], -1), [2026, 2, 28]) assert_eq(days_between([2026, 1, 1], [2026, 12, 31]), 364) assert_eq(days_between([2100, 2, 28], [2100, 3, 1]), 1) assert_eq(days_between([2026, 5, 1], [2026, 4, 1]), -30) test "weekdays": assert_eq(weekday([2026, 12, 5]), 5) assert_eq(weekday_name([2024, 3, 1]), "Friday") assert_eq(weekday_name([1970, 1, 1]), "Thursday") assert_eq(is_weekend([2026, 12, 5]), true) assert_eq(is_weekend([2026, 12, 7]), false) test "adding months clamps the day": assert_eq(add_months([2026, 1, 31], 1), [2026, 2, 28]) assert_eq(add_months([2024, 1, 31], 1), [2024, 2, 29]) assert_eq(add_months([2026, 11, 15], 3), [2027, 2, 15]) assert_eq(add_months([2026, 1, 15], -2), [2025, 11, 15]) test "formatting and parsing": assert_eq(format_date([2026, 3, 7]), "2026-03-07") assert_eq(parse_date("2026-12-05"), ok([2026, 12, 5])) assert_eq(parse_date(" 2024-02-29 "), ok([2024, 2, 29])) assert_eq(parse_date("2026-02-29").is_err(), true) assert_eq(parse_date("2026-13-01").is_err(), true) assert_eq(parse_date("12/05/2026").is_err(), true) assert_eq(parse_date("2026-xx-01").is_err(), true)