Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-12-06 13:12:06 -06:00
commit 5577896856
1624 files changed
+43436

No files matched your search

+347
View File
@@ -0,0 +1,347 @@
# Tests of time: lengths of time and their arithmetic, the calendar, moments written
# and read as text, and time zones, read from TZif files the tests carry, so that
# what they find does not depend on the database of the system they run on.
import std::io;
import system;
import std::testing;
import std::time;
# The zone of a TZif file the tests carry, found from the root of the repository.
def carried_zone(name: strview): zone {
for &folder in ["tests/time/zones"] {
val text := $"{folder}/{name}.tzif";
var at := path::parse(text.span) else (_) { continue; };
var made := zone::read(&at) else (_) { continue; };
return move(made);
}
abort();
return zone::utc();
}
# Why a file the tests carry is no zone, or `no_such_zone` where it is one, or is
# not found.
def carried_fault(name: strview): time::error {
for &folder in ["tests/time/zones"] {
val text := $"{folder}/{name}.tzif";
var at := path::parse(text.span) else (_) { continue; };
val read := zone::read(&at);
if read is error(why) && why != time::error::no_such_zone {
return why;
}
}
return time::error::no_such_zone;
}
# What a zone's clocks show at a second since 1970, as RFC 3339 writes it, and
# what the zone calls its time then.
def shown_in(place: &zone, second: i64): string {
val there := timestamp::of_unix(second).at(place);
var made := there.rfc3339();
made.append(" ");
made.append(there.name);
return made;
}
# A day of the calendar, written as it is.
def day_of(year: i32, of: u8, day: u8): date {
return date { year = year, month = of as month, day = day };
}
# A time of day, to the second.
def clock_of(hour: u8, minute: u8, second: u8): time_of_day {
return time_of_day { hour = hour, minute = minute, second = second };
}
@test def durations_are_counts_of_their_units(t: &mut tester): void {
t.expects(nanoseconds(1500).in_nanoseconds == 1500 && microseconds(2).in_nanoseconds == 2000);
t.expects(milliseconds(3).in_nanoseconds == 3000000 && seconds(4).in_nanoseconds == 4000000000);
t.expects(minutes(5) == seconds(300) && hours(2) == minutes(120) && hours(-1) == -minutes(60));
t.expects(milliseconds(1500).in_seconds == 1.5 && (-seconds(2)).in_seconds == -2.0);
# Their arithmetic is their operators: with one another, and by a count.
t.expects(seconds(2) + milliseconds(500) == milliseconds(2500) && seconds(2) - seconds(5) == -seconds(3));
t.expects(seconds(2) * 3 == seconds(6) && seconds(7) / 2 == milliseconds(3500) && seconds(-7) / 2 == milliseconds(-3500));
t.expects(seconds(3) / seconds(2) == 1.5 && minutes(1) / seconds(1) == 60.0);
t.expects(seconds(1) < seconds(2) && -seconds(1) < nanoseconds(0) && hours(1) >= minutes(60));
var held := seconds(1);
held += seconds(2);
held *= 4;
held /= 3;
held -= milliseconds(500);
t.expects(held == milliseconds(3500));
t.expects(seconds(1) is additive && seconds(1) is ordered && !(seconds(1) is numeric));
}
@test def durations_describe_themselves(t: &mut tester): void {
t.expects(nanoseconds(0).describe().span == "0s" && nanoseconds(999).describe().span == "999ns");
t.expects(nanoseconds(1500).describe().span == "1.5us" && microseconds(250).describe().span == "250us");
t.expects(milliseconds(250).describe().span == "250ms" && microseconds(1050).describe().span == "1.05ms");
t.expects(milliseconds(1500).describe().span == "1.5s" && milliseconds(2310).describe().span == "2.31s");
t.expects(nanoseconds(2319999999).describe().span == "2.31s" && seconds(59).describe().span == "59s");
# From a minute on, the two largest units that are not zero.
t.expects(seconds(60).describe().span == "1m" && seconds(90).describe().span == "1m30s");
t.expects((hours(2) + minutes(3) + seconds(4)).describe().span == "2h3m" && (hours(1) + seconds(4)).describe().span == "1h4s");
t.expects(hours(50).describe().span == "50h" && (minutes(61) + milliseconds(900)).describe().span == "1h1m");
t.expects((-milliseconds(250)).describe().span == "-250ms" && (-minutes(90)).describe().span == "-1h30m");
}
@test def time_outside_its_range_stops_a_checked_program(t: &mut tester): void {
t.expects(!stops(def() { val a := hours(2562047) + hours(0) - minutes(1); }));
t.expects(stops(def() { val a := seconds(1) / 0; }));
# What goes past the range is a contract's to stop, so it stops where they are
# checked, as a checked program checks them.
if const contracts() {
t.expects(stops(def() { val a := hours(2562047) * 2; }));
t.expects(stops(def() { val a := seconds(9223372037); }));
t.expects(stops(def() { val a := hours(2562047) + hours(2562047); }));
t.expects(stops(def() { val a := nanoseconds(-9223372036854775807) - nanoseconds(2); }));
t.expects(stops(def() { val a := -(nanoseconds(-9223372036854775807) - nanoseconds(1)); }));
t.expects(stops(def() { val a := timestamp::of_unix(9223372036) + seconds(1); }));
t.expects(stops(def() { val a := timestamp::of_unix(-9223372036) - timestamp::of_unix(9223372036); }));
}
}
@test def the_calendar_counts_days_from_1970(t: &mut tester): void {
val first := day_of(1970, 1, 1);
t.expects(first.weekday == weekday::thursday && first.day_of_year == 1 && first.days_since(first) == 0);
t.expects(day_of(2000, 2, 29).days_since(first) == 11016 && day_of(2100, 3, 1).days_since(first) == 47541);
t.expects(day_of(1677, 9, 21).days_since(first) == -106752 && day_of(2262, 4, 11).days_since(first) == 106751);
t.expects(day_of(1, 1, 1).days_since(first) == -719162);
# Days of the week, as far back and forward as moments reach, and past them.
t.expects(day_of(2000, 2, 29).weekday == weekday::tuesday && day_of(1900, 3, 1).weekday == weekday::thursday);
t.expects(day_of(2100, 3, 1).weekday == weekday::monday && day_of(1677, 9, 21).weekday == weekday::tuesday);
t.expects(day_of(2262, 4, 11).weekday == weekday::friday && day_of(1, 1, 1).weekday == weekday::monday);
t.expects(day_of(2026, 10, 31).weekday == weekday::saturday && day_of(2026, 10, 11).weekday == weekday::sunday);
# Leap years: every fourth, but for the centuries 400 does not divide.
t.expects(is_leap_year(2024) && is_leap_year(2000) && !is_leap_year(1900) && !is_leap_year(2100) && !is_leap_year(2023));
t.expects(date::of(2024, month::february, 29) is value && date::of(2000, month::february, 29) is value);
t.expects(date::of(2023, month::february, 29).error == time::error::no_such_date);
t.expects(date::of(1900, month::february, 29).error == time::error::no_such_date);
t.expects(date::of(2100, month::february, 29).error == time::error::no_such_date);
t.expects(date::of(2026, month::april, 31).error == time::error::no_such_date);
t.expects(date::of(2026, month::april, 0).error == time::error::no_such_date);
t.expects(day_of(2024, 12, 31).day_of_year == 366 && day_of(2023, 12, 31).day_of_year == 365 && day_of(2024, 3, 1).day_of_year == 61);
}
@test def dates_move_by_days_and_months(t: &mut tester): void {
t.expects(day_of(2026, 12, 31).plus_days(1) == day_of(2027, 1, 1) && day_of(2024, 2, 28).plus_days(2) == day_of(2024, 3, 1));
t.expects(day_of(1970, 1, 1).plus_days(-1) == day_of(1969, 12, 31) && day_of(1970, 1, 1).plus_days(106751) == day_of(2262, 4, 11));
t.expects(day_of(1970, 1, 1).plus_days(-106752) == day_of(1677, 9, 21) && day_of(1970, 1, 1).plus_days(-719162) == day_of(1, 1, 1));
t.expects(day_of(2023, 3, 1).plus_days(-1) == day_of(2023, 2, 28) && day_of(1900, 3, 1).plus_days(-1) == day_of(1900, 2, 28));
# The 31st becomes the last day of a month that has none.
t.expects(day_of(2026, 10, 31).plus_months(1) == day_of(2026, 11, 30) && day_of(2024, 1, 31).plus_months(1) == day_of(2024, 2, 29));
t.expects(day_of(2023, 1, 31).plus_months(1) == day_of(2023, 2, 28) && day_of(2024, 3, 31).plus_months(-1) == day_of(2024, 2, 29));
t.expects(day_of(2026, 12, 15).plus_months(1) == day_of(2027, 1, 15) && day_of(2026, 1, 15).plus_months(-1) == day_of(2025, 12, 15));
t.expects(day_of(2026, 5, 31).plus_months(12) == day_of(2027, 5, 31) && day_of(2026, 5, 31).plus_months(-14) == day_of(2025, 3, 31));
t.expects(day_of(2024, 2, 29).plus_months(12) == day_of(2025, 2, 28) && day_of(2024, 2, 29).plus_months(0) == day_of(2024, 2, 29));
t.expects(day_of(2026, 10, 7) < day_of(2026, 10, 8) && day_of(2026, 9, 30) < day_of(2026, 10, 1) && day_of(2025, 12, 31) < day_of(2026, 1, 1));
t.expects(clock_of(7, 42, 3) < clock_of(7, 42, 4) && clock_of(23, 59, 59) > clock_of(0, 0, 0) && clock_of(1, 2, 3) == clock_of(1, 2, 3));
t.expects(day_of(2026, 10, 7).iso().span == "2026-10-07" && day_of(44, 3, 15).iso().span == "0044-03-15");
t.expects(day_of(-1, 1, 1).iso().span == "-0001-01-01" && day_of(12026, 1, 1).iso().span == "12026-01-01");
}
@test def moments_are_written_as_rfc_3339(t: &mut tester): void {
val then := timestamp::of_unix(1791376923);
t.expects(then.rfc3339().span == "2026-10-07T12:42:03Z" && then.unix == 1791376923);
t.expects((then + milliseconds(451)).rfc3339().span == "2026-10-07T12:42:03.451Z");
t.expects((then + nanoseconds(1)).rfc3339().span == "2026-10-07T12:42:03.000000001Z");
t.expects((then + milliseconds(500)).unix == 1791376923 && (then - then) == nanoseconds(0));
t.expects(timestamp::of_unix(0).rfc3339().span == "1970-01-01T00:00:00Z");
# Before 1970, a moment is in the second that began before it.
t.expects(timestamp::of_unix(-1).rfc3339().span == "1969-12-31T23:59:59Z");
t.expects((timestamp::of_unix(-1) + milliseconds(500)).rfc3339().span == "1969-12-31T23:59:59.5Z");
t.expects((timestamp::of_unix(0) - nanoseconds(1)).unix == -1);
# As far as an `i64` of nanoseconds reaches, either way.
t.expects((timestamp::of_unix(-9223372036) - nanoseconds(854775808)).rfc3339().span == "1677-09-21T00:12:43.145224192Z");
t.expects((timestamp::of_unix(9223372036) + nanoseconds(854775807)).rfc3339().span == "2262-04-11T23:47:16.854775807Z");
# In a zone a fixed way from UTC, which calls its time by its offset.
val west := zone::fixed(hours(-5));
val east := zone::fixed(minutes(330));
val odd := zone::fixed(seconds(-21036));
t.expects(then.at(&west).rfc3339().span == "2026-10-07T07:42:03-05:00" && then.at(&west).name == "-05:00");
t.expects(then.at(&east).rfc3339().span == "2026-10-07T18:12:03+05:30" && then.at(&east).name == "+05:30");
t.expects(then.at(&odd).rfc3339().span == "2026-10-07T06:51:27-05:50" && then.at(&odd).name == "-05:50:36");
t.expects(then.at(&zone::utc()).name == "UTC" && west.offset_at(then) == hours(-5));
val moved := then.at(&east);
t.expects(moved.date == day_of(2026, 10, 7) && moved.time == clock_of(18, 12, 3) && moved.offset == minutes(330));
t.expects(moved.timestamp() == then && moved == then.at(&east) && moved != then.at(&west));
val info := file_info { modified = 1500000000123456789 };
t.expects(timestamp::of_file(&info).unix == 1500000000 && timestamp::of_file(&info) - timestamp::of_unix(1500000000) == nanoseconds(123456789));
}
@test def moments_are_read_as_rfc_3339(t: &mut tester): void {
val then := timestamp::of_unix(1791376923);
for &text in ["2026-10-07T12:42:03Z", "2026-10-07T07:42:03-05:00", "2026-10-07T18:12:03+05:30",
"2026-10-07t12:42:03z", "2026-10-07 12:42:03Z", "2026-10-07T12:42:03+00:00"] {
val read := parse_rfc3339(text) else (_) { t.expects(false); continue; };
t.expects(read.timestamp() == then && read.name.length == 0);
}
# What is written is what is read back.
for &text in ["2026-10-07T07:42:03-05:00", "2026-10-07T12:42:03.451Z", "1969-12-31T23:59:59.5Z", "0044-03-15T12:00:00+00:50"] {
val read := parse_rfc3339(text) else (_) { t.expects(false); continue; };
t.expects(read.rfc3339().span == text);
}
val west := parse_rfc3339("2026-10-07T07:42:03.5-05:00").value;
t.expects(west.date == day_of(2026, 10, 7) && west.time.hour == 7 && west.time.nanosecond == 500000000 && west.offset == hours(-5));
# A fraction of any length is read to the nanosecond.
t.expects(parse_rfc3339("2026-10-07T12:42:03.123456789Z").value.time.nanosecond == 123456789);
t.expects(parse_rfc3339("2026-10-07T12:42:03.1234567891234Z").value.time.nanosecond == 123456789);
t.expects(parse_rfc3339("2026-10-07T12:42:03.000000001Z").value.timestamp() == then + nanoseconds(1));
for &text in ["", "2026-10-07", "2026-10-07T12:42", "2026-10-07T12:42:03", "2026-10-07T12:42:03.Z", "2026-10-07T12:42:03Zx",
"2026-10-07T12:42:03+0500", "2026-10-07T12:42:03+05", "26-10-07T12:42:03Z", "2026-10-7T12:42:03Z",
"2026/10/07T12:42:03Z", "2026-10-07T12-42-03Z", " 2026-10-07T12:42:03Z"] {
t.expects(parse_rfc3339(text).error == time::error::malformed);
}
for &text in ["2026-13-07T12:42:03Z", "2026-00-07T12:42:03Z", "2026-10-07T24:00:00Z", "2026-10-07T12:60:03Z",
"2026-10-07T12:42:60Z", "2026-10-07T12:42:03+24:00", "2026-10-07T12:42:03-05:60"] {
t.expects(parse_rfc3339(text).error == time::error::out_of_range);
}
t.expects(parse_rfc3339("2026-02-30T12:42:03Z").error == time::error::no_such_date);
t.expects(parse_rfc3339("2023-02-29T12:42:03Z").error == time::error::no_such_date);
t.expects(parse_date("2026-10-07").value == day_of(2026, 10, 7) && parse_date("2024-02-29").value == day_of(2024, 2, 29));
t.expects(parse_date("2026-10-7").error == time::error::malformed && parse_date("2026-10-07T").error == time::error::malformed);
t.expects(parse_date("2026-02-30").error == time::error::no_such_date && parse_date("2026-00-10").error == time::error::out_of_range);
}
@test def zones_keep_the_offsets_their_files_list(t: &mut tester): void {
# Either side of a change of the clocks, forward and back.
val chicago := carried_zone("chicago");
t.expects(shown_in(&chicago, 1772956799).span == "2026-03-08T01:59:59-06:00 CST");
t.expects(shown_in(&chicago, 1772956800).span == "2026-03-08T03:00:00-05:00 CDT");
t.expects(shown_in(&chicago, 1793516399).span == "2026-11-01T01:59:59-05:00 CDT");
t.expects(shown_in(&chicago, 1793516400).span == "2026-11-01T01:00:00-06:00 CST");
t.expects(chicago.offset_at(timestamp::of_unix(1772956799)) == hours(-6) && chicago.offset_at(timestamp::of_unix(1772956800)) == hours(-5));
# Before the first change it lists, and long ago.
t.expects(shown_in(&chicago, -2717650800).span == "1883-11-18T11:09:24-05:50 LMT");
t.expects(chicago.offset_at(timestamp::of_unix(-2717650800)) == seconds(-21036));
t.expects(shown_in(&chicago, -615452400).span == "1950-07-01T12:00:00-05:00 CDT");
val tokyo := carried_zone("tokyo");
t.expects(shown_in(&tokyo, -618105600).span == "1950-06-01T10:00:00+10:00 JDT");
t.expects(shown_in(&tokyo, 1767225600).span == "2026-01-01T09:00:00+09:00 JST");
# An offset that is no whole hour, a name that is no letters, and the south.
val kolkata := carried_zone("kolkata");
t.expects(shown_in(&kolkata, 1780252200).span == "2026-06-01T00:00:00+05:30 IST");
val paulo := carried_zone("sao_paulo");
t.expects(shown_in(&paulo, 1767268800).span == "2026-01-01T09:00:00-03:00 -03");
t.expects(shown_in(&paulo, 1543665600).span == "2018-12-01T10:00:00-02:00 -02");
val sydney := carried_zone("sydney");
t.expects(shown_in(&sydney, 1791043199).span == "2026-10-04T01:59:59+10:00 AEST");
t.expects(shown_in(&sydney, 1791043200).span == "2026-10-04T03:00:00+11:00 AEDT");
val dublin := carried_zone("dublin");
t.expects(shown_in(&dublin, 1782864000).span == "2026-07-01T01:00:00+01:00 IST");
t.expects(shown_in(&dublin, 1796083200).span == "2026-12-01T00:00:00Z GMT");
}
@test def a_zones_rule_keeps_time_after_its_last_change(t: &mut tester): void {
val chicago := carried_zone("chicago");
t.expects(shown_in(&chicago, 2209312800).span == "2040-01-04T12:00:00-06:00 CST");
t.expects(shown_in(&chicago, 2225034000).span == "2040-07-04T12:00:00-05:00 CDT");
t.expects(shown_in(&chicago, 2215065599).span == "2040-03-11T01:59:59-06:00 CST");
t.expects(shown_in(&chicago, 2215065600).span == "2040-03-11T03:00:00-05:00 CDT");
t.expects(shown_in(&chicago, 2235625199).span == "2040-11-04T01:59:59-05:00 CDT");
t.expects(shown_in(&chicago, 2235625200).span == "2040-11-04T01:00:00-06:00 CST");
# South of the equator, where the summer runs over the new year.
val sydney := carried_zone("sydney");
t.expects(shown_in(&sydney, 2210202000).span == "2040-01-15T12:00:00+11:00 AEDT");
t.expects(shown_in(&sydney, 2225930400).span == "2040-07-15T12:00:00+10:00 AEST");
t.expects(shown_in(&sydney, 4102403400).span == "2099-12-31T23:30:00+11:00 AEDT");
t.expects(shown_in(&sydney, 4102407000).span == "2100-01-01T00:30:00+11:00 AEDT");
# A rule whose other time is the winter's, and one with none.
val dublin := carried_zone("dublin");
t.expects(shown_in(&dublin, 2210241600).span == "2040-01-15T12:00:00Z GMT");
t.expects(shown_in(&dublin, 2225962800).span == "2040-07-15T12:00:00+01:00 IST");
val tokyo := carried_zone("tokyo");
t.expects(shown_in(&tokyo, 4107542400).span == "2100-03-01T09:00:00+09:00 JST");
val paulo := carried_zone("sao_paulo");
t.expects(shown_in(&paulo, 4107542400).span == "2100-02-28T21:00:00-03:00 -03");
}
@test def a_zone_resolves_what_its_clocks_show(t: &mut tester): void {
val chicago := carried_zone("chicago");
t.expects(chicago.resolve(day_of(2026, 7, 4), clock_of(12, 0, 0)) == timestamp::of_unix(1783184400));
# A time the clocks skip is that long after the change, and one they show
# twice is its first.
t.expects(chicago.resolve(day_of(2026, 3, 8), clock_of(2, 30, 0)) == timestamp::of_unix(1772958600));
t.expects(chicago.resolve(day_of(2026, 11, 1), clock_of(1, 30, 0)) == timestamp::of_unix(1793514600));
t.expects(shown_in(&chicago, 1772958600).span == "2026-03-08T03:30:00-05:00 CDT");
# By the rule, after the last change the file lists.
val sydney := carried_zone("sydney");
t.expects(sydney.resolve(day_of(2040, 10, 7), clock_of(2, 30, 0)) == timestamp::of_unix(2233153800));
t.expects(sydney.resolve(day_of(2040, 4, 1), clock_of(2, 30, 0)) == timestamp::of_unix(2216820600));
val dublin := carried_zone("dublin");
t.expects(dublin.resolve(day_of(2040, 3, 25), clock_of(1, 30, 0)) == timestamp::of_unix(2216251800));
t.expects(dublin.resolve(day_of(2040, 10, 28), clock_of(1, 30, 0)) == timestamp::of_unix(2234997000));
val tokyo := carried_zone("tokyo");
val nine := time_of_day { hour = 9, nanosecond = 5 };
t.expects(tokyo.resolve(day_of(2026, 6, 1), nine) == timestamp::of_unix(1780272000) + nanoseconds(5));
t.expects(zone::utc().resolve(day_of(1970, 1, 2), clock_of(0, 0, 1)) == timestamp::of_unix(86401));
# What a zone's clocks show at a moment is the moment they show it.
for &second in [0, 1772956799, 1772956800, 1793514599, 1783184400, 2225034000, -615452400, -2717650800] {
val there := timestamp::of_unix(second).at(&chicago);
t.expects(chicago.resolve(there.date, there.time) == timestamp::of_unix(second));
}
}
@test def zone_names_stay_within_the_database(t: &mut tester): void {
t.expects(zone::named("../../etc/passwd").error == time::error::no_such_zone);
t.expects(zone::named("/etc/localtime").error == time::error::no_such_zone);
t.expects(zone::named("America/../../../etc/localtime").error == time::error::no_such_zone);
t.expects(zone::named("").error == time::error::no_such_zone);
t.expects(zone::named("No/Such_Zone").error == time::error::no_such_zone);
# A file that is no zone's, or is cut short, is unreadable.
t.expects(carried_fault("not_a_zone") == time::error::unreadable_zone);
t.expects(carried_fault("cut_short") == time::error::unreadable_zone);
t.expects(carried_fault("chicago") == time::error::no_such_zone);
t.expects(zone::parse("").error == time::error::unreadable_zone && zone::parse("TZif").error == time::error::unreadable_zone);
# The system's own zone is read once, and kept.
val here: &zone = zone::local();
val offset := here.offset_at(timestamp::now());
t.expects(offset > hours(-24) && offset < hours(24) && here.offset_at(timestamp::now()) == offset);
}
@test def the_clocks_go_forward(t: &mut tester): void {
val first := instant::now();
val second := instant::now();
t.expects(second >= first && second - first >= nanoseconds(0) && first + (second - first) == second);
val began := instant::now();
time::sleep(milliseconds(20));
val took := began.elapsed();
t.expects(took >= milliseconds(20) && took < seconds(5) && instant::now() - milliseconds(20) >= began);
# No time at all is no wait.
val before := instant::now();
time::sleep(nanoseconds(0));
time::sleep(seconds(-5));
t.expects(before.elapsed() < seconds(1));
# The wall clock is past the day this was written.
t.expects(timestamp::now() > timestamp::of_unix(1791376923));
}