Lines
100 %
Functions
Branches
use chrono::{TimeZone, Utc};
use chrono_tz::Tz;
use forms::{now_instant, parse_date, parse_date_in, parse_day, parse_post_date, step_date};
#[test]
fn rfc3339_keeps_full_precision() {
let dt = parse_date("2026-10-27T09:30:15.123456+02:00", false).expect("rfc3339");
assert_eq!(dt.to_rfc3339(), "2026-10-27T07:30:15.123456+00:00");
}
fn org_read_date_forms_are_midnight_free() {
let expected = Utc.with_ymd_and_hms(2026, 10, 27, 9, 30, 0).unwrap();
assert_eq!(parse_date("2026-10-27T09:30", false), Ok(expected));
assert_eq!(parse_date("2026-10-27 09:30", true), Ok(expected));
fn date_only_is_midnight_or_end_of_day() {
let start = parse_date("2026-10-27", false).expect("date");
let end = parse_date("2026-10-27", true).expect("date");
assert_eq!(start.to_rfc3339(), "2026-10-27T00:00:00+00:00");
assert_eq!(end.to_rfc3339(), "2026-10-27T23:59:59.999999+00:00");
fn garbage_is_rejected() {
assert!(parse_date("not-a-date", false).is_err());
assert!(parse_date("2026-13-01", false).is_err());
assert!(parse_date("", false).is_err());
fn now_instant_is_a_full_utc_instant() {
let now = now_instant();
assert!(chrono::DateTime::parse_from_rfc3339(&now).is_ok(), "{now}");
assert!(now.ends_with('Z'));
assert!(parse_date(&now, false).is_ok());
fn step_preserves_minute_forms() {
assert_eq!(
step_date("2024-01-31T10:30", 1).as_deref(),
Some("2024-02-01T10:30")
);
step_date("2024-03-01 08:15", -1).as_deref(),
Some("2024-02-29T08:15")
assert_eq!(step_date("2024-01-31", 1).as_deref(), Some("2024-02-01"));
assert_eq!(step_date("2024-12-31", 1).as_deref(), Some("2025-01-01"));
fn step_keeps_the_seconds_of_an_offset_free_value() {
step_date("2026-10-27T09:30:15", 1).as_deref(),
Some("2026-10-28T09:30:15")
step_date(" 2026-10-27T09:30:15 ", -1).as_deref(),
Some("2026-10-26T09:30:15")
fn step_keeps_seconds_fractions_and_offset_of_rfc3339() {
step_date("2026-10-27T09:30:15.123456+00:00", 1).as_deref(),
Some("2026-10-28T09:30:15.123456Z")
step_date("2026-10-27T09:30:15+02:00", -1).as_deref(),
Some("2026-10-26T09:30:15+02:00")
fn step_of_garbage_is_none() {
assert_eq!(step_date("not-a-date", 1), None);
assert_eq!(step_date("", -1), None);
assert_eq!(step_date("2026-10-27", i64::MAX), None);
fn range_end_keeps_a_sub_second_instant_of_that_day_inside() {
let late = parse_date("2026-10-27T23:59:59.5Z", false).expect("rfc3339");
let next = parse_date("2026-10-28", false).expect("date");
assert!(late <= end);
assert!(end < next);
fn seconds_without_offset_are_utc() {
let expected = Utc.with_ymd_and_hms(2026, 10, 27, 9, 30, 15).unwrap();
assert_eq!(parse_date("2026-10-27T09:30:15", false), Ok(expected));
fn utc(y: i32, mo: u32, d: u32, h: u32, mi: u32, s: u32) -> chrono::DateTime<Utc> {
Utc.with_ymd_and_hms(y, mo, d, h, mi, s).unwrap()
fn a_date_only_range_is_the_local_day_in_tokyo() {
let from = parse_date_in("2026-12-01", false, &Tz::Asia__Tokyo).expect("from");
let to = parse_date_in("2026-12-01", true, &Tz::Asia__Tokyo).expect("to");
assert_eq!(from, utc(2026, 11, 30, 15, 0, 0));
assert_eq!(to.to_rfc3339(), "2026-12-01T14:59:59.999999+00:00");
fn a_date_only_range_is_the_local_day_in_new_york() {
let from = parse_date_in("2026-12-01", false, &Tz::America__New_York).expect("from");
let to = parse_date_in("2026-12-01", true, &Tz::America__New_York).expect("to");
assert_eq!(from, utc(2026, 12, 1, 5, 0, 0));
assert_eq!(to.to_rfc3339(), "2026-12-02T04:59:59.999999+00:00");
fn a_zone_does_not_move_an_explicit_instant_or_an_offset_free_time() {
let zone = Tz::Asia__Tokyo;
let explicit = parse_date_in("2026-12-01T00:00:00+02:00", false, &zone).expect("rfc3339");
assert_eq!(explicit, utc(2026, 11, 30, 22, 0, 0));
let bare = parse_date_in("2026-12-01T09:30", false, &zone).expect("minutes");
assert_eq!(bare, utc(2026, 12, 1, 9, 30, 0));
fn a_date_only_post_date_is_noon_utc_and_other_forms_are_unchanged() {
parse_post_date("2026-12-01"),
Ok(utc(2026, 12, 1, 12, 0, 0))
parse_post_date(" 2026-12-01 "),
parse_post_date("2026-12-01T00:00"),
Ok(utc(2026, 12, 1, 0, 0, 0))
parse_post_date("2026-12-01T08:00:00+02:00"),
Ok(utc(2026, 12, 1, 6, 0, 0))
assert!(parse_post_date("nope").is_err());
fn day(date: &str, range_end: bool, zone: Tz) -> chrono::DateTime<Utc> {
parse_date_in(date, range_end, &zone).expect("date")
fn rfc(instant: chrono::DateTime<Utc>) -> String {
instant.to_rfc3339_opts(chrono::SecondsFormat::Micros, true)
fn a_midnight_skipped_by_a_dst_gap_starts_when_the_gap_ends() {
let from = day("2018-11-04", false, Tz::America__Sao_Paulo);
assert_eq!(from, utc(2018, 11, 4, 3, 0, 0));
fn a_day_ending_in_a_repeated_hour_keeps_the_whole_hour() {
let santiago = day("2026-04-04", true, Tz::America__Santiago);
assert_eq!(rfc(santiago), "2026-04-05T03:59:59.999999Z");
let beirut = day("2026-10-24", true, Tz::Asia__Beirut);
assert_eq!(rfc(beirut), "2026-10-24T21:59:59.999999Z");
let cairo_next = day("2026-10-30", false, Tz::Africa__Cairo);
let cairo = day("2026-10-29", true, Tz::Africa__Cairo);
assert_eq!(cairo + chrono::Duration::microseconds(1), cairo_next);
fn a_skipped_whole_day_resolves_to_the_next_existing_instant() {
let apia = Tz::Pacific__Apia;
let start = utc(2011, 12, 30, 10, 0, 0);
assert_eq!(day("2011-12-30", false, apia), start);
assert_eq!(day("2011-12-31", false, apia), start);
rfc(day("2011-12-29", true, apia)),
"2011-12-30T09:59:59.999999Z"
rfc(day("2011-12-30", true, apia)),
let kiritimati = Tz::Pacific__Kiritimati;
let start = utc(1994, 12, 31, 10, 0, 0);
assert_eq!(day("1994-12-31", false, kiritimati), start);
assert_eq!(day("1995-01-01", false, kiritimati), start);
fn back_to_back_days_are_contiguous_in_every_zone() {
let days = [
("2026-03-07", "2026-03-08"),
("2026-04-04", "2026-04-05"),
("2018-11-03", "2018-11-04"),
("2026-10-24", "2026-10-25"),
("2026-12-01", "2026-12-02"),
];
for zone in [
Tz::UTC,
Tz::Asia__Tokyo,
Tz::America__New_York,
Tz::America__Santiago,
Tz::America__Sao_Paulo,
Tz::Asia__Beirut,
Tz::Australia__Lord_Howe,
] {
for (first, second) in days {
let end = day(first, true, zone);
let next = day(second, false, zone);
end + chrono::Duration::microseconds(1),
next,
"{zone} {first}"
fn a_day_is_a_plain_date_and_a_time_is_refused() {
parse_day(" 2026-12-01 ").map(|d| d.to_string()),
Ok("2026-12-01".to_string())
for text in [
"2026-12-01T10:00",
"2026-12-01 10:00",
"2026-12-01T10:00:00Z",
let message = parse_day(text).expect_err(text);
assert!(message.contains("is not a day"), "{message}");