code wiki / (root) / civil_date.nx

civil_date.nx

buildroot/runtime/civil_date.nx

4974 B145 linesdepth 3pulls 3 transitivereach 5 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

civil_date.nx -- Gregorian civil date from unix seconds. Pairs with ntp.nx (returns unix seconds) to convert seconds into (year, month, day, hour, minute, second, weekday) without depending on glibc strftime or the OS tzdata. UTC only; timezone handling is a separate concern. Uses Howard Hinnant's "civil_from_days" algorithm (the <chrono> library implementation used by libc++). Correct for all years in i64 range (billions of years past and future). Algorithm: z = days since 1970-01-01 Shift epoch to 0000-03-01 so Feb is last: z += 719468 era = z / 146097 (146097 days = 400 years) doe = z % 146097 yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365 y = yoe + era*400 doy = doe - (365*yoe + yoe/4 - yoe/100) mp = (5*doy + 2) / 153 d = doy - (153*mp + 2)/5 + 1 m = mp < 10 ? mp + 3 : mp - 9 y += m <= 2 ? 1 : 0 Invariants: CD1 1970-01-01 00:00:00 UTC <-> unix_sec = 0, all fields returned accordingly (year=1970, month=1, day=1, ...). CD2 Negative unix_sec works (dates before 1970). CD3 Weekday: 0 = Sunday, 1 = Monday, ..., 6 = Saturday (Unix convention; struct tm compatible).

dependencies 1 imports · 3 importers

syscalls.nx civil_date.nx http_date.nx iso8601.nx sitemap.nx

imports: syscalls.nx

imported by: http_date.nxiso8601.nxsitemap.nx

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main civil_from_unix cd_fdiv cd_fmod cd_fdiv ↻ unix_from_civil cd_fdiv ↻

structs

34struct CivilDate {

consts

44const SECONDS_PER_DAY: i64 = 86400

functions

49func cd_fdiv(a: i64, b: i64) -> i64 {
58func cd_fmod(a: i64, b: i64) -> i64 {
called by 1: civil_from_unix calls 1: cd_fdiv
64func civil_from_unix(unix_sec: i64, out: *CivilDate) -> i64 {
97func unix_from_civil(y: i64, m: i64, d: i64,
112func main() -> i64 {