use std::collections::BTreeMap;
use std::panic::catch_unwind;
use super::clock::now_dt;
use super::construct::date_from_week;
use super::test_support::{component_map, naive_utc};
use super::*;
#[test]
fn week_date() {
let d = date_from_week(1816, 1, Weekday::Mon).unwrap();
assert_eq!(d.format("%Y-%m-%d").to_string(), "1816-01-01");
}
#[test]
fn week_year_and_weekday_accessors() {
let d = Temporal::Date(NaiveDate::from_ymd_opt(1984, 1, 1).unwrap());
assert_eq!(d.component("weekYear"), Ok(Value::Int(1983)));
assert_eq!(d.component("week"), Ok(Value::Int(52)));
assert_eq!(d.component("weekDay"), Ok(Value::Int(7)));
assert_eq!(d.component("dayOfWeek"), Ok(Value::Int(7)));
}
#[test]
fn date_null_rejects_like_its_siblings() {
assert_eq!(
construct_date(Some(&Value::Null)),
Err(Error::syntax("Invalid call signature: date()"))
);
assert_eq!(
construct_datetime(Some(&Value::Null)),
Err(Error::syntax("Invalid call signature: datetime()"))
);
}
#[test]
fn truncate_time_preserves_leap_second_encoding() {
let leap = Temporal::Time {
time: NaiveTime::from_hms_nano_opt(23, 59, 59, 1_234_567_890).unwrap(),
offset: 0,
};
let expected = |h, m, s| Temporal::Time {
time: NaiveTime::from_hms_opt(h, m, s).unwrap(),
offset: 0,
};
assert_eq!(
truncate(ClockKind::Time, "second", &leap, None).unwrap(),
expected(23, 59, 59)
);
assert_eq!(
truncate(ClockKind::Time, "minute", &leap, None).unwrap(),
expected(23, 59, 0)
);
assert_eq!(
truncate(ClockKind::Time, "hour", &leap, None).unwrap(),
expected(23, 0, 0)
);
assert_eq!(
truncate(ClockKind::Time, "day", &leap, None).unwrap(),
expected(0, 0, 0)
);
assert_eq!(
truncate(ClockKind::Time, "millisecond", &leap, None).unwrap(),
Temporal::Time {
time: NaiveTime::from_hms_nano_opt(23, 59, 59, 1_234_000_000).unwrap(),
offset: 0,
}
);
assert_eq!(
truncate(ClockKind::Time, "nanosecond", &leap, None).unwrap(),
leap
);
}
#[test]
fn duration_component_table() {
let d = construct_duration(Some(&Value::Map(
[
("years".into(), Value::Int(1)),
("months".into(), Value::Int(4)),
("days".into(), Value::Int(10)),
("hours".into(), Value::Int(1)),
("minutes".into(), Value::Int(1)),
("seconds".into(), Value::Int(1)),
("nanoseconds".into(), Value::Int(111111111)),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
.unwrap();
assert_eq!(d.component("years"), Ok(Value::Int(1)));
assert_eq!(d.component("quarters"), Ok(Value::Int(5)));
assert_eq!(d.component("months"), Ok(Value::Int(16)));
assert_eq!(d.component("weeks"), Ok(Value::Int(1)));
assert_eq!(d.component("minutes"), Ok(Value::Int(61)));
assert_eq!(d.component("seconds"), Ok(Value::Int(3661)));
assert_eq!(d.component("quartersOfYear"), Ok(Value::Int(1)));
assert_eq!(d.component("monthsOfQuarter"), Ok(Value::Int(1)));
assert_eq!(d.component("monthsOfYear"), Ok(Value::Int(4)));
assert_eq!(d.component("daysOfWeek"), Ok(Value::Int(3)));
assert_eq!(d.component("millisecondsOfSecond"), Ok(Value::Int(111)));
assert_eq!(
d.component("nanosecondsOfSecond"),
Ok(Value::Int(111111111))
);
}
#[test]
fn datetime_timezone_and_epoch_accessors() {
let d = construct_datetime(Some(&Value::Map(
[
("year".into(), Value::Int(1984)),
("month".into(), Value::Int(11)),
("day".into(), Value::Int(11)),
("hour".into(), Value::Int(12)),
("minute".into(), Value::Int(31)),
("second".into(), Value::Int(14)),
("nanosecond".into(), Value::Int(645876123)),
("timezone".into(), Value::String("Europe/Stockholm".into())),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
.unwrap();
assert_eq!(
d.component("timezone"),
Ok(Value::String("Europe/Stockholm".into()))
);
assert_eq!(d.component("offset"), Ok(Value::String("+01:00".into())));
assert_eq!(d.component("epochSeconds"), Ok(Value::Int(469020674)));
assert_eq!(d.component("epochMillis"), Ok(Value::Int(469020674645)));
}
#[test]
fn zone_offset_at_resolves_unique_wall_time() {
let local = NaiveDate::from_ymd_opt(1970, 1, 1)
.unwrap()
.and_hms_opt(2, 15, 0)
.unwrap();
assert_eq!(zone_offset_at("Europe/Paris", local), Ok(3600));
}
#[test]
fn zone_offset_at_unknown_zone_is_typed_error() {
let local = NaiveDate::from_ymd_opt(2021, 3, 28)
.unwrap()
.and_hms_opt(2, 30, 0)
.unwrap();
assert_eq!(
zone_offset_at("Not/AZone", local),
Err(ZoneError::UnknownZone)
);
}
#[test]
fn zone_offset_at_dst_gap_is_typed_error() {
let local = NaiveDate::from_ymd_opt(2021, 3, 28)
.unwrap()
.and_hms_opt(2, 30, 0)
.unwrap();
assert_eq!(
zone_offset_at("Europe/Stockholm", local),
Err(ZoneError::NonexistentWallTime)
);
}
#[test]
fn zone_offset_at_dst_overlap_takes_earliest_offset() {
let local = NaiveDate::from_ymd_opt(2021, 10, 31)
.unwrap()
.and_hms_opt(2, 30, 0)
.unwrap();
assert_eq!(zone_offset_at("Europe/Stockholm", local), Ok(7200));
}
#[test]
fn construct_datetime_iana_gap_shifts_and_unknown_zone_errors() {
let fields = |timezone: &str| {
Value::Map(
[
("year".into(), Value::Int(2021)),
("month".into(), Value::Int(3)),
("day".into(), Value::Int(28)),
("hour".into(), Value::Int(2)),
("minute".into(), Value::Int(30)),
("timezone".into(), Value::String(timezone.into())),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)
};
assert_eq!(
construct_datetime(Some(&fields("Europe/Stockholm")))
.unwrap()
.iso_string(),
"2021-03-28T03:30+02:00[Europe/Stockholm]"
);
assert_eq!(
construct_datetime(Some(&fields("Not/AZone"))),
Err(Error::ty("unknown timezone 'Not/AZone'"))
);
}
#[test]
fn datetime_from_a_datetime_source_keeps_its_offset() {
let x = parse_iso("datetime", "2025-10-26T02:30:00+01:00[Europe/Stockholm]").unwrap();
let source = || Value::Temporal(x.clone());
assert_eq!(
construct_datetime(Some(&component_map(&[("datetime", source())])))
.unwrap()
.iso_string(),
"2025-10-26T02:30+01:00[Europe/Stockholm]"
);
assert_eq!(
construct_datetime(Some(&component_map(&[
("datetime", source()),
("timezone", Value::String("UTC".into())),
])))
.unwrap()
.iso_string(),
"2025-10-26T01:30Z"
);
assert_eq!(
construct_datetime(Some(&component_map(&[
("datetime", source()),
("year", Value::Int(2024)),
])))
.unwrap()
.iso_string(),
"2024-10-26T02:30+02:00[Europe/Stockholm]"
);
}
#[test]
fn datetime_map_timezone_must_be_a_string() {
let fields = |timezone: Option<Value>| {
let mut m: BTreeMap<String, Value> = [
("year".into(), Value::Int(1984)),
("month".into(), Value::Int(11)),
("day".into(), Value::Int(11)),
("hour".into(), Value::Int(12)),
]
.into_iter()
.collect();
if let Some(timezone) = timezone {
m.insert("timezone".into(), timezone);
}
Value::Map(m.into())
};
assert_eq!(
construct_datetime(Some(&fields(Some(Value::Int(42))))),
Err(Error::argument_error("timezone must be STRING"))
);
let d = construct_datetime(Some(&fields(None))).unwrap();
assert_eq!(d.component("timezone"), Ok(Value::String("Z".into())));
assert_eq!(d.component("offset"), Ok(Value::String("Z".into())));
}
#[test]
fn time_truncate_resolves_timezone_at_the_pinned_utc_instant() {
let _clock = QueryClockGuard::pinned_at(naive_utc(2021, 10, 31, 1, 30, 0), 0);
let extra = Value::Map(
[("timezone".into(), Value::String("Europe/Stockholm".into()))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
);
let value = Temporal::Time {
time: NaiveTime::from_hms_opt(12, 31, 14).unwrap(),
offset: 0,
};
let out = truncate(ClockKind::Time, "hour", &value, Some(&extra)).unwrap();
assert_eq!(out.iso_string(), "12:00+01:00");
}
#[test]
fn datetime_tostring_roundtrip_equals() {
let d = construct_datetime(Some(&Value::Map(
[
("year".into(), Value::Int(1984)),
("month".into(), Value::Int(10)),
("day".into(), Value::Int(11)),
("hour".into(), Value::Int(12)),
("minute".into(), Value::Int(31)),
("second".into(), Value::Int(14)),
("nanosecond".into(), Value::Int(645876123)),
("timezone".into(), Value::String("+01:00".into())),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
.unwrap();
assert_eq!(d.iso_string(), "1984-10-11T12:31:14.645876123+01:00");
let parsed = parse_iso("datetime", &d.iso_string()).unwrap();
assert_eq!(d.compare(&parsed), Some(std::cmp::Ordering::Equal));
assert_eq!(
format_pattern(&d, "yyyy-MM-dd'T'HH:mm:ss.SSSZ").unwrap(),
"1984-10-11T12:31:14.645+0100"
);
}
#[test]
fn parse_iso_year_month_and_compact_time_offset() {
assert_eq!(
parse_iso("date", "2015-07").unwrap().iso_string(),
"2015-07-01"
);
assert_eq!(
parse_iso("time", "2140-00:00").unwrap().iso_string(),
"21:40Z"
);
assert_eq!(
parse_iso("datetime", "+10000-01-01T00:00Z")
.unwrap()
.iso_string(),
"+10000-01-01T00:00Z"
);
}
#[test]
fn full_seconds_string_always_emits_the_seconds_field() {
let cases = [
("localtime", "10:00", "10:00:00"),
("localtime", "10:00:30", "10:00:30"),
("localtime", "10:00:30.250", "10:00:30.25"),
("time", "10:00", "10:00:00Z"),
("time", "10:00+02:00", "10:00:00+02:00"),
("localdatetime", "2020-01-01T10:00", "2020-01-01T10:00:00"),
("datetime", "2020-01-01T10:00Z", "2020-01-01T10:00:00Z"),
(
"datetime",
"2020-01-01T10:00+02:00",
"2020-01-01T10:00:00+02:00",
),
];
for (kind, input, expected) in cases {
let value = parse_iso(kind, input).unwrap();
assert_eq!(
value.full_seconds_string(),
expected,
"for {kind}('{input}')"
);
}
assert_eq!(
parse_iso("localtime", "10:00").unwrap().iso_string(),
"10:00"
);
assert_eq!(
parse_iso("localdatetime", "2020-01-01T10:00")
.unwrap()
.iso_string(),
"2020-01-01T10:00"
);
}
#[test]
fn truncate_week_day_of_week() {
let d = 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 extra = Value::Map(
[("dayOfWeek".into(), Value::Int(2))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
);
let out = truncate(ClockKind::Date, "week", &d, Some(&extra)).unwrap();
assert_eq!(out.iso_string(), "1984-10-09");
}
#[test]
fn parse_pattern_never_panics_on_malformed_input() {
let cases: &[(&str, &str, &str)] = &[
("datetime", "abc", "yyyy-MM-ddXXX"),
("date", "abc", "yyyy-MM-dd a"),
("datetime", "abc", "pppyy-MM-dd"),
("datetime", "abc", "yyyy, DDD 'day of year' c 'day-of-week'"),
("duration", "abc", "yyyy-MM-dd"),
("date", "2015-01-01", "JJ"),
("datetime", "2015-01", "yyyy-MM"),
("localtime", "abc def", "H G"),
("localdatetime", "abc", "yyyy-MM-dd VV"),
("date", "abc", "yyyy-MM-dd EEEE"),
("date", "2015-17-21", "yyyy-MM-dd EEEE"),
("datetime", "abc", "EEEE, MMMM d, G uuuu"),
("datetime", "abc", "yyyy-MM-dd k:mm z"),
("datetime", "abc", "yyyy-MM-dd K:mm a O"),
("datetime", "abc", "yyyy LLL d, A m s"),
("datetime", "abc", "'Nano:' n 'of day:' N yyyy-MM-dd"),
("datetime", "abc", "yyyy-MM-dd hh:mm a VV B"),
("datetime", "abc", "YYYY-'W'ww-e"),
("datetime", "abc", "QQQQ yyyy 'day': D"),
("date", "201501a", "yyyyMMdd"),
("date", "201\u{e5}071", "yyyyMMdd"),
("localtime", "12345678", "HHmmssSSS"),
("localtime", "1234x", "HHmmss"),
("localdatetime", "201501011210", "yyyyMMddHHmmss"),
("datetime", "2015010112101x", "yyyyMMddHHmmssZ"),
("time", "99xx99+01", "HHmmssZ"),
("time", "\u{e5}", "HH"),
("date", "2015W5\u{e5}5", "yyyy"),
("date", "not-a-date", "yyyy"),
];
for &(kind, value, pattern) in cases {
let outcome = catch_unwind(|| parse_pattern(kind, value, pattern))
.unwrap_or_else(|_| panic!("parse_pattern({kind:?}, {value:?}, {pattern:?}) panicked"));
assert!(
outcome.is_err(),
"parse_pattern({kind:?}, {value:?}, {pattern:?}) must fail, got {outcome:?}"
);
}
}
#[test]
fn now_epoch_millis_matches_pinned_datetime_and_stays_fixed() {
let _clock = QueryClockGuard::new();
let epoch_millis = now_dt()
.component("epochMillis")
.expect("datetime() exposes epochMillis");
let Value::Int(expected) = epoch_millis else {
panic!("epochMillis must be an INTEGER, got {epoch_millis:?}");
};
assert_eq!(now_epoch_millis(), expected);
std::thread::sleep(std::time::Duration::from_millis(10));
assert_eq!(now_epoch_millis(), expected);
}
#[test]
fn datetime_from_epoch_millis_and_epoch_seconds_map_fields() {
let by_millis = construct_datetime(Some(&Value::Map(
[("epochMillis".into(), Value::Int(469020674645))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
.unwrap();
assert_eq!(by_millis.iso_string(), "1984-11-11T11:31:14.645Z");
assert_eq!(
by_millis.component("epochMillis").unwrap(),
Value::Int(469020674645)
);
let by_seconds = construct_datetime(Some(&Value::Map(
[("epochSeconds".into(), Value::Int(469020674))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
.unwrap();
assert_eq!(by_seconds.iso_string(), "1984-11-11T11:31:14Z");
}
#[test]
fn datetime_from_epoch_millis_with_timezone() {
let dt = construct_datetime(Some(&Value::Map(
[
("epochMillis".into(), Value::Int(469020674645)),
("timezone".into(), Value::String("Europe/Stockholm".into())),
]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
)))
.unwrap();
assert_eq!(dt.component("offset"), Ok(Value::String("+01:00".into())));
assert_eq!(dt.component("epochMillis"), Ok(Value::Int(469020674645)));
}
#[test]
fn non_datetime_kinds_reject_epoch_millis() {
let ms = Value::Map(
[("epochMillis".into(), Value::Int(469020674645))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
);
for rejected in [
construct_date(Some(&ms)),
construct_localdatetime(Some(&ms)),
construct_time(Some(&ms)),
construct_localtime(Some(&ms)),
] {
assert!(rejected.is_err(), "an epoch map is datetime-only");
}
}
#[test]
fn epoch_fields_out_of_range_error() {
let ms = Value::Map(
[("epochMillis".into(), Value::Int(i64::MAX))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
);
assert!(construct_datetime(Some(&ms)).is_err());
assert!(construct_date(Some(&ms)).is_err());
}
#[test]
fn epoch_fields_must_be_integers() {
let ms = Value::Map(
[("epochMillis".into(), Value::Float(469020674.645))]
.into_iter()
.collect::<BTreeMap<_, _>>()
.into(),
);
assert!(construct_datetime(Some(&ms)).is_err());
}
#[test]
fn zone_region_classifies_iana_names_only() {
assert_eq!(
zone_region(Some("Europe/Stockholm")),
Some("Europe/Stockholm")
);
for offset_only in [None, Some("Z"), Some("+01:00"), Some("UTC")] {
assert_eq!(zone_region(offset_only), None, "{offset_only:?}");
}
}
#[test]
fn zoned_literal_offset_disambiguates_a_dst_overlap() {
let first = parse_iso("datetime", "2020-10-25T02:30+02:00[Europe/Stockholm]").unwrap();
let second = parse_iso("datetime", "2020-10-25T02:30+01:00[Europe/Stockholm]").unwrap();
assert_eq!(
first.iso_string(),
"2020-10-25T02:30+02:00[Europe/Stockholm]"
);
assert_eq!(
second.iso_string(),
"2020-10-25T02:30+01:00[Europe/Stockholm]"
);
assert_eq!(
first.full_seconds_string(),
"2020-10-25T02:30:00+02:00[Europe/Stockholm]"
);
assert!(first.equals(&second));
assert_eq!(first.compare(&second), Some(Ordering::Less));
assert_eq!(
parse_iso("datetime", "2020-10-25T02:30[Europe/Stockholm]"),
Err(Error::ty("ambiguous datetime"))
);
let gap = parse_iso("datetime", "2021-03-28T02:30[Europe/Stockholm]").unwrap();
assert_eq!(gap.iso_string(), "2021-03-28T03:30+02:00[Europe/Stockholm]");
assert_eq!(
parse_iso("datetime", "2021-03-28T02:30+01:00[Europe/Stockholm]"),
Err(Error::ty("ambiguous datetime"))
);
}
#[test]
fn zoned_equality_degrades_not_panics_on_out_of_range_offset() {
let extreme = |zone: Option<&str>| Temporal::DateTime {
utc: chrono::NaiveDateTime::MAX,
offset: 100_000,
zone: zone.map(str::to_string),
};
let payload = extreme(None);
let twin = payload.clone();
assert!(payload.equals(&twin));
assert_eq!(payload.datetime_eq_identity(), twin.datetime_eq_identity());
assert!(!payload.equals(&extreme(Some("Europe/Stockholm"))));
assert_eq!(
payload.iso_string(),
format!(
"{}+27:46:40",
chrono::NaiveDateTime::MAX.format("%Y-%m-%dT%H:%M:%S%.9f")
)
);
}
#[test]
fn temporal_equality_is_offset_and_zone_sensitive() {
use crate::temporal::ZoneId;
use chrono::NaiveTime;
let time = |h, m, offset| Temporal::Time {
time: NaiveTime::from_hms_opt(h, m, 0).expect("valid time"),
offset,
};
let a = time(12, 0, 3600);
let b = time(11, 0, 0);
assert!(!a.equals(&b));
assert_eq!(a.compare(&b), Some(Ordering::Greater));
assert!(time(12, 0, 0).equals(&time(12, 0, 0)));
let utc = chrono::NaiveDate::from_ymd_opt(2025, 10, 26)
.expect("valid date")
.and_hms_opt(0, 30, 0)
.expect("valid time");
let first = Temporal::DateTime {
utc,
offset: 7200,
zone: Some("Europe/Stockholm".into()),
};
let second = Temporal::DateTime {
utc: utc + chrono::Duration::hours(1),
offset: 3600,
zone: Some("Europe/Stockholm".into()),
};
assert!(first.equals(&second));
assert_eq!(first.compare(&second), Some(Ordering::Less));
let plain = Temporal::DateTime {
utc,
offset: 3600,
zone: None,
};
assert!(!plain.equals(&second));
let d = Temporal::Duration {
months: 0,
days: 1,
nanos: 0,
};
assert!(d.equals(&d));
assert_eq!(d.compare(&d), None);
assert_eq!(ZoneId::of(Some("Z"), 0), ZoneId::Offset(0));
}
#[test]
fn chronological_same_instant_and_same_representation() {
let dt = |s| parse_iso("datetime", s).unwrap();
let offset = dt("2020-01-01T12:00+01:00");
let zulu = dt("2020-01-01T11:00Z");
assert!(offset.same_instant(&zulu));
assert!(!offset.equals(&zulu));
let named = dt("2020-01-01T12:00[Europe/Stockholm]");
assert!(offset.same_instant(&named));
assert!(!offset.equals(&named));
let early = dt("2020-10-25T02:30+02:00[Europe/Stockholm]");
let late = dt("2020-10-25T02:30+01:00[Europe/Stockholm]");
assert!(!early.same_instant(&late));
assert!(early.equals(&late));
let z = dt("2020-01-01T12:00Z");
let plus = dt("2020-01-01T12:00+00:00");
assert!(z.same_instant(&plus));
assert!(z.equals(&plus));
let d = parse_iso("date", "2020-01-01").unwrap();
assert!(!d.same_instant(&zulu));
assert!(!d.equals(&zulu));
}
#[test]
fn chronological_instant_ordering_for_zoned_kinds() {
let dt = |s| parse_iso("datetime", s).unwrap();
let offset = dt("2020-01-01T12:00+01:00");
let zulu = dt("2020-01-01T11:00Z");
assert_eq!(
offset.instant_ordering(&zulu),
Some(std::cmp::Ordering::Equal)
);
let t = |s| parse_iso("time", s).unwrap();
let past_midnight = t("23:30-02:00"); let early_utc = t("01:30Z");
assert_eq!(
past_midnight.instant_ordering(&early_utc),
Some(std::cmp::Ordering::Greater)
);
assert!(!past_midnight.same_instant(&early_utc));
let before_midnight = t("00:30+02:00"); assert_eq!(
before_midnight.instant_ordering(&early_utc),
Some(std::cmp::Ordering::Less)
);
assert!(!before_midnight.same_instant(&t("22:30Z")));
assert_eq!(
t("12:00+02:00").instant_ordering(&t("12:00+01:00")),
Some(std::cmp::Ordering::Less)
);
}
#[test]
fn padded_iso_date_value_mirrors_the_general_construction() {
let cases = [
"1990-01-01",
"1985-06-15",
"0000-01-01",
"9999-12-31",
"2000-02-29",
"1990-02-30",
"2000-13-01",
"1990-00-10",
"19900101",
"1990-01",
"1990",
"1816-W01-1",
"2016-366",
"+1990-01-01",
" 1990-01-01",
"1990-01-01 ",
"990-01-01",
"19900-01-01",
"1990-1-01",
"1990-01-1",
"not a date",
"",
];
for case in cases {
let fast = padded_iso_date_value(case);
let general = construct_named("date", &[Value::String(case.into())]);
let general_ok = general.is_ok();
match (&fast, general) {
(Some(value), Ok(general_value)) => assert_eq!(value, &general_value, "{case:?}"),
(Some(_), Err(err)) => panic!("fast value for {case:?} but general errors: {err}"),
(None, _) => {}
}
let is_form = case.len() == 10 && case.as_bytes()[4] == b'-' && case.as_bytes()[7] == b'-';
assert_eq!(
fast.is_some(),
is_form && general_ok,
"form classification {case:?}"
);
}
}
#[test]
fn accepts_pattern_covers_the_constructors_and_their_aliases() {
let constructors: &[(&str, &str)] = &[
("date", "2024-06-27"),
("time", "14:30:45Z"),
("localtime", "14:30:45"),
("datetime", "2024-06-27T14:30:45Z"),
("localdatetime", "2024-06-27T14:30:45"),
("duration", "P1D"),
("local_time", "14:30:45"),
("local_datetime", "2024-06-27T14:30:45"),
("local_date_time", "2024-06-27T14:30:45"),
("zoned_time", "14:30:45+02:00"),
("zoned_datetime", "2024-06-27T14:30:45+02:00"),
("zoned_date_time", "2024-06-27T14:30:45+02:00"),
];
for &(name, value) in constructors {
for spelling in [name.to_string(), name.to_ascii_uppercase()] {
assert!(accepts_pattern(&spelling), "{spelling}");
assert!(
construct_named(&spelling, &[Value::String(value.into())]).is_ok(),
"{spelling} must construct through the same vocabulary"
);
}
}
for name in [
"vector",
"coalesce",
"datetime_from_epoch",
"date.truncate",
"",
] {
assert!(!accepts_pattern(name), "{name}");
assert!(
construct_named(name, &[Value::String("P1D".into())]).is_err(),
"{name} is not a constructor"
);
}
}
#[test]
fn construct_named_rejects_more_than_two_arguments() {
let value = Value::String("2024-06-27".into());
let pattern = Value::String("yyyy-MM-dd".into());
let extra = Value::String("extra".into());
for name in ["date", "LOCAL_TIME", "zoned_datetime", "duration"] {
let err = construct_named(name, &[value.clone(), pattern.clone(), extra.clone()])
.expect_err("three arguments must be rejected");
assert!(
err.to_string().contains("InvalidNumberOfArguments"),
"{name}: {err}"
);
}
assert!(construct_named("date", &[]).is_ok(), "the clock form");
assert!(construct_named("date", &[value.clone()]).is_ok());
assert!(construct_named("date", &[value, pattern]).is_ok());
}
#[test]
fn temporal_map_non_integer_components_are_typed_errors() {
let string = || Value::String("x".into());
let list = || Value::List(vec![Value::Int(1), Value::Int(2)].into());
assert!(construct_date(Some(&component_map(&[
("year", Value::Int(2000)),
("month", string()),
])))
.is_err());
assert!(construct_date(Some(&component_map(&[
("year", Value::Int(2000)),
("week", string()),
])))
.is_err());
assert!(construct_date(Some(&component_map(&[("year", Value::Float(2000.0))]))).is_err());
assert!(construct_localtime(Some(&component_map(&[
("hour", Value::Int(12)),
("minute", list()),
])))
.is_err());
assert!(construct_datetime(Some(&component_map(&[
("year", Value::Int(2000)),
("month", Value::Int(1)),
("day", Value::Int(1)),
("hour", Value::Bool(true)),
])))
.is_err());
assert!(construct_localdatetime(Some(&component_map(&[
("year", Value::Int(2000)),
("month", Value::Int(1)),
("day", Value::Int(1)),
("nanosecond", Value::Bool(true)),
])))
.is_err());
}
#[test]
fn temporal_map_subsecond_fields_are_range_checked() {
let time = |entries: &[(&str, Value)]| {
let mut all = vec![
("hour", Value::Int(1)),
("minute", Value::Int(1)),
("second", Value::Int(1)),
];
all.extend_from_slice(entries);
construct_time(Some(&component_map(&all)))
};
for entries in [
vec![("millisecond", Value::Int(-1))],
vec![("millisecond", Value::Int(1000))],
vec![
("millisecond", Value::Int(0)),
("microsecond", Value::Int(1000)),
],
vec![
("millisecond", Value::Int(0)),
("nanosecond", Value::Int(1_000_000)),
],
vec![
("microsecond", Value::Int(0)),
("nanosecond", Value::Int(1000)),
],
vec![("microsecond", Value::Int(1_000_000))],
vec![("nanosecond", Value::Int(1_000_000_000))],
] {
assert!(time(&entries).is_err(), "{entries:?}");
}
assert!(time(&[
("millisecond", Value::Int(999)),
("microsecond", Value::Int(999)),
("nanosecond", Value::Int(999)),
])
.is_ok());
}
#[test]
fn temporal_map_quarter_components_are_validated() {
assert!(construct_date(Some(&component_map(&[
("year", Value::Int(2020)),
("quarter", Value::Int(0)),
])))
.is_err());
assert!(construct_date(Some(&component_map(&[
("year", Value::Int(2020)),
("quarter", Value::Int(1)),
("dayOfQuarter", Value::Int(-1)),
])))
.is_err());
}
#[test]
fn temporal_map_null_day_of_quarter_does_not_shift_the_quarter() {
let date = Value::Temporal(parse_iso("date", "2020-09-15").unwrap());
let with_null = construct_date(Some(&component_map(&[
("date", date.clone()),
("quarter", Value::Int(3)),
("dayOfQuarter", Value::Null),
])))
.unwrap();
let absent = construct_date(Some(&component_map(&[
("date", date),
("quarter", Value::Int(3)),
])))
.unwrap();
assert_eq!(with_null, absent);
assert_eq!(with_null.iso_string(), "2020-09-15");
}
#[test]
fn temporal_format_fraction_width_above_nine_is_an_error() {
let date = parse_iso("datetime", "2020-09-15T12:34:56.123456789Z").unwrap();
assert!(format_pattern(&date, "SSSSSSSSSS").is_err());
assert!(format_pattern(&date, "SSSSSSSSS").is_ok());
let duration = Temporal::Duration {
months: 0,
days: 0,
nanos: 123_456_789,
};
assert!(format_pattern(&duration, "nnnnnnnnnn").is_err());
assert!(format_pattern(&duration, "nnnnnnnnn").is_ok());
}
#[test]
fn named_zone_offset_must_match_the_zone() {
for invalid in [
"1984-07-07T12:34+03:00[Europe/Stockholm]",
"2012-11-04T00:59:59-06:00[America/Chicago]",
"2021-03-28T00:59:59+01:00[Europe/London]",
"2019-10-30T07:54:02.129790999+01:00[UTC]",
] {
assert!(parse_iso("datetime", invalid).is_err(), "{invalid}");
}
assert!(parse_iso("datetime", "1984-07-07T12:34+02:00[Europe/Stockholm]").is_ok());
assert!(parse_iso("datetime", "2019-10-30T07:54:02+00:00[UTC]").is_ok());
}
#[test]
fn offsets_are_bounded_and_time_literals_reject_named_zones() {
assert!(parse_iso("datetime", "2018-04-01T12:45:45+45").is_err());
assert!(parse_iso("datetime", "2018-04-01T12:45:45+18:00").is_ok());
assert!(parse_iso("datetime", "2018-04-01T12:45:45+18:01").is_err());
assert!(parse_iso("time", "07:54:02.129790999+00:00[UTC]").is_err());
assert!(parse_iso("time", "12:34:56[Europe/Stockholm]").is_err());
}
#[test]
fn gmt_prefixed_offsets_keep_their_spelling() {
let dt = construct_datetime(Some(&component_map(&[
("year", Value::Int(2025)),
("timezone", Value::String("GMT+03:00".into())),
])))
.unwrap();
assert_eq!(
dt.component("timezone").unwrap(),
Value::String("GMT+03:00".into())
);
assert_eq!(
dt.component("offset").unwrap(),
Value::String("+03:00".into())
);
assert_eq!(dt.component("offsetMinutes").unwrap(), Value::Int(180));
assert_eq!(dt.iso_string(), "2025-01-01T00:00+03:00");
}
#[test]
fn datetime_kinds_need_a_time_bearing_source() {
let date = Value::Temporal(Temporal::Date(
NaiveDate::from_ymd_opt(1984, 2, 11).unwrap(),
));
let time = Value::Temporal(Temporal::LocalTime(
NaiveTime::from_hms_opt(12, 0, 0).unwrap(),
));
assert!(construct_datetime(Some(&date)).is_err());
assert!(construct_localdatetime(Some(&date)).is_err());
assert!(construct_datetime(Some(&time)).is_err());
assert!(construct_localdatetime(Some(&time)).is_err());
assert!(construct_date(Some(&time)).is_err());
let local = Value::Temporal(parse_iso("localdatetime", "1984-02-11T12:00").unwrap());
assert!(construct_datetime(Some(&local)).is_ok());
}