use jiff::{Timestamp, Zoned, civil, tz, tz::TimeZone};
use super::TemporalStorage;
#[test]
fn the_jiff_instant_range_sits_inside_times_own() {
let time_min = time::OffsetDateTime::new_in_offset(
time::Date::MIN,
time::Time::MIDNIGHT,
time::UtcOffset::UTC,
)
.unix_timestamp_nanos();
let time_max =
time::OffsetDateTime::new_in_offset(time::Date::MAX, time::Time::MAX, time::UtcOffset::UTC)
.unix_timestamp_nanos();
assert!(Timestamp::MIN.as_nanosecond() >= time_min);
assert!(Timestamp::MAX.as_nanosecond() <= time_max);
const OFFSET_MAX_NANOS: i128 = 93_599 * 1_000_000_000;
assert_eq!(Timestamp::MIN.as_nanosecond() - time_min, OFFSET_MAX_NANOS);
assert_eq!(time_max - Timestamp::MAX.as_nanosecond(), OFFSET_MAX_NANOS);
}
#[test]
fn an_i64_nanosecond_intermediate_is_what_would_have_made_it_fallible() {
assert!(i64::try_from(Timestamp::MIN.as_nanosecond()).is_err());
assert!(i64::try_from(Timestamp::MAX.as_nanosecond()).is_err());
}
#[test]
fn a_timestamp_carries_its_extremes() {
for instant in [Timestamp::MIN, Timestamp::MAX, Timestamp::UNIX_EPOCH] {
assert_eq!(
instant.to_storage().unix_timestamp_nanos(),
instant.as_nanosecond(),
"{instant} did not survive the crossing"
);
}
}
#[test]
fn a_timestamp_carries_its_subsecond_nanoseconds() {
let instant: Timestamp = "2024-05-06T07:08:09.123456789Z".parse().unwrap();
let stored = instant.to_storage();
assert_eq!(stored.nanosecond(), 123_456_789);
assert_eq!(stored.unix_timestamp_nanos(), instant.as_nanosecond());
}
#[test]
fn a_timestamp_before_the_epoch_carries_its_subsecond_nanoseconds() {
let instant = Timestamp::from_nanosecond(-1).unwrap();
assert_eq!(instant.subsec_nanosecond(), -1);
let stored = instant.to_storage();
assert_eq!(stored.unix_timestamp_nanos(), -1);
assert_eq!(stored.nanosecond(), 999_999_999);
}
#[test]
fn a_zoned_stores_its_instant_in_utc() {
let instant: Timestamp = "2024-05-06T07:08:09.123456789Z".parse().unwrap();
let tokyo = instant.to_zoned(TimeZone::fixed(jiff::tz::offset(9)));
let stored = tokyo.to_storage();
assert_eq!(stored.offset(), time::UtcOffset::UTC);
assert_eq!(stored.unix_timestamp_nanos(), instant.as_nanosecond());
assert_eq!(stored, instant.to_storage());
}
#[test]
fn two_zones_over_one_instant_store_one_value() {
let instant: Timestamp = "2024-05-06T07:08:09Z".parse().unwrap();
let east = instant.to_zoned(TimeZone::fixed(jiff::tz::offset(9)));
let west = instant.to_zoned(TimeZone::fixed(jiff::tz::offset(-8)));
assert_ne!(east.datetime(), west.datetime());
assert_eq!(east.to_storage(), west.to_storage());
assert_eq!(east.to_storage().offset(), west.to_storage().offset());
assert_eq!(east.to_storage().time(), west.to_storage().time());
}
#[test]
fn a_zoned_carries_its_extremes() {
for instant in [Timestamp::MIN, Timestamp::MAX] {
let zoned = instant.to_zoned(TimeZone::UTC);
assert_eq!(
zoned.to_storage().unix_timestamp_nanos(),
instant.as_nanosecond()
);
}
}
#[test]
fn a_zoned_at_the_widest_offsets_carries_its_extremes() {
for (instant, offset) in [
(Timestamp::MIN, jiff::tz::Offset::MIN),
(Timestamp::MAX, jiff::tz::Offset::MAX),
] {
let zoned = instant.to_zoned(TimeZone::fixed(offset));
assert_eq!(
zoned.to_storage().unix_timestamp_nanos(),
instant.as_nanosecond()
);
assert_eq!(zoned.to_storage().offset(), time::UtcOffset::UTC);
}
}
#[test]
fn the_two_date_ranges_are_one_span() {
assert_eq!(civil::Date::MIN.to_storage(), time::Date::MIN);
assert_eq!(civil::Date::MAX.to_storage(), time::Date::MAX);
}
#[test]
fn a_date_carries_its_extremes() {
for date in [
civil::Date::MIN,
civil::Date::MAX,
civil::date(1970, 1, 1),
civil::date(2024, 2, 29),
civil::date(-4000, 2, 29),
] {
let stored = date.to_storage();
assert_eq!(i32::from(date.year()), stored.year());
assert_eq!(
u8::try_from(date.month()).unwrap(),
u8::from(stored.month())
);
assert_eq!(u8::try_from(date.day()).unwrap(), stored.day());
}
}
#[test]
fn the_two_time_ranges_are_one_span() {
assert_eq!(civil::Time::MIN.to_storage(), time::Time::MIDNIGHT);
assert_eq!(civil::Time::MAX.to_storage(), time::Time::MAX);
}
#[test]
fn a_time_carries_its_subsecond_nanoseconds() {
let noon = civil::Time::new(12, 34, 56, 123_456_789).unwrap();
let stored = noon.to_storage();
assert_eq!(stored.nanosecond(), 123_456_789);
assert_eq!(civil::Time::MAX.to_storage().nanosecond(), 999_999_999);
}
#[test]
fn a_time_carries_its_extremes() {
for at in [
civil::Time::MIN,
civil::Time::MAX,
civil::Time::new(12, 34, 56, 0).unwrap(),
] {
let stored = at.to_storage();
assert_eq!(u8::try_from(at.hour()).unwrap(), stored.hour());
assert_eq!(u8::try_from(at.minute()).unwrap(), stored.minute());
assert_eq!(u8::try_from(at.second()).unwrap(), stored.second());
assert_eq!(
u32::try_from(at.subsec_nanosecond()).unwrap(),
stored.nanosecond()
);
}
}
#[test]
fn the_crossing_returns_the_value_and_not_a_result() {
fn total<T: TemporalStorage>(value: &T) -> T::Storage {
value.to_storage()
}
let _: time::OffsetDateTime = total(&Timestamp::UNIX_EPOCH);
let _: time::OffsetDateTime = total(&Zoned::new(Timestamp::UNIX_EPOCH, TimeZone::UTC));
let _: time::Date = total(&civil::date(2024, 5, 6));
let _: time::Time = total(&civil::Time::new(12, 0, 0, 0).unwrap());
}
#[test]
fn a_wall_clock_crosses_as_its_two_halves() {
for local in [
civil::DateTime::MIN,
civil::DateTime::MAX,
civil::date(1970, 1, 1).at(0, 0, 0, 0),
civil::date(2021, 8, 14).at(16, 45, 9, 123_456_789),
civil::date(-4000, 2, 29).at(23, 59, 59, 999_999_999),
] {
let stored = local.to_storage();
assert_eq!(stored.date(), local.date().to_storage());
assert_eq!(stored.time(), local.time().to_storage());
assert_eq!(stored.nanosecond(), local.subsec_nanosecond() as u32);
}
}
#[test]
fn the_two_wall_clock_ranges_are_one_span() {
assert_eq!(
civil::DateTime::MIN.to_storage(),
time::PrimitiveDateTime::new(time::Date::MIN, time::Time::MIDNIGHT)
);
assert_eq!(
civil::DateTime::MAX.to_storage(),
time::PrimitiveDateTime::new(time::Date::MAX, time::Time::MAX)
);
}
#[test]
fn an_offset_crosses_as_its_seconds() {
for (offset, seconds) in [
(tz::Offset::MIN, -93_599),
(tz::Offset::MAX, 93_599),
(tz::Offset::UTC, 0),
(tz::Offset::constant(9), 32_400),
(tz::Offset::from_seconds(-16_200).unwrap(), -16_200),
] {
assert_eq!(offset.to_storage(), seconds);
}
}
#[test]
fn the_offset_domain_sits_far_inside_an_i32() {
assert_eq!(tz::Offset::MIN.seconds(), -93_599);
assert_eq!(tz::Offset::MAX.seconds(), 93_599);
assert!(i64::from(tz::Offset::MAX.seconds()) * 10_000 < i64::from(i32::MAX));
}
#[test]
fn the_two_new_crossings_return_the_value_too() {
fn total<T: TemporalStorage>(value: &T) -> T::Storage {
value.to_storage()
}
let _: time::PrimitiveDateTime = total(&civil::date(2021, 8, 14).at(16, 45, 9, 0));
let _: i32 = total(&tz::Offset::constant(9));
}