use std::time::{Duration, SystemTime};
use super::{Timestamp, TimestampPartsError};
const NANOS: i32 = 123_456_789;
const SECONDS: i64 = 1_786_310_043;
fn with_nanos() -> Timestamp {
Timestamp::from(
jiff::Timestamp::new(SECONDS, NANOS).expect("the fixture is inside the representable range"),
)
}
#[test]
fn the_range_is_the_documented_one() {
assert_eq!(Timestamp::MIN.to_string(), "-009999-01-02T01:59:59Z");
assert_eq!(Timestamp::MAX.to_string(), "9999-12-30T22:00:00.999999999Z");
assert_eq!(Timestamp::UNIX_EPOCH.to_string(), "1970-01-01T00:00:00Z");
assert_eq!(Timestamp::UNIX_EPOCH.get().as_nanosecond(), 0);
}
#[test]
fn orders_by_the_instant() {
let before_epoch = Timestamp::from(
jiff::Timestamp::new(-1, -500_000_000).expect("half a second before the epoch is an instant"),
);
let mut instants = [
Timestamp::MAX,
with_nanos(),
Timestamp::UNIX_EPOCH,
before_epoch,
Timestamp::MIN,
];
instants.sort();
assert_eq!(
instants,
[
Timestamp::MIN,
before_epoch,
Timestamp::UNIX_EPOCH,
with_nanos(),
Timestamp::MAX
]
);
}
#[test]
fn the_pairs_lexicographic_order_is_the_instants() {
let mut instants = [
Timestamp::MAX,
with_nanos(),
Timestamp::from(jiff::Timestamp::new(0, 1).expect("a nanosecond after the epoch")),
Timestamp::UNIX_EPOCH,
Timestamp::from(jiff::Timestamp::new(0, -1).expect("a nanosecond before the epoch")),
Timestamp::from(jiff::Timestamp::new(-1, -500_000_000).expect("half a second before it")),
Timestamp::from(jiff::Timestamp::new(-1, 0).expect("a second before it")),
Timestamp::MIN,
];
let mut pairs = instants;
instants.sort();
pairs.sort_by_key(|instant| (instant.as_second(), instant.subsec_nanosecond()));
assert_eq!(
instants, pairs,
"sorting the (secs, nanos) pairs left to right has to be sorting the instants"
);
}
#[test]
fn an_offset_spelling_normalises_to_the_instant() {
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
let east: Timestamp = "2026-08-09T21:14:03+09:00"
.parse()
.expect("an offset spelling parses");
let utc: Timestamp = "2026-08-09T12:14:03Z"
.parse()
.expect("the UTC spelling parses");
assert_eq!(east, utc);
assert_eq!(east.to_string(), "2026-08-09T12:14:03Z");
let hash = |instant: Timestamp| {
let mut hasher = DefaultHasher::new();
instant.hash(&mut hasher);
hasher.finish()
};
assert_eq!(hash(east), hash(utc));
}
#[test]
fn a_string_without_an_offset_refuses() {
assert!("2026-08-09T21:14:03".parse::<Timestamp>().is_err());
assert!("2026-08-09".parse::<Timestamp>().is_err());
assert!("not a time".parse::<Timestamp>().is_err());
assert!("1786310043".parse::<Timestamp>().is_err());
}
#[test]
fn the_text_round_trip_keeps_the_nanoseconds() {
let instant = with_nanos();
assert_eq!(instant.to_string(), "2026-08-09T21:14:03.123456789Z");
assert_eq!(
instant
.to_string()
.parse::<Timestamp>()
.expect("its own rendering parses"),
instant
);
assert_eq!(instant.get().subsec_nanosecond(), NANOS);
}
#[test]
fn a_system_time_crosses_with_its_nanoseconds() {
let at = SystemTime::UNIX_EPOCH + Duration::new(SECONDS as u64, NANOS as u32);
let instant = Timestamp::try_from(at).expect("a recent instant is inside the range");
assert_eq!(instant.get().as_second(), SECONDS);
assert_eq!(instant.get().subsec_nanosecond(), NANOS);
assert_eq!(SystemTime::from(instant), at);
}
#[test]
fn a_system_time_outside_the_range_refuses() {
let far = SystemTime::UNIX_EPOCH + Duration::from_secs(10_000_000_000_000);
assert!(Timestamp::try_from(far).is_err());
}
#[test]
fn the_jiff_crossing_is_the_identity() {
let instant = with_nanos();
assert_eq!(Timestamp::from(instant.get()), instant);
assert_eq!(jiff::Timestamp::from(instant), instant.get());
}
#[test]
fn every_instant_is_the_pair_its_own_accessors_answer() {
let before_epoch = Timestamp::from(
jiff::Timestamp::new(-1, -500_000_000).expect("half a second before the epoch is an instant"),
);
for instant in [
Timestamp::MIN,
before_epoch,
Timestamp::UNIX_EPOCH,
with_nanos(),
Timestamp::MAX,
] {
assert_eq!(
Timestamp::from_parts(instant.as_second(), instant.subsec_nanosecond())
.expect("the pair this instant writes is a pair it reads"),
instant,
"{instant}"
);
}
assert_eq!(before_epoch.to_string(), "1969-12-31T23:59:58.5Z");
assert_eq!(before_epoch.as_second(), -1);
assert_eq!(before_epoch.subsec_nanosecond(), -500_000_000);
assert_eq!(with_nanos().as_second(), SECONDS);
assert_eq!(with_nanos().subsec_nanosecond(), NANOS);
}
#[test]
fn a_pair_that_would_have_been_normalised_is_refused() {
let refused =
Timestamp::from_parts(2, -999_999_999).expect_err("a sign-crossing pair is not a spelling");
assert!(
matches!(
refused,
TimestampPartsError::Unnormalised {
second: 2,
nanosecond: -999_999_999
}
),
"{refused:?}"
);
assert!(refused.to_string().contains("(2, -999999999)"), "{refused}");
let resolved = Timestamp::from_parts(1, 1).expect("the resolved pair is the written one");
assert_eq!(resolved.to_string(), "1970-01-01T00:00:01.000000001Z");
assert_eq!(
jiff::Timestamp::new(2, -999_999_999).expect("jiff resolves it"),
resolved.get(),
"which is what the constructor beneath would have done in silence"
);
assert!(matches!(
Timestamp::from_parts(-2, 999_999_999),
Err(TimestampPartsError::Unnormalised { .. })
));
}
#[test]
fn a_pair_outside_the_types_range_carries_jiffs_refusal() {
for (second, nanosecond) in [
(0, 1_000_000_000),
(0, -1_000_000_000),
(i64::MAX, 0),
(i64::MIN, 0),
] {
let refused = Timestamp::from_parts(second, nanosecond)
.expect_err("neither a fraction past a second nor an instant past the span");
assert!(
matches!(refused, TimestampPartsError::Range(_)),
"({second}, {nanosecond}) → {refused:?}"
);
}
let refused = Timestamp::from_parts(0, 1_000_000_000).expect_err("a fraction past a second");
assert!(
std::error::Error::source(&refused).is_some(),
"jiff's own error is the source"
);
assert!(
refused
.to_string()
.starts_with("the stored pair is not an instant: "),
"{refused}"
);
}
#[test]
fn the_two_refusals_cover_different_pairs() {
assert!(Timestamp::from_parts(Timestamp::MAX.as_second(), 999_999_999).is_ok());
assert!(matches!(
Timestamp::from_parts(Timestamp::MAX.as_second() + 1, 0),
Err(TimestampPartsError::Range(_))
));
assert!(matches!(
Timestamp::from_parts(1, -1),
Err(TimestampPartsError::Unnormalised { .. })
));
}