use jiff::{civil, tz};
use super::{LocalTimestamp, LocalTimestampFilter, NoUtcPosition};
fn wall() -> civil::DateTime {
civil::date(2021, 8, 14).at(16, 45, 9, 0)
}
#[test]
fn the_three_constructors_agree() {
let offset = tz::Offset::constant(9);
assert_eq!(
LocalTimestamp::wall(wall()),
LocalTimestamp::new(wall(), None)
);
assert_eq!(
LocalTimestamp::at(wall(), offset),
LocalTimestamp::new(wall(), Some(offset))
);
assert_eq!(LocalTimestamp::wall(wall()).offset(), None);
assert_eq!(LocalTimestamp::at(wall(), offset).offset(), Some(offset));
assert!(!LocalTimestamp::wall(wall()).is_zoned());
assert!(LocalTimestamp::at(wall(), offset).is_zoned());
}
#[test]
fn a_stated_utc_is_not_the_same_value_as_no_zone_at_all() {
let stated = LocalTimestamp::at(wall(), tz::Offset::UTC);
let silent = LocalTimestamp::wall(wall());
assert_ne!(stated, silent);
assert_eq!(
stated.to_utc().map(|utc| utc.to_string()).ok(),
Some(String::from("2021-08-14T16:45:09Z"))
);
assert_eq!(silent.to_utc(), Err(NoUtcPosition::Unzoned));
}
#[test]
fn a_zoned_wall_clock_names_the_instant_its_offset_makes_it() {
let capture = LocalTimestamp::at(wall(), tz::Offset::constant(9));
assert_eq!(
capture.to_utc().expect("both halves are here").to_string(),
"2021-08-14T07:45:09Z"
);
assert_eq!(
capture.to_utc_nanos().expect("inside the ceiling"),
1_628_927_109_000_000_000
);
}
#[test]
fn an_unzoned_wall_clock_has_no_instant_and_no_position() {
let capture = LocalTimestamp::wall(wall());
assert_eq!(capture.to_utc(), Err(NoUtcPosition::Unzoned));
assert_eq!(capture.to_utc_nanos(), Err(NoUtcPosition::Unzoned));
}
#[test]
fn a_zoned_wall_clock_past_the_nanosecond_ceiling_has_an_instant_and_no_position() {
for local in [
civil::date(1500, 1, 1).at(0, 0, 0, 0),
civil::date(2500, 1, 1).at(0, 0, 0, 0),
] {
let capture = LocalTimestamp::at(local, tz::Offset::UTC);
assert!(capture.to_utc().is_ok(), "the instant exists");
assert!(
matches!(
capture.to_utc_nanos(),
Err(NoUtcPosition::NanosecondOverflow { .. })
),
"and has no i64 nanosecond position"
);
assert_eq!(capture.local(), local, "the value is untouched by either");
}
}
#[test]
fn the_civil_extremes_can_compose_past_the_instant_range() {
let latest = LocalTimestamp::at(civil::DateTime::MAX, tz::Offset::MIN);
assert_eq!(latest.to_utc(), Err(NoUtcPosition::OutOfRange));
let earliest = LocalTimestamp::at(civil::DateTime::MIN, tz::Offset::MAX);
assert_eq!(earliest.to_utc(), Err(NoUtcPosition::OutOfRange));
}
#[test]
fn the_declared_order_is_the_storage_order_and_not_the_clock() {
let tokyo = LocalTimestamp::at(wall(), tz::Offset::constant(9));
let london = LocalTimestamp::at(wall(), tz::Offset::constant(1));
assert!(tokyo.to_utc().unwrap() < london.to_utc().unwrap());
assert!(tokyo > london);
assert!(LocalTimestamp::wall(wall()) < london);
}
#[test]
fn an_empty_filter_is_empty_through_its_components() {
assert!(LocalTimestampFilter::new().is_empty());
assert!(LocalTimestampFilter::default().is_empty());
assert!(
LocalTimestampFilter::new()
.with_local(crate::ScalarFilter::new())
.is_empty(),
"a component slot holding an empty filter asks nothing"
);
assert!(!LocalTimestampFilter::new().with_zoned(false).is_empty());
assert!(
!LocalTimestampFilter::new()
.with_exact(LocalTimestamp::wall(wall()))
.is_empty()
);
assert!(
!LocalTimestampFilter::new()
.with_local(crate::ScalarFilter::new().with_eq(wall()))
.is_empty()
);
}