feat(stdlib): Time/Date/Duration calendar-clock core (issue #9)
Some checks are pending
docs / build-and-deploy (push) Waiting to run

Implement the calendar/clock half of #9 as plain-i32 integer epochs — no
new type, no floating point (the issue's "integer epochs to avoid drift") —
so every operation is deterministic and bit-identical on every platform:

  Duration — a span in whole seconds; seconds/minutes/hours/days build one,
             as_seconds/as_minutes/as_hours/as_days read it back. Because a
             duration is just an int, `+` and `>` work with no extra machinery
             (Duration.minutes(5) + Duration.seconds(30), away > Duration.hours(3)).
  Date     — a civil day as days-since-1970 (UTC): new/year/month/day/weekday/
             is_leap/days_in_month/to_epoch/add_days/diff_days.
  DateTime — an instant as seconds-since-1970 (UTC, matching Time.now):
             from/date/add/year/month/day/weekday/hour/minute/second.
  Time.since(past) = now - past, for offline-progress / "time away" checks.

New selfhost/emit_datetime.ludic (is_/emit_ for the three namespaces, wired
into emit_ns_call + the frag list). The two civil<->epoch conversions are
Howard Hinnant's public-domain proleptic-Gregorian algorithms, emitted once
per program as the @fn_days_from_civil / @fn_civil_from_days prelude and gated
by g_uses_datert; days_in_month is next-month-day-0 (no lookup table). Time
gains `since`. Docs (Duration/Date/DateTime sections, 28 method pages +
time-since), inventory, and LSP hover kept in sync; a registered test checks
component math against hand-computed values. Reseeded; C-free fixpoint holds;
all suites green (26 self-host / 45 regression / 29 tools); check.py,
check-impl.py and validate.py OK.

format/parse, a game-controlled simulated clock, and timezones are tracked
follow-ups; v1 is UTC-only and, on the i32 epoch, valid through 2038.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-30 03:37:38 +03:00
parent 121053e179
commit 1a2c6ec2c7
43 changed files with 10670 additions and 7626 deletions

View file

@ -315,7 +315,8 @@
"time-frame",
"time-delta",
"time-elapsed",
"time-now"
"time-now",
"time-since"
],
"types": [
"type-bool",
@ -366,5 +367,39 @@
"vector-rotate",
"vector-angle",
"vector-from_angle"
],
"duration": [
"duration-seconds",
"duration-minutes",
"duration-hours",
"duration-days",
"duration-as_seconds",
"duration-as_minutes",
"duration-as_hours",
"duration-as_days"
],
"date": [
"date-new",
"date-year",
"date-month",
"date-day",
"date-weekday",
"date-is_leap",
"date-days_in_month",
"date-to_epoch",
"date-add_days",
"date-diff_days"
],
"datetime": [
"datetime-from",
"datetime-date",
"datetime-add",
"datetime-year",
"datetime-month",
"datetime-day",
"datetime-weekday",
"datetime-hour",
"datetime-minute",
"datetime-second"
]
}

View file

@ -1401,6 +1401,41 @@ program LudicLsp {
if (meth == "delta") { return "Time.delta() -> fixed" }
if (meth == "elapsed") { return "Time.elapsed() -> fixed" }
if (meth == "now") { return "Time.now() -> int" }
if (meth == "since") { return "Time.since(past) -> int" }
}
if (ns == "Duration") {
if (meth == "seconds") { return "Duration.seconds(n) -> int" }
if (meth == "minutes") { return "Duration.minutes(n) -> int" }
if (meth == "hours") { return "Duration.hours(n) -> int" }
if (meth == "days") { return "Duration.days(n) -> int" }
if (meth == "as_seconds") { return "Duration.as_seconds(span) -> int" }
if (meth == "as_minutes") { return "Duration.as_minutes(span) -> int" }
if (meth == "as_hours") { return "Duration.as_hours(span) -> int" }
if (meth == "as_days") { return "Duration.as_days(span) -> int" }
}
if (ns == "Date") {
if (meth == "new") { return "Date.new(year, month, day) -> int" }
if (meth == "year") { return "Date.year(date) -> int" }
if (meth == "month") { return "Date.month(date) -> int" }
if (meth == "day") { return "Date.day(date) -> int" }
if (meth == "weekday") { return "Date.weekday(date) -> int" }
if (meth == "is_leap") { return "Date.is_leap(year) -> bool" }
if (meth == "days_in_month") { return "Date.days_in_month(year, month) -> int" }
if (meth == "to_epoch") { return "Date.to_epoch(date) -> int" }
if (meth == "add_days") { return "Date.add_days(date, n) -> int" }
if (meth == "diff_days") { return "Date.diff_days(a, b) -> int" }
}
if (ns == "DateTime") {
if (meth == "from") { return "DateTime.from(year, month, day, hour, minute, second) -> int" }
if (meth == "date") { return "DateTime.date(dt) -> int" }
if (meth == "add") { return "DateTime.add(dt, span) -> int" }
if (meth == "year") { return "DateTime.year(dt) -> int" }
if (meth == "month") { return "DateTime.month(dt) -> int" }
if (meth == "day") { return "DateTime.day(dt) -> int" }
if (meth == "weekday") { return "DateTime.weekday(dt) -> int" }
if (meth == "hour") { return "DateTime.hour(dt) -> int" }
if (meth == "minute") { return "DateTime.minute(dt) -> int" }
if (meth == "second") { return "DateTime.second(dt) -> int" }
}
if (ns == "Memory") {
if (meth == "bytes") { return "Memory.bytes(n) -> pointer" }

View file

@ -33,6 +33,7 @@ function selfhost_frags() -> []pointer {
push(f, "selfhost/emit_collide.ludic")
push(f, "selfhost/emit_mem.ludic")
push(f, "selfhost/emit_time.ludic")
push(f, "selfhost/emit_datetime.ludic")
push(f, "selfhost/emit_colorfn.ludic")
push(f, "selfhost/emit_color.ludic")
push(f, "selfhost/emit_new.ludic")

View file

@ -47,6 +47,7 @@ function cmd_selfhost_test() -> int {
sh_case("math3", "45 90 180 -135 30 60 90")
sh_case("transcend", "2718 1 148 1000 0 2303 1024 1414 3 250 0 1000 1016")
sh_case("vector", "3 4 5 4 6 2 2 11 2 6 999 999 999 1570 5 10")
sh_case("datetime", "10800 330 172800 3 2 0 10957 2026 8 30 4 6 1 0 1 29 28 31 2 1 365 946729815 2000 1 1 12 30 15 6 10957 1")
sh_case("textsplit", "1 1 1 3 1 1 1 1 1 1 1")
sh_case("hash", "-2128831035 -468965076 114400290 114400290 0 -873187034 1095738169 0 1364076727 -2114883783 -845898438 -845898438 -78065325 -78057399 -3750763034362895579 -5808556873153909620 -4100651535478758590 -4100651535478758590 0 -5451962507482445012 7256831767414464289")
sh_case("long", "1000000000000 1000000000001 1000000000005 3000000000000 1 1 1 -1000000000000 1000000 13")