mod common;
use common::*;
use flow_value::{Duration, Instant, Int, Value};
fn at(seconds: &str) -> Instant {
Instant::from_unix_seconds(dec(seconds))
}
#[test]
fn a_duration_of_milliseconds_is_an_exact_number_of_seconds() {
assert_eq!(
Duration::from_millis(&Int::from(250)).seconds(),
&dec("0.25")
);
assert_eq!(
Duration::from_millis(&Int::from(90_000)).seconds(),
&dec("90")
);
assert_eq!(
Duration::from_millis(&Int::from(-5)).seconds(),
&dec("-0.005")
);
assert_eq!(Duration::from_seconds(dec("1.5")).seconds(), &dec("1.5"));
}
#[test]
fn a_durations_text_is_pt_seconds_s() {
let text = |millis: i64| Duration::from_millis(&Int::from(millis)).to_string();
assert_eq!(text(90_000), "PT90S");
assert_eq!(text(250), "PT0.25S");
assert_eq!(text(0), "PT0S");
assert_eq!(text(-5000), "PT-5S");
}
#[test]
fn durations_and_instants_order_chronologically() {
let span = |millis: i64| Value::Duration(Duration::from_millis(&Int::from(millis)));
assert!(order(&span(999), &span(1000)).is_lt());
assert!(order(&span(-1), &span(0)).is_lt());
assert!(order(&Value::Instant(at("-1")), &Value::Instant(at("0.5"))).is_lt());
assert!(same(
&Value::Instant(at("1.50")),
&Value::Instant(at("1.5"))
));
}
#[test]
fn adding_a_duration_to_an_instant_is_exact() {
let later = at("10.75")
.add(&Duration::from_millis(&Int::from(250)))
.unwrap();
assert_eq!(later.unix_seconds(), &dec("11"));
let earlier = at("0").add(&Duration::from_millis(&Int::from(-1))).unwrap();
assert_eq!(earlier.unix_seconds(), &dec("-0.001"));
}
#[test]
fn an_instants_text_is_utc_iso_8601_with_the_fewest_exact_fraction_digits() {
assert_eq!(at("0").to_string(), "1970-01-01T00:00:00Z");
assert_eq!(at("1774085400").to_string(), "2026-03-21T09:30:00Z");
assert_eq!(at("1774085400.25").to_string(), "2026-03-21T09:30:00.25Z");
assert_eq!(
at("1774085400.000001").to_string(),
"2026-03-21T09:30:00.000001Z"
);
assert_eq!(at("86399.5").to_string(), "1970-01-01T23:59:59.5Z");
assert_eq!(at("-0.5").to_string(), "1969-12-31T23:59:59.5Z");
assert_eq!(at("-86400").to_string(), "1969-12-31T00:00:00Z");
}
#[test]
fn an_instants_date_follows_the_proleptic_gregorian_calendar() {
assert_eq!(
at("951782400").to_string(),
"2000-02-29T00:00:00Z",
"2000 is a leap year"
);
assert_eq!(at("951868800").to_string(), "2000-03-01T00:00:00Z");
assert_eq!(
at("4107542400").to_string(),
"2100-03-01T00:00:00Z",
"2100 is not"
);
assert_eq!(at("4107456000").to_string(), "2100-02-28T00:00:00Z");
assert_eq!(at("946598400").to_string(), "1999-12-31T00:00:00Z");
assert_eq!(at("-62135596800").to_string(), "0001-01-01T00:00:00Z");
assert_eq!(at("1704067200").to_string(), "2024-01-01T00:00:00Z");
assert_eq!(at("1735603200").to_string(), "2024-12-31T00:00:00Z");
assert_eq!(at("1709164800").to_string(), "2024-02-29T00:00:00Z");
assert_eq!(at("1717200000").to_string(), "2024-06-01T00:00:00Z");
assert_eq!(at("1730419200").to_string(), "2024-11-01T00:00:00Z");
}
#[test]
fn a_year_outside_four_digits_carries_a_sign() {
assert_eq!(at("253402300799").to_string(), "9999-12-31T23:59:59Z");
assert_eq!(at("253402300800").to_string(), "+10000-01-01T00:00:00Z");
assert_eq!(at("-62167219200").to_string(), "0000-01-01T00:00:00Z");
assert_eq!(at("-62167219201").to_string(), "-0001-12-31T23:59:59Z");
assert_eq!(
at("-31557014162035200").to_string(),
"-1000000000-03-01T00:00:00Z"
);
}