Lines
100 %
Functions
Branches
use super::*;
const HOUR: i64 = 3_600_000;
/// New York, 2025: DST began 2025-03-09 07:00Z and ended 2025-11-02 06:00Z.
fn new_york(utc_ms: i64) -> i32 {
let start = parse_local("2025-03-09T07:00").unwrap();
let end = parse_local("2025-11-02T06:00").unwrap();
if (start..end).contains(&utc_ms) {
-240
} else {
-300
}
fn ny(text: &str) -> String {
format_utc(local_to_utc(parse_local(text).unwrap(), new_york))
#[test]
fn parses_and_formats_round_trip() {
let ms = parse_local("2025-11-02T01:30:15").unwrap();
assert_eq!(format_utc(ms), "2025-11-02T01:30:15.000Z");
assert_eq!(format_local(ms, 0), "2025-11-02T01:30");
assert_eq!(format_local(ms, -300), "2025-11-01T20:30");
assert_eq!(format_local(ms, 570), "2025-11-02T11:00");
assert_eq!(format_utc(0), "1970-01-01T00:00:00.000Z");
assert_eq!(parse_local("1969-12-31T23:59"), Some(-60_000));
fn rejects_malformed_text() {
for bad in [
"",
"2025-11-02",
"2025-13-02T01:00",
"2025-02-30T01:00",
"2025-11-02T24:00",
"2025-11-02T01:60",
"2025-11-02T01",
"2025-1-02T01:00",
"2025-11-02T01:00:99",
"2025-11-02T01:00:5",
"x025-11-02T01:00",
] {
assert_eq!(parse_local(bad), None, "{bad}");
assert!(parse_local("2024-02-29T00:00").is_some());
fn an_ordinary_time_uses_the_zone_offset() {
assert_eq!(ny("2025-07-01T12:00"), "2025-07-01T16:00:00.000Z");
assert_eq!(ny("2025-12-01T12:00"), "2025-12-01T17:00:00.000Z");
fn a_repeated_hour_resolves_to_its_first_occurrence() {
assert_eq!(ny("2025-11-02T01:30"), "2025-11-02T05:30:00.000Z");
assert_eq!(ny("2025-11-02T00:59"), "2025-11-02T04:59:00.000Z");
assert_eq!(ny("2025-11-02T02:00"), "2025-11-02T07:00:00.000Z");
fn a_missing_hour_is_read_with_the_offset_before_the_change() {
assert_eq!(ny("2025-03-09T02:30"), "2025-03-09T07:30:00.000Z");
assert_eq!(ny("2025-03-09T01:59"), "2025-03-09T06:59:00.000Z");
assert_eq!(ny("2025-03-09T03:00"), "2025-03-09T07:00:00.000Z");
fn both_instants_of_a_repeated_hour_display_the_same_text() {
let first = parse_local("2025-11-02T05:30").unwrap();
for utc in [first, first + HOUR] {
assert_eq!(format_local(utc, new_york(utc)), "2025-11-02T01:30");
fn a_zone_east_of_utc_with_a_half_hour_offset() {
let india = |_| 330;
assert_eq!(
format_utc(local_to_utc(
parse_local("2025-06-01T05:30").unwrap(),
india
)),
"2025-06-01T00:00:00.000Z"
);