#![allow(clippy::unwrap_used, clippy::expect_used)]
use std::process::Command;
fn tg_int(format_id: &str, value: i64) -> String {
let inst = timeglyph::format(format_id)
.unwrap()
.decode_int(value)
.unwrap();
inst.render(&timeglyph::RenderZone::Utc).unwrap()[..19].to_string()
}
fn tg_float(format_id: &str, value: f64) -> String {
let inst = timeglyph::format(format_id)
.unwrap()
.decode_float(value)
.unwrap();
inst.render(&timeglyph::RenderZone::Utc).unwrap()[..19].to_string()
}
fn run(cmd: &str, args: &[&str]) -> Option<String> {
let out = Command::new(cmd).args(args).output().ok()?;
out.status
.success()
.then(|| String::from_utf8_lossy(&out.stdout).trim().to_string())
}
const UNIX_V: i64 = 1_577_836_800;
#[test]
fn cpython_datetime_agrees_on_unix() {
let code = format!(
"import datetime;print(datetime.datetime.fromtimestamp({UNIX_V},datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S'))"
);
let Some(got) = run("python3", &["-c", &code]) else {
eprintln!("python3 absent — skipping");
return;
};
assert_eq!(got, tg_int("unix", UNIX_V), "CPython vs timeglyph unix");
}
#[test]
fn sqlite3_agrees_on_unix() {
let q = format!("SELECT strftime('%Y-%m-%dT%H:%M:%S',{UNIX_V},'unixepoch')");
let Some(got) = run("sqlite3", &[":memory:", &q]) else {
eprintln!("sqlite3 absent — skipping");
return;
};
assert_eq!(got, tg_int("unix", UNIX_V), "sqlite3 vs timeglyph unix");
}
#[test]
fn sqlite3_julianday_agrees_on_sqlite_julian() {
let Some(jd) = run(
"sqlite3",
&[":memory:", "SELECT julianday('2020-01-01 00:00:00')"],
) else {
eprintln!("sqlite3 absent — skipping");
return;
};
let v: f64 = jd.parse().unwrap();
assert_eq!(
tg_float("sqlite_julian", v),
"2020-01-01T00:00:00",
"sqlite3 julianday vs timeglyph sqlite_julian"
);
}
#[test]
fn gnu_date_agrees_on_unix() {
let Some(got) = run(
"gdate",
&["-u", "-d", &format!("@{UNIX_V}"), "+%Y-%m-%dT%H:%M:%S"],
) else {
eprintln!("gdate (GNU coreutils) absent — skipping");
return;
};
assert_eq!(got, tg_int("unix", UNIX_V), "GNU date vs timeglyph unix");
}
#[test]
fn cpython_agrees_on_unix_float() {
let v = 1_712_345_678.001_2_f64;
assert_eq!(tg_float("unix_float", v), "2024-04-05T19:34:38");
let code = format!(
"import datetime;print(datetime.datetime.fromtimestamp({v},datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S'))"
);
let Some(got) = run("python3", &["-c", &code]) else {
eprintln!("python3 absent — skipping");
return;
};
assert_eq!(
got,
tg_float("unix_float", v),
"CPython vs timeglyph unix_float"
);
}