Expand description
Exact calendar primitives — the bedrock of the time tower.
Everything here is exact integer arithmetic over a single absolute coordinate: the day count since the Unix epoch (1970-01-01). Two calendars are projected onto that coordinate — the proleptic Gregorian calendar (the civil default) and the proleptic Julian calendar (for historical dates and the Gregorian/Julian divergence) — so a date in either calendar is just a different lens on the same day number, and converting between them is lossless.
The Gregorian conversions are Howard Hinnant’s branch-light days_from_civil/civil_from_days
(valid for any year, no lookup tables). The Julian conversions go through the Julian Day Number
(JDN); the epoch 1970-01-01 is JDN 2440588. ISO-8601 week dates are derived from the day
number directly. Weekday is 0 = Sunday … 6 = Saturday; ISO weekday is 1 = Monday … 7 = Sunday.
Structs§
- Civil
Date Time - A civil (wall-clock) date-time, calendar + time-of-day, with no zone — the human-readable decomposition of a SmoothUTC instant (nanoseconds since the Unix epoch, leap-second-free).
- DstRule
- A POSIX-style DST transition rule: the
week-thweekdayofmonth, attime_secondslocal.weekis1..=5(5 = the last occurrence);weekdayis0 = Sunday … 6 = Saturday. This is theMm.w.dform a TZ string uses (e.g.M3.2.0= the 2nd Sunday of March), and it generates one transition per year — enough to express any zone with a regular DST schedule. - Estimate
Interval - A bound on a remote node’s current time: it lies somewhere in
[lower_nanos, upper_nanos]. The width is the irreducible uncertainty — across a light-delay you can never pin a remote clock tighter than the physics allows. Theintersectof two valid bounds is a lattice meet (it never widens), which is what makes aSyncClockconflict-free. - Hlc
- A Hybrid Logical Clock timestamp: a physical instant (nanoseconds) plus a logical counter
that breaks ties and preserves causality when the physical clock stalls, jumps backward, or two
events share a nanosecond. The total order is lexicographic
(physical_nanos, logical)— so the derivedOrdis the HLC order. This is the causally-consistent timestamp theSyncClockCRDT keys on: it stays monotonic and orders happens-before correctly even across machines whose wall clocks disagree (the bounded-skew / interplanetary case the campaign targets). - Sync
Clock - A light-cone-aware CRDT clock: a Hybrid Logical Clock (causal order) plus per-node knowledge
intervals (light-cone-bounded estimates of each peer’s current time). Its
mergeis a join over a lattice —maxon the HLC, interval intersection per node — so replicas separated by bounded (even interplanetary) delay converge conflict-free, regardless of message order. This realises “spacelike separation = CRDT concurrency”: events with no shared light cone need no total order, only this commutative/associative/idempotent merge. - Zone
Spec - A named time zone: its standard UTC offset, and an optional DST schedule
(dst_offset, spring rule, fall rule). A zone withdst = Noneis a fixed offset (UTC, IST, JST). DST transitions are generated per year from the rules, so aZoneSpecis tiny — no transition table. - Zone
Transition - A UTC-offset transition: from
at_unix_seconds(a UTC instant) onward, the zone’s local time isUTC + offset_seconds. A timezone is a list of these, sorted ascending — the shape every IANA zone reduces to. DST is just two alternating offsets with transitions twice a year.
Enums§
- Fold
- How to resolve a local civil time that maps ambiguously to UTC: in a DST fold (the hour
repeated when clocks fall back, so a local time occurs twice) or a gap (the hour skipped
when clocks spring forward, so a local time never occurs).
Earlierpicks the earlier instant,Laterthe later one.
Constants§
- NANOS_
PER_ DAY - Nanoseconds in one civil day.
- NANOS_
PER_ SECOND - Nanoseconds in one SI second.
- SECONDS_
PER_ DAY - Seconds in one civil day (SmoothUTC: no leap seconds, so every day is exactly 86 400 s).
- TT_
MINUS_ TAI_ NANOS - The fixed
TT − TAIoffset: Terrestrial Time runs 32.184 s ahead of atomic time, exactly. - UNIX_
EPOCH_ JDN - The Julian Day Number of the Unix epoch (1970-01-01, Gregorian).
Functions§
- add_
business_ days - Advance a day number by
nbusiness days (skipping weekends):n > 0moves forward,n < 0backward. Landing rule is “next business day in the direction of travel”, soadd_business_days(friday, 1)is the following Monday andadd_business_days(monday, -1)is the preceding Friday. - add_
span - Add a calendar span (
monthsthendays) to a civil date-time — the civil (wall-clock) operation, distinct from adding a physicalDuration. Months are applied first with end-of-month clamping (Jan 31 + 1 month = Feb 28/29), then wholedaysroll over exactly through the day number. The time-of-day is preserved. (Spanarithmetic on a zoned value is where it diverges from a physical duration across a DST boundary; in UTC the two agree.) - business_
days_ between - The count of business days (Mon–Fri) in the half-open interval between two day numbers.
Signed by direction (
b < ayields a negative count);[a, b)so adjacent days differ by 1. - civil_
from_ days - The inverse of
days_from_civil: the Gregorian(year, month, day)of a day number. - civil_
from_ unix_ nanos - Decompose a SmoothUTC instant (nanoseconds since 1970-01-01T00:00:00) into its civil date-time.
Floor division makes pre-epoch instants correct (e.g.
-1 nsis1969-12-31T23:59:59.999999999). - day_
of_ year - The 1-based day of the Gregorian year (
1..=366). - days_
from_ civil - Days since 1970-01-01 for a Gregorian civil date. Howard Hinnant’s algorithm — exact for
any year,
monthin1..=12,dayin1..=last_day_of_month. - days_
from_ iso_ week - The inverse of
iso_week_from_days: the day number of an ISO-8601 week date(iso_year, week, iso_weekday)(week1..=53, weekday1..=7Monday … Sunday). - days_
from_ julian - Days since 1970-01-01 for a Julian-calendar date.
- dst_
transitions_ for_ year - The two UTC transitions for
yearof a zone with a regular DST schedule: spring-forward (std_offset → dst_offset) atstart, fall-back (dst_offset → std_offset) atend. Each rule’stime_secondsis wall-clock local in the offset in effect just before the transition. - format_
duration - Format a physical duration (nanoseconds) as a compact human string —
1h30m,500ms,1.5s,2d,-45m,0s. Largest unit first; zero components omitted; sub-second fractions trimmed. The inverse ofparse_duration. (This is a physical elapsed time, distinct from a calendarSpan: it has no months — those aren’t a fixed number of nanoseconds.) - format_
rfc3339 - Format a SmoothUTC instant as an RFC 3339 / ISO 8601 UTC timestamp (
Zzone), e.g.1970-01-01T00:00:00Zor2024-03-10T07:30:00.123456789Z. Sub-second precision appears only when nonzero, with trailing zeros trimmed. - format_
time_ of_ day - Format a time-of-day (nanoseconds from midnight) as
HH:MM:SS, with a trailing sub-second fraction only when nonzero (trailing zeros trimmed) — e.g.07:30:45,16:00:00,07:30:45.5. The clock face is lossless to the nanosecond; the input is normalised modulo a day so a negative or overflowing value still maps onto[00:00:00, 24:00:00). - format_
zoned - Format a UTC instant as the local wall-clock time in a named zone, with its offset —
2024-07-01T08:00:00-04:00(the timezone-aware “relative read” of an instant).Noneif the zone is unknown. The space-aware/zoned generalisation offormat_rfc3339. - from_
local - The UTC instant of a local civil date-time in a zone. A local time normally maps to exactly one
instant; in a DST fold it maps to two (resolved by
fold); in a DST gap it maps to none (the boundary instants are returned,foldchoosing which). This is the offset-dependent inverse ofto_local, solved by testing each candidate offset for self-consistency. - is_
gregorian_ leap - True if
yearis a leap year in the proleptic Gregorian calendar (every 4th year, except centuries, except every 400th). - is_
julian_ leap - True if
yearis a leap year in the proleptic Julian calendar (every 4th year, no century exception). - is_
weekend - True if a day number falls on a weekend (Saturday or Sunday).
- iso_
week_ from_ days - The ISO-8601 week date of a day number:
(iso_year, week, iso_weekday), withweekin1..=53andiso_weekdayin1..=7(Monday … Sunday). The ISO year can differ from the civil year near January 1 / December 31 (e.g. 2021-01-01 is ISO 2020-W53-5). - iso_
weekday_ from_ days - The ISO-8601 weekday of a day number:
1 = Monday … 7 = Sunday. - jdn_
from_ julian - The Julian Day Number of a Julian-calendar date (proleptic,
monthin1..=12). - julian_
from_ days - The inverse: the Julian-calendar
(year, month, day)of a day number. - julian_
from_ jdn - The inverse of
jdn_from_julian: the Julian-calendar(year, month, day)of a JDN. - knowable_
horizon - The freshest remote time you can possibly know: an event happening at a node
light_delay_nanosaway cannot be observed beforenow − light_delay. This is the light-cone horizon — the reason a distributed clock’s knowledge is principled-uncertain rather than sloppy. - last_
day_ of_ month - The last day (28–31) of a Gregorian month.
- local_
instant_ nanos - The local-as-UTC instant of a UTC instant in a named zone: the nanoseconds that, read back
with the plain UTC calendar functions, yield the zone’s local wall-clock components. This is
the lowering target for zoned component reads (
the hour of m in "Asia/Tokyo"): a local clock face encoded as an instant, so every UTC extractor composes onto it unchanged.Noneif the zone is unknown. (Not a real instant — it is the wall clock; do not re-format it with aZ.) - months_
between - The number of complete calendar months from instant
ato instantb(signed; negative whenbprecedesa). A month is complete only whenbreaches at leasta + k monthsunder the same end-of-month-clamping, time-of-day-preserving rule asadd_span— soJan 15 → Mar 14is 1 month (not 2),Jan 31 → Feb 28(non-leap) is a full month, and one hour short of a month is 0. - nth_
weekday_ of_ month - The day-of-month of the
week-thweekdayof(year, month)— e.g. the 2nd Sunday of March, or (withweek = 5) the last Sunday of October.weekis1..=5,weekday0 = Sun … 6 = Sat. - offset_
at - The UTC offset (seconds) a zone has at
instant_ns.transitionsmust be sorted ascending byat_unix_seconds;base_offsetapplies before the first transition. - parse_
duration - Parse a physical duration string into nanoseconds —
1h30m,90m,1.5s,500ms,2d,-1h,1d2h3m4s. Units:d h m s ms us/µsns.Noneon malformed input or i64 overflow. - parse_
rfc3339 - Parse an RFC 3339 / ISO 8601 timestamp into a SmoothUTC instant (nanoseconds since epoch).
Accepts a
Zzone or a numeric±HH:MMoffset (normalized to UTC);Noneon malformed input. - tai_
minus_ utc TAI − UTC(whole seconds) in effect at a SmoothUTC/Unix instant — the count of leap seconds (plus the 1972 base of 10) inserted on/before it. Constant between leap seconds;10before 1972.- tai_
nanos_ from_ unix_ nanos - Convert a SmoothUTC/Unix instant in nanoseconds to TAI nanoseconds (leap-second exact).
- tai_
to_ unix_ seconds - The inverse: TAI seconds back to a SmoothUTC/Unix instant. The offset is monotone in UTC, so
a first guess (
tai − offset(tai)) corrected againstoffset(guess)converges in one step. - to_
local - The local civil date-time of a UTC instant in a zone (apply the offset in effect).
- tt_
nanos_ from_ unix_ nanos - Convert a SmoothUTC/Unix instant in nanoseconds to Terrestrial Time (TT) nanoseconds:
TT = TAI + 32.184 s— the continuous astronomical scale ephemerides are tabulated against. - unix_
nanos_ from_ civil - The inverse: the SmoothUTC instant (nanoseconds since the epoch) of a civil date-time.
- unix_
nanos_ from_ tt_ nanos - The inverse: TT nanoseconds back to a SmoothUTC/Unix instant in nanoseconds.
- unix_
to_ tai_ seconds - Convert a SmoothUTC/Unix instant (seconds) to TAI (atomic time) seconds:
TAI = UTC + (TAI−UTC). - weekday_
from_ days - The weekday of a day number:
0 = Sunday … 6 = Saturday. (1970-01-01 was a Thursday = 4.) - years_
between - The number of complete calendar years from
atob(signed) — complete 12-month periods, so2020-06-01 → 2024-03-01is 3 (the fourth year, ending 2024-06-01, has not arrived). - zone_
by_ name - Look up a
ZoneSpecby IANA name (e.g."America/New_York","UTC","Australia/Sydney").Nonefor an unknown zone. A growable registry seeded with common zones (POSIX-rule based); the full IANA set ingests later.