import Base # Time: civil dates and ISO 8601 (time's job in the Haskell backend: what # Data.Time's showGregorian, iso8601ParseM and iso8601Show do for UTC). # # What it means: a time is a number of seconds since 1970-01-01T00:00Z and # nanoseconds after them (UTC, no leap seconds, as POSIX counts; Bend's Nat # holds 2^48, so milliseconds are one Nat but nanoseconds two); a day is a # number of days since # 1970-01-01. `tm.ymd` and `tm.days` are inverse (Howard Hinnant's # civil_from_days and days_from_civil); probe/Time.bend checks them on # every day of 1970..2100 and on the chat's dates. # # Every def is prefixed `tm.` (a program's defs share one namespace). def tm.pad2(+n: Nat) -> String: Bool.pick(String, Nat.is_lt(n, 10n), "0" ++ Nat.show(n), Nat.show(n)) def tm.civil.m(+mp: Nat) -> Nat: Bool.pick(Nat, Nat.is_lt(mp, 10n), Nat.add(mp, 3n), Nat.sub(mp, 9n)) def tm.civil.ymd(+y: Nat, +m: Nat, +d: Nat) -> String: Nat.show(Bool.pick(Nat, Nat.is_le(m, 2n), Nat.add(y, 1n), y)) ++ "-" ++ tm.pad2(m) ++ "-" ++ tm.pad2(d) def tm.civil.mp(+y: Nat, +doy: Nat, +mp: Nat) -> String: tm.civil.ymd(y, tm.civil.m(mp), Nat.add(Nat.sub(doy, Nat.div(Nat.add(Nat.mul(153n, mp), 2n), 5n)), 1n)) def tm.civil.doy(+y: Nat, +doy: Nat) -> String: tm.civil.mp(y, doy, Nat.div(Nat.add(Nat.mul(5n, doy), 2n), 153n)) def tm.civil.yoe(+era: Nat, +doe: Nat, +yoe: Nat) -> String: tm.civil.doy(Nat.add(yoe, Nat.mul(era, 400n)), Nat.sub(doe, Nat.sub(Nat.add(Nat.mul(365n, yoe), Nat.div(yoe, 4n)), Nat.div(yoe, 100n)))) def tm.civil.doe(+era: Nat, +doe: Nat) -> String: tm.civil.yoe(era, doe, Nat.div(Nat.sub(Nat.add(Nat.sub(doe, Nat.div(doe, 1460n)), Nat.div(doe, 36524n)), Nat.div(doe, 146096n)), 365n)) def tm.civil.z(+z: Nat) -> String: tm.civil.doe(Nat.div(z, 146097n), Nat.sub(z, Nat.mul(Nat.div(z, 146097n), 146097n))) # a day (days since 1970-01-01) as YYYY-MM-DD def tm.ymd(+days: Nat) -> String: tm.civil.z(Nat.add(days, 719468n)) def tm.days.yoe(+era: Nat, +yoe: Nat, +doy: Nat) -> Nat: Nat.sub(Nat.add(Nat.mul(era, 146097n), Nat.add(Nat.sub(Nat.add(Nat.mul(yoe, 365n), Nat.div(yoe, 4n)), Nat.div(yoe, 100n)), doy)), 719468n) def tm.days.y(+y: Nat, +doy: Nat) -> Nat: tm.days.yoe(Nat.div(y, 400n), Nat.sub(y, Nat.mul(Nat.div(y, 400n), 400n)), doy) def tm.days.mp(+mp: Nat, +d: Nat) -> Nat: Nat.add(Nat.div(Nat.add(Nat.mul(153n, mp), 2n), 5n), Nat.sub(d, 1n)) # YYYY-MM-DD as days since 1970-01-01 def tm.days(+y: Nat, +m: Nat, +d: Nat) -> Nat: tm.days.y(Bool.pick(Nat, Nat.is_le(m, 2n), Nat.sub(y, 1n), y), tm.days.mp(Bool.pick(Nat, Nat.is_gt(m, 2n), Nat.sub(m, 3n), Nat.add(m, 9n)), d)) # ISO 8601 # -------- def tm.digit.at(ok: Bool, +c: U32, +n: Nat, more: Nat -> Maybe<&2, Nat>) -> Maybe<&2, Nat>: match ok: case True{}: more(Nat.add(Nat.mul(n, 10n), U32.to_nat((c - 48 : U32)))) case False{}: None{} # a run of decimal digits' value (None: something else in it) def tm.digits.go(s: String, +n: Nat) -> Maybe<&2, Nat>: match s: case SNil{}: Some{n} case SCon{Chr{+c}, t}: tm.digit.at(U32.is_ge(c, 48) && U32.is_le(c, 57), c, n, m => tm.digits.go(t, m)) def tm.num(+s: String) -> Maybe<&2, Nat>: Bool.pick(Maybe<&2, Nat>, String.is_empty(s), None{}, tm.digits.go(s, 0n)) # a fraction's digits as milliseconds (the first three) def tm.frac(+s: String) -> Maybe<&2, Nat>: tm.num(String.take(s ++ "000", 3n)) def tm.part(+s: String, +at: Nat, +n: Nat) -> Maybe<&2, Nat>: tm.num(String.take(String.drop(s, at), n)) def tm.ns.of(y: Maybe<&2, Nat>, mo: Maybe<&2, Nat>, d: Maybe<&2, Nat>, h: Maybe<&2, Nat>, mi: Maybe<&2, Nat>, sec: Maybe<&2, Nat>, f: Maybe<&2, Nat>) -> Maybe<&2, Nat>: match y mo d h mi sec f: case Some{+yy} Some{+mm} Some{+dd} Some{hh} Some{nn} Some{ss} Some{ff}: Some{Nat.add(Nat.mul(Nat.add(Nat.add(Nat.add(Nat.mul(tm.days(yy, mm, dd), 86400n), Nat.mul(hh, 3600n)), Nat.mul(nn, 60n)), ss), 1000n), ff)} case a b c e g k l: None{} # the fraction after the seconds: none, or "." and digits, then "Z" def tm.tail(+s: String) -> Maybe<&2, Nat>: Bool.pick(Maybe<&2, Nat>, String.eq(s, "Z"), Some{0n}, Bool.pick(Maybe<&2, Nat>, String.starts_with(s, ".") && String.ends_with(s, "Z"), tm.frac(String.take(String.drop(s, 1n), Nat.sub(String.length(s), 2n))), None{})) # "YYYY-MM-DDTHH:MM:SS[.f]Z" as milliseconds since 1970 (iso8601ParseM, UTC) def tm.iso.read(+s: String) -> Maybe<&2, Nat>: tm.ns.of(tm.part(s, 0n, 4n), tm.part(s, 5n, 2n), tm.part(s, 8n, 2n), tm.part(s, 11n, 2n), tm.part(s, 14n, 2n), tm.part(s, 17n, 2n), tm.tail(String.drop(s, 19n))) def tm.pad.go(+n: Nat, +s: String) -> String: Bool.pick(String, Nat.is_lt(String.length(s), n), String.repeat("0", Nat.sub(n, String.length(s))) ++ s, s) def tm.drop0.at(z: Bool, +c: U32, t: String, more: Unit -> String) -> String: match z: case True{}: more(Unit{}) case False{}: SCon{Chr{c}, t} def tm.drop0(s: String) -> String: match s: case SNil{}: "" case SCon{Chr{+c}, +t}: tm.drop0.at(U32.is_eq(c, 48), c, t, u => tm.drop0(t)) def tm.trim0(s: String) -> String: String.reverse(tm.drop0(String.reverse(s))) def tm.fraction(+ns: Nat) -> String: Bool.pick(String, Nat.is_eq(ns, 0n), "", "." ++ tm.trim0(tm.pad.go(9n, Nat.show(ns)))) def tm.clock(+secs: Nat) -> String: tm.pad2(Nat.div(secs, 3600n)) ++ ":" ++ tm.pad2(Nat.mod(Nat.div(secs, 60n), 60n)) ++ ":" ++ tm.pad2(Nat.mod(secs, 60n)) # seconds since 1970 and nanoseconds after them, as iso8601Show writes a # UTCTime (the fraction's trailing zeros dropped) def tm.iso.ns(+secs: Nat, +ns: Nat) -> String: tm.ymd(Nat.div(secs, 86400n)) ++ "T" ++ tm.clock(Nat.mod(secs, 86400n)) ++ tm.fraction(ns) ++ "Z" # milliseconds since 1970, so def tm.iso(+ms: Nat) -> String: tm.iso.ns(Nat.div(ms, 1000n), Nat.mul(Nat.mod(ms, 1000n), 1000000n))