radio-cli 0.14.0

A lightweight terminal radio and podcast player
Documentation
use jiff::Timestamp;

/// Preserve the UTC offset and fractional precision used by existing SQLite
/// text indexes and the JSON output.
pub(crate) fn format(ts: Timestamp) -> String {
    let nanos = ts.subsec_nanosecond();
    let digits = if nanos == 0 {
        0
    } else if nanos % 1_000_000 == 0 {
        3
    } else if nanos % 1_000 == 0 {
        6
    } else {
        9
    };
    let mut value = format!("{ts:.digits$}");
    value.pop(); // Timestamp displays UTC with a trailing Z.
    value.push_str("+00:00");
    value
}

pub(crate) fn now() -> String {
    format(Timestamp::now())
}

#[cfg(test)]
mod tests {
    #[test]
    fn preserves_stored_timestamp_precision_and_offset() {
        for date in [
            "2026-01-01T00:00:00+00:00",
            "2026-01-01T00:00:00.100+00:00",
            "2026-01-01T00:00:00.000001+00:00",
            "1960-01-01T00:00:00.123456789+00:00",
        ] {
            assert_eq!(super::format(date.parse().unwrap()), date);
        }
    }
}