ref: 0ea0bb526b6da68de61d2adca11e2dcea0be7670
parent: a36bb18853d22d68fbead016756a23bb6be87431
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Jun 24 22:52:16 EDT 2020
guarantee that we round-trip times in tmnorm.
--- a/libc/date.c
+++ b/libc/date.c
@@ -37,13 +37,13 @@
}
void
-testtm(int i, int year, int mon, int mday, int hour, int min, int sec, Tm *tm){
- if(tm->year != year-1900) fail("%d wrong year expected=%d actual=%d\n", i, year, tm->year);
- if(tm->mon != mon) fail("%d wrong month expected=%d actual=%d\n", i, mon, tm->mon);
- if(tm->mday != mday) fail("%d wrong mday expected=%d actual=%d\n", i, mday, tm->mday);
- if(tm->hour != hour) fail("%d wrong hour expected=%d actual=%d\n", i, hour, tm->hour);
- if(tm->min != min) fail("%d wrong min expected=%d actual=%d\n", i, min, tm->min);
- if(tm->sec != sec) fail("%d wrong sec expected=%d actual=%d\n", i, sec, tm->sec);
+testtm(char *s, int year, int mon, int mday, int hour, int min, int sec, Tm *tm){
+ if(tm->year != year-1900) fail("%s wrong year expected=%d actual=%d\n", s, year, tm->year);
+ if(tm->mon != mon) fail("%s wrong month expected=%d actual=%d\n", s, mon, tm->mon);
+ if(tm->mday != mday) fail("%s wrong mday expected=%d actual=%d\n", s, mday, tm->mday);
+ if(tm->hour != hour) fail("%s wrong hour expected=%d actual=%d\n", s, hour, tm->hour);
+ if(tm->min != min) fail("%s wrong min expected=%d actual=%d\n", s, min, tm->min);
+ if(tm->sec != sec) fail("%s wrong sec expected=%d actual=%d\n", s, sec, tm->sec);
}
void
@@ -76,33 +76,33 @@
sysfatal("shift time: %r\n");
tmtime(&tm, 1586574870, gmt);
- testtm(1, 2020, 3, 11, 3, 14, 30, &tm);
+ testtm("tmtime-gmt", 2020, 3, 11, 3, 14, 30, &tm);
tmtime(&tm, 1586574870, us_arizona);
- testtm(1, 2020, 3, 10, 20, 14, 30, &tm);
+ testtm("tmtime-az", 2020, 3, 10, 20, 14, 30, &tm);
tmtime(&tm, 0, gmt);
- testtm(3, 1970, 0, 1, 0, 0, 0, &tm);
+ testtm("tmtime-0-gmt", 1970, 0, 1, 0, 0, 0, &tm);
tmnorm(&tm);
- testtm(4, 1970, 0, 1, 0, 0, 0, &tm);
+ testtm("tmnorm-0-gmt", 1970, 0, 1, 0, 0, 0, &tm);
tmtime(&tm, 84061, gmt);
- testtm(5, 1970, 0, 1, 23, 21, 1, &tm);
+ testtm("tmtime-near0-gmt", 1970, 0, 1, 23, 21, 1, &tm);
tmnorm(&tm);
- testtm(6, 1970, 0, 1, 23, 21, 1, &tm);
+ testtm("tmnorm-near0-gmt", 1970, 0, 1, 23, 21, 1, &tm);
tmtime(&tm, 1586574870, us_arizona);
- testtm(7, 2020, 3, 10, 20, 14, 30, &tm);
+ testtm("tmtime-recent-az", 2020, 3, 10, 20, 14, 30, &tm);
tmnorm(&tm);
- testtm(8, 2020, 3, 10, 20, 14, 30, &tm);
+ testtm("tmnorm-recent-az", 2020, 3, 10, 20, 14, 30, &tm);
tmtime(&tm, 1586574870, us_eastern);
- testtm(9, 2020, 3, 10, 23, 14, 30, &tm);
+ testtm("tmtime-recent-est", 2020, 3, 10, 23, 14, 30, &tm);
tmnorm(&tm);
- testtm(10, 2020, 3, 10, 23, 14, 30, &tm);
+ testtm("tmnorm-recent-est", 2020, 3, 10, 23, 14, 30, &tm);
if(tmparse(&tm, "hhmm", "1600", gmt) == nil)
sysfatal("failed parse: %r\n");
- testtm(99, 1970, 0, 1, 16, 0, 0, &tm);
+ testtm("hhmm", 1970, 0, 1, 16, 0, 0, &tm);
if(tmparse(&tm, "YYYY-MM-DD hh:mm:ss Z", "1969-12-31 16:00:00 -0800", nil) == nil)
fail("parse failed: %r\n");
@@ -120,45 +120,45 @@
if(tmparse(&tm, "YYYY MM DD hh:mm:ss", "1969 12 31 16:00:00", gmt) == nil)
sysfatal("failed parse: %r\n");
- testtm(100, 1969, 11, 31, 16, 0, 0, &tm);
+ testtm("parse-notz1", 1969, 11, 31, 16, 0, 0, &tm);
if(tmparse(&tm, "YYYY MM DD hh:mm:ss", "1970 01 01 04:00:00", gmt) == nil)
fail("failed parse: %r\n");
- testtm(101, 1970, 0, 1, 4, 0, 0, &tm);
+ testtm("parse-notz2", 1970, 0, 1, 4, 0, 0, &tm);
if(tmparse(&tm, "YYYY MM DD", "1970 01 01", gmt) == nil)
fail("failed parse: %r\n");
- testtm(102, 1970, 0, 1, 0, 0, 0, &tm);
+ testtm("parse-notz3", 1970, 0, 1, 0, 0, 0, &tm);
- if(tmparse(&tm, "YYYY MMMM DD WW hh:mm:ss", "2020 April 10 Friday 16:04:00", gmt) == nil)
+ if(tmparse(&tm, "YYYY MMMM DD WW hh:mm:ss", "2020 April 10 Friday 16:04:00", gmt) == nil)
sysfatal("failed parse: %r\n");
- testtm(103, 2020, 3, 10, 16, 4, 0, &tm);
+ testtm("parse-notz4", 2020, 3, 10, 16, 4, 0, &tm);
if(tmparse(&tm, "MM DD hh:mm:ss", "12 31 16:00:00", gmt) == nil)
sysfatal("failed parse: %r\n");
- testtm(104, 1970, 11, 31, 16, 0, 0, &tm);
+ testtm("parse-notz5", 1970, 11, 31, 16, 0, 0, &tm);
if(tmparse(&tm, "MM DD h:mm:ss", "12 31 4:00:00", gmt) == nil)
sysfatal("failed parse: %r\n");
- testtm(105, 1970, 11, 31, 4, 0, 0, &tm);
+ testtm("parse-mmdd-hms", 1970, 11, 31, 4, 0, 0, &tm);
if(tm.tzoff != 0) print("%d wrong tzoff expected=%d actual=%d\n", 6, 0, tm.tzoff);
if(tmparse(&tm, "YYYY MM DD hh:mm:ss", "2020 04 10 23:14:30", us_eastern) == nil)
- fail("failed parse: %r\n");
- testtm(106, 2020, 3, 10, 23, 14, 30, &tm);
+ fail("failed parse: %r\n");
+ testtm("parse-est", 2020, 3, 10, 23, 14, 30, &tm);
tmtime(&tm, tm.abs, nil);
if(tmparse(&tm, "YYYY MM DD hh:mm:ss", "2020 04 10 20:14:30", us_arizona) == nil)
fail("failed parse: %r\n");
- testtm(108, 2020, 3, 10, 20, 14, 30, &tm);
+ testtm("parse-az", 2020, 3, 10, 20, 14, 30, &tm);
if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", "2020 04 10 20:14:30 EST", us_arizona) == nil)
fail("failed parse: %r\n");
- testtm(108, 2020, 3, 10, 17, 14, 30, &tm);
+ testtm("parse-tz1", 2020, 3, 10, 17, 14, 30, &tm);
if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", "2020 04 10 20:14:30 -0400", nil) == nil)
fail("failed parse: %r\n");
- testtm(108, 2020, 3, 10, 20, 14, 30, &tm);
+ testtm("parse-tz2", 2020, 3, 10, 20, 14, 30, &tm);
snprint(buf, sizeof(buf), "%τ", tmfmt(&tm, "YYYY MM DD hh:mm:ss Z"));
if(strcmp(buf, "2020 04 10 20:14:30 -0400") != 0)
fail("failed format: %s != 2020 04 10 20:14:30 -0400", buf);
@@ -166,7 +166,7 @@
/* edge case: leap year feb 29 */
if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", "2020 02 29 20:14:30 -0400", nil) == nil)
fail("failed leap year feb 29: %r\n");
- testtm(109, 2020, 1, 29, 20, 14, 30, &tm);
+ testtm("parse-leapfeb", 2020, 1, 29, 20, 14, 30, &tm);
if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", "2021 02 29 20:14:30 -0400", nil) != nil)
fail("incorrectly accepted non-leap year feb 29\n");
@@ -196,6 +196,9 @@
fail("parse: %r\n");
if(tm.abs != tt.abs)
fail("parse: wrong time (%lld != %lld)\n", tm.abs, tt.abs);
+ tm = tt;
+ tmnorm(&tm);
+ testtm("norm-rt", tt.year + 1900, tt.mon, tt.mday, tt.hour, tt.min, tt.sec, &tm);
}
if(tmtime(&tm, -624623143, nil) == nil)
@@ -252,7 +255,7 @@
/* flexible date parsing */
if(tmparse(&tm, "?YYYY ?MM DD hh:mm:ss ?ZZZ", "89 04 10 20:14:30 -0400", nil) == nil)
fail("failed parse: %r\n");
- testtm(200, 1989, 3, 10, 20, 14, 30, &tm);
+ testtm("flexdates", 1989, 3, 10, 20, 14, 30, &tm);
char **d, *flexdates[] = {
"1920 4 10 20:14:30 -0400",
"1920 04 10 20:14:30 -0400",
@@ -267,13 +270,13 @@
for(d = flexdates; *d; d++){
if(tmparse(&tm, "?YYYY ?MM DD hh:mm:ss ?ZZZ", *d, nil) == nil)
fail("failed parse: %r\n");
- testtm(201, 1920, 3, 10, 20, 14, 30, &tm);
+ testtm("flexdates", 1920, 3, 10, 20, 14, 30, &tm);
}
/* Fuzzy zone */
if(tmparse(&tm, "?YYYY ?MM DD hh:mm:ss ?ZZZ", "2020 04 10 20:14:30 NOPE", nil) == nil)
fail("failed parse: %r\n");
- testtm(201, 2020, 3, 10, 20, 14, 30, &tm);
+ testtm("fuzzy-nonzone", 2020, 3, 10, 20, 14, 30, &tm);
/* test tmnorm() offset */
memset(&tm, 0, sizeof(Tm));