use crate::errors::AppError;
use crate::i18n::validation;
use chrono::{DateTime, TimeZone, Utc};
use chrono_tz::Tz;
use std::sync::OnceLock;
static GLOBAL_TZ: OnceLock<Tz> = OnceLock::new();
fn resolve_tz_from_env() -> Result<Tz, AppError> {
match std::env::var("SQLITE_GRAPHRAG_DISPLAY_TZ") {
Ok(v) if !v.trim().is_empty() => v
.trim()
.parse::<Tz>()
.map_err(|_| AppError::Validation(validation::invalid_tz(v.trim()))),
_ => Ok(Tz::UTC),
}
}
pub fn init(explicit: Option<Tz>) -> Result<(), AppError> {
let fuso = match explicit {
Some(tz) => tz,
None => resolve_tz_from_env()?,
};
let _ = GLOBAL_TZ.set(fuso);
Ok(())
}
pub fn current_tz() -> Tz {
*GLOBAL_TZ.get_or_init(|| resolve_tz_from_env().unwrap_or(Tz::UTC))
}
pub fn format_iso(ts: DateTime<Utc>) -> String {
let fuso = current_tz();
ts.with_timezone(&fuso)
.format("%Y-%m-%dT%H:%M:%S%:z")
.to_string()
}
pub fn epoch_to_iso(epoch: i64) -> String {
Utc.timestamp_opt(epoch, 0)
.single()
.map(format_iso)
.unwrap_or_else(|| "1970-01-01T00:00:00+00:00".to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
#[test]
#[serial]
fn utc_default_when_env_missing() {
std::env::remove_var("SQLITE_GRAPHRAG_DISPLAY_TZ");
let result = resolve_tz_from_env().expect("must not fail with env absent");
assert_eq!(result, Tz::UTC);
}
#[test]
#[serial]
fn env_valid_applies_timezone() {
std::env::set_var("SQLITE_GRAPHRAG_DISPLAY_TZ", "America/Sao_Paulo");
let result = resolve_tz_from_env().expect("America/Sao_Paulo is valid");
assert_eq!(result.name(), "America/Sao_Paulo");
std::env::remove_var("SQLITE_GRAPHRAG_DISPLAY_TZ");
}
#[test]
#[serial]
fn env_invalid_returns_validation_error() {
std::env::set_var("SQLITE_GRAPHRAG_DISPLAY_TZ", "Invalid/Nonexistent");
let result = resolve_tz_from_env();
assert!(result.is_err(), "invalid timezone must return Err");
match result {
Err(AppError::Validation(msg)) => {
assert!(
msg.contains("SQLITE_GRAPHRAG_DISPLAY_TZ"),
"message must cite the env var"
);
assert!(
msg.contains("Invalid/Nonexistent"),
"message must cite the invalid value"
);
}
other => unreachable!("expected AppError::Validation, got: {other:?}"),
}
std::env::remove_var("SQLITE_GRAPHRAG_DISPLAY_TZ");
}
#[test]
fn epoch_zero_yields_utc_iso() {
std::env::remove_var("SQLITE_GRAPHRAG_DISPLAY_TZ");
let result = {
let tz = Tz::UTC;
Utc.timestamp_opt(0, 0)
.single()
.map(|dt| {
dt.with_timezone(&tz)
.format("%Y-%m-%dT%H:%M:%S%:z")
.to_string()
})
.unwrap_or_else(|| "1970-01-01T00:00:00+00:00".to_string())
};
assert_eq!(result, "1970-01-01T00:00:00+00:00");
}
#[test]
fn format_iso_utc_preserves_zero_offset() {
let ts = Utc.timestamp_opt(1_705_320_000, 0).single().unwrap();
let result = ts
.with_timezone(&Tz::UTC)
.format("%Y-%m-%dT%H:%M:%S%:z")
.to_string();
assert_eq!(result, "2024-01-15T12:00:00+00:00");
}
#[test]
fn format_iso_sao_paulo_applies_offset() {
let ts = Utc.timestamp_opt(1_705_320_000, 0).single().unwrap();
let sao_paulo: Tz = "America/Sao_Paulo".parse().unwrap();
let result = ts
.with_timezone(&sao_paulo)
.format("%Y-%m-%dT%H:%M:%S%:z")
.to_string();
assert!(
result.contains("-03:00"),
"expected offset -03:00, got: {result}"
);
}
}