Files
typikon/src/lib.rs
T
funkpower 69907eb186 javascript and typescript, as one wasm package for every runtime
WebAssembly rather than a native Node addon: one artifact serves Node,
Deno, Bun, a browser and anything built on them, where napi would have
meant a prebuilt binary per platform and a source build for whoever the
matrix missed. The engine is pure computation and the menologion is
already compiled in, so there is nothing for it to give up.

A day comes back as a plain object, not a handle. A wasm class would
have meant a .free() that every caller eventually forgets, and this way
JSON.stringify works and the thing spreads into state. Two serializer
defaults had to be overridden to keep the promise that it is the same
document --json emits: maps as objects, and an absent field as null
rather than undefined, which stringify would otherwise drop entirely.

The TypeScript is hand-written, because the generated types for a value
crossing as JsValue are `any`, and a binding that types its whole
return as `any` is a binding TypeScript cannot check. tsc --strict is a release
gate now, the way mypy --strict already is, and the check includes three
lines that must fail to compile.

The one place this differs from every other binding: no remembered
default jurisdiction, because a browser has nowhere to keep one. It is
called out in the README, in the types and in docs/versioning.md.

The test resolves typikon through a staged node_modules rather than a
relative path, so what it exercises is the exports map, and it compares
a year of days field by field against the command line for four
jurisdictions.
2026-09-18 14:55:40 -04:00

62 lines
2.0 KiB
Rust

use crate::calendars::Date;
pub mod calendars;
pub mod config;
pub mod day;
pub mod export;
pub mod fasting;
pub mod jurisdictions;
pub mod menaia;
pub mod menologion;
pub mod paschalion;
pub mod rules;
pub mod seasons;
#[cfg(feature = "python")]
mod python;
#[cfg(feature = "capi")]
pub mod capi;
#[cfg(feature = "wasm")]
pub mod wasm;
/// Today, from the system clock, in local time.
///
/// The local offset comes from `date +%z`, because working it out from
/// /etc/localtime means parsing TZif and this program would rather shell out
/// than carry a time zone database. Where `date` is absent, as in a scratch
/// container, the answer is UTC and it says so on stderr rather than quietly
/// reporting the wrong day either side of midnight.
pub fn local_today() -> Date {
let secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0);
let local = secs + local_offset_secs();
// Unix epoch is JDN 2440588. The clock cannot reach either bound.
Date::from_jdn(2440588 + local.div_euclid(86400) as i32).unwrap_or(Date::MIN)
}
/// Seconds east of UTC, or None when it could not be determined.
fn local_offset_secs() -> i64 {
let Ok(out) = std::process::Command::new("date").arg("+%z").output() else {
eprintln!("typikon: `date` is not on PATH; reckoning today in UTC");
return 0;
};
let text = String::from_utf8_lossy(&out.stdout);
let s = text.trim();
// "+HHMM". Checked as bytes so a short or multi-byte reply cannot panic a
// slice, which a str index here would.
let b = s.as_bytes();
if b.len() == 5 && (b[0] == b'+' || b[0] == b'-') && b[1..].iter().all(u8::is_ascii_digit) {
let h = (s[1..3].parse::<i64>()).unwrap_or(0);
let m = (s[3..5].parse::<i64>()).unwrap_or(0);
let mag = h * 3600 + m * 60;
return if b[0] == b'-' { -mag } else { mag };
}
eprintln!("typikon: could not read the local time offset; reckoning today in UTC");
0
}