use std::collections::BTreeMap;
use super::*;
use crate::temporal::{construct_date, construct_duration, parse_iso};
use crate::Value;
fn dur_map(fields: &[(&str, i64)]) -> Value {
Value::Map(
fields
.iter()
.map(|(key, value)| ((*key).into(), Value::Int(*value)))
.collect::<BTreeMap<_, _>>()
.into(),
)
}
#[test]
fn date_plus_fractional_duration() {
let date = construct_date(Some(&Value::Map(
[
("year".into(), Value::Int(1984)),
("month".into(), Value::Int(10)),
("day".into(), Value::Int(11)),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
.unwrap();
let dur = construct_duration(Some(&Value::Map(
[
("years".into(), Value::Float(12.5)),
("months".into(), Value::Float(5.5)),
("days".into(), Value::Float(14.5)),
("hours".into(), Value::Float(16.5)),
("minutes".into(), Value::Float(12.5)),
("seconds".into(), Value::Float(70.5)),
("nanoseconds".into(), Value::Int(3)),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
.unwrap();
assert_eq!(
add_duration(&date, &dur).unwrap().iso_string(),
"1997-10-11"
);
assert_eq!(
sub_duration(&date, &dur).unwrap().iso_string(),
"1971-10-12"
);
}
#[test]
fn date_plus_duration_months_and_neg_days() {
let d = construct_duration(Some(&dur_map(&[
("months", 1),
("days", -14),
("hours", 16),
("minutes", -12),
("seconds", 70),
])))
.unwrap();
let date = construct_date(Some(&Value::Map(
[
("year".into(), Value::Int(1984)),
("month".into(), Value::Int(10)),
("day".into(), Value::Int(11)),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
.unwrap();
let sum = add_duration(&date, &d).unwrap();
let diff = sub_duration(&date, &d).unwrap();
assert_eq!(sum.iso_string(), "1984-10-28", "sum {sum:?}");
assert_eq!(diff.iso_string(), "1984-09-25", "diff {diff:?}");
}
#[test]
fn scale_duration_half() {
let d = construct_duration(Some(&dur_map(&[
("years", 12),
("months", 5),
("days", 14),
("hours", 16),
("minutes", 12),
("seconds", 70),
("nanoseconds", 1),
])))
.unwrap();
let half = scale_duration(&d, 0.5).unwrap();
assert_eq!(half.iso_string(), "P6Y2M22DT13H21M8S");
}
#[test]
fn scaling_past_the_i64_seconds_field_is_an_error() {
let max = construct_duration(Some(&dur_map(&[("seconds", i64::MAX)]))).unwrap();
assert!(scale_duration(&max, 1.0).is_ok());
assert_eq!(
scale_duration(&max, 2.0),
Err(Error::ty("temporal value out of range"))
);
assert_eq!(
add_duration(&max, &max),
Err(Error::ty("temporal value out of range"))
);
}
#[test]
fn the_average_length_bound_applies_to_every_producer() {
let months = 4_000_000_000_000i64;
assert_eq!(
construct_duration(Some(&dur_map(&[("months", months)]))),
Err(Error::ty("temporal value out of range"))
);
assert_eq!(
parse_iso("duration", "P4000000000000M"),
Err(Error::ty("temporal value out of range"))
);
let value = Temporal::Duration {
months,
days: 0,
nanos: 0,
};
assert_eq!(
scale_duration(&value, 1.0),
Err(Error::ty("temporal value out of range"))
);
assert_eq!(
add_duration(&value, &value),
Err(Error::ty("temporal value out of range"))
);
}
#[test]
fn civil_epoch_days_counts_from_the_epoch() {
assert_eq!(civil_epoch_days(1970, 1, 1), 0);
assert_eq!(civil_epoch_days(2024, 1, 2), 19724);
assert_eq!(civil_epoch_days(1969, 12, 31), -1);
}
#[test]
fn civil_from_days_inverts_civil_epoch_days() {
for &(y, m, d) in &[
(1970i64, 1u32, 1u32),
(2024, 1, 2),
(1969, 12, 31),
(2000, 2, 29),
(1900, 2, 28),
(2100, 3, 1),
(1, 1, 1),
(-1, 12, 31),
] {
let days = civil_epoch_days(y, m, d);
assert_eq!(civil_from_days(days), (y, m, d));
}
assert_eq!(civil_from_days(0), (1970, 1, 1));
}
#[test]
fn negating_a_bound_edge_duration_is_the_interval_field_overflow() {
let edge_nanos = field::MAX_DURATION_SECONDS * 1_000_000_000 + 1;
assert!(Temporal::duration_checked(0, 0, edge_nanos).is_ok());
let edge = Temporal::Duration {
months: 0,
days: 0,
nanos: edge_nanos,
};
assert!(negate_duration(&edge).is_err());
let small = Temporal::Duration {
months: 0,
days: 0,
nanos: 1_000_000_000,
};
assert!(negate_duration(&small).is_ok());
}