a C ABI, so this can be embedded rather than spawned

Every desktop shell that reads typikon today runs the binary and parses
its JSON. That costs about 1.5ms and is the right answer for a calendar
that changes once a day, but it is the wrong shape for anything with an
FFI and no business spawning a process on every redraw.

Dates cross as Julian Day Numbers, so no struct layout is part of the
contract. A day is opaque and owns its strings, built once when it is
reckoned: an accessor returns the same borrowed pointer every time, so a
statusline polling one in a loop allocates nothing. The accessors carry
every field of the JSON, so nothing on the other side needs a parser.

The Python bindings and this were resolving a jurisdiction by two copies
of the same logic, and the copy here leaked one per call. Both now share
jurisdictions::resolve_full, which interns the combinations instead.

The header is a promise made in a second file, and a second file drifts:
a test reads both and fails if either has a symbol the other does not,
and does the same for the LuaJIT cdef, where a stale declaration is a
segfault rather than a compile error.
This commit is contained in:
funkpower
2026-09-18 14:46:25 -04:00
parent 452d7fc0a1
commit d90d4c171f
14 changed files with 1523 additions and 98 deletions
+44
View File
@@ -1,3 +1,5 @@
use crate::calendars::Date;
pub mod calendars;
pub mod config;
pub mod day;
@@ -12,3 +14,45 @@ pub mod seasons;
#[cfg(feature = "python")]
mod python;
#[cfg(feature = "capi")]
pub mod capi;
/// 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
}