ref: a1ed2432f3e01e94a49263c1457523a8946180c4
parent: 0e9fc18398b15c81a26de4bac1c41f7afb15edd3
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Jul 6 00:16:36 EDT 2020
update to new api
--- a/libc/date.c
+++ b/libc/date.c
@@ -9,9 +9,9 @@
void
printtm(Tm *tm)
{
- fprint(2, "abs=%lld sec=%d min=%d hour=%d mday=%d mon=%d"
+ fprint(2, "sec=%d min=%d hour=%d mday=%d mon=%d"
" year=%d day=%d yday=%d zone=%s tzoff=%d\n",
- tm->abs, /* milliseconds since Jan 1 1970, GMT */
+// tm->abs, /* milliseconds since Jan 1 1970, GMT */
tm->sec, /* seconds (range 0..59) */
tm->min, /* minutes (0..59) */
tm->hour, /* hours (0..23) */
@@ -72,7 +72,7 @@
sysfatal("load zone: %r\n");
if(tmnow(&here, zl) == nil)
sysfatal("get time: %r\n");
- if(tmtime(&there, here.abs, zp) == nil)
+ if(tmtime(&there, tmnorm(&here), zp) == nil)
sysfatal("shift time: %r\n");
tmtime(&tm, 1586574870, gmt);
@@ -100,63 +100,63 @@
tmnorm(&tm);
testtm("tmnorm-recent-est", 2020, 3, 10, 23, 14, 30, &tm);
- if(tmparse(&tm, "hhmm", "1600", gmt) == nil)
+ if(tmparse(&tm, "hhmm", "1600", gmt, nil) == nil)
sysfatal("failed parse: %r\n");
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)
+ if(tmparse(&tm, "YYYY-MM-DD hh:mm:ss Z", "1969-12-31 16:00:00 -0800", nil, nil) == nil)
fail("parse failed: %r\n");
- if(tm.abs != 0)
- fail("wrong result: %lld != 0\n", tm.abs);
+ if(tmnorm(&tm) != 0)
+ fail("wrong result: %lld != 0\n", tmnorm(&tm));
- if(tmparse(&tm, "YYYY MM DD", "1990,01,03", nil) == nil)
+ if(tmparse(&tm, "YYYY MM DD", "1990,01,03", nil, nil) == nil)
fail("comma parse failed");
- if(tm.abs != 631324800)
+ if(tmnorm(&tm) != 631324800)
fail("wrong result");
- if(tmparse(&tm, "YYYY MM DD", "1990 ,\t01,03", nil) == nil)
+ if(tmparse(&tm, "YYYY MM DD", "1990 ,\t01,03", nil, nil) == nil)
fail("comma parse failed");
- if(tm.abs != 631324800)
+ if(tmnorm(&tm) != 631324800)
fail("wrong result");
- if(tmparse(&tm, "YYYY MM DD hh:mm:ss", "1969 12 31 16:00:00", gmt) == nil)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss", "1969 12 31 16:00:00", gmt, nil) == nil)
sysfatal("failed parse: %r\n");
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)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss", "1970 01 01 04:00:00", gmt, nil) == nil)
fail("failed parse: %r\n");
testtm("parse-notz2", 1970, 0, 1, 4, 0, 0, &tm);
- if(tmparse(&tm, "YYYY MM DD", "1970 01 01", gmt) == nil)
+ if(tmparse(&tm, "YYYY MM DD", "1970 01 01", gmt, nil) == nil)
fail("failed parse: %r\n");
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) == nil)
sysfatal("failed parse: %r\n");
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)
+ if(tmparse(&tm, "MM DD hh:mm:ss", "12 31 16:00:00", gmt, nil) == nil)
sysfatal("failed parse: %r\n");
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)
+ if(tmparse(&tm, "MM DD h:mm:ss", "12 31 4:00:00", gmt, nil) == nil)
sysfatal("failed parse: %r\n");
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)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss", "2020 04 10 23:14:30", us_eastern, nil) == nil)
fail("failed parse: %r\n");
testtm("parse-est", 2020, 3, 10, 23, 14, 30, &tm);
- tmtime(&tm, tm.abs, nil);
+ tmtime(&tm, tmnorm(&tm), nil);
- if(tmparse(&tm, "YYYY MM DD hh:mm:ss", "2020 04 10 20:14:30", us_arizona) == nil)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss", "2020 04 10 20:14:30", us_arizona, nil) == nil)
fail("failed parse: %r\n");
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)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", "2020 04 10 20:14:30 EST", us_arizona, nil) == nil)
fail("failed parse: %r\n");
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)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", "2020 04 10 20:14:30 -0400", nil, nil) == nil)
fail("failed parse: %r\n");
testtm("parse-tz2", 2020, 3, 10, 20, 14, 30, &tm);
snprint(buf, sizeof(buf), "%τ", tmfmt(&tm, "YYYY MM DD hh:mm:ss Z"));
@@ -164,10 +164,10 @@
fail("failed format: %s != 2020 04 10 20:14:30 -0400", buf);
/* 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)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", "2020 02 29 20:14:30 -0400", nil, nil) == nil)
fail("failed leap year feb 29: %r\n");
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)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", "2021 02 29 20:14:30 -0400", nil, nil) != nil)
fail("incorrectly accepted non-leap year feb 29\n");
/* lots of round trips: Jan 1960 => Jun 2020, in almost-11 day increments */
@@ -174,14 +174,14 @@
for(i = -315619200; i < 1592179806; i += 23*3600 + 1732){
if(tmtime(&tm, i, nil) == nil)
fail("load time %d\n", i);
- if(tm.abs != i)
+ if(tmnorm(&tm) != i)
fail("wrong load time: %d\n", i);
if(snprint(buf, sizeof(buf), "%τ", tmfmt(&tm, "W MMM DD hh:mm:ss Z YYYY")) == -1)
fail("format: %r\n");
- if(tmparse(&tt, "W MMM DD hh:mm:ss Z YYYY", buf, nil) == nil)
+ if(tmparse(&tt, "W MMM DD hh:mm:ss Z YYYY", buf, nil, nil) == nil)
fail("parse: %r\n");
- if(tm.abs != tt.abs)
- fail("parse: wrong time (%lld != %lld)\n", tm.abs, tt.abs);
+ if(tmnorm(&tm) != tmnorm(&tt))
+ fail("parse: wrong time (%lld != %lld)\n", tmnorm(&tm), tmnorm(&tt));
}
/* lots of round trips: Jan 1960 => Jun 2020, in almost-dailyincrements, now with timezone */
@@ -188,14 +188,14 @@
for(i = -315619200; i < 1592179806; i += 23*3600 + 1732){
if(tmtime(&tm, i, us_eastern) == nil)
fail("load time %d\n", i);
- if(tm.abs != i)
+ if(tmnorm(&tm) != i)
fail("wrong load time: %d\n", i);
if(snprint(buf, sizeof(buf), "%τ", tmfmt(&tm, "W MMM DD hh:mm:ss Z YYYY")) == -1)
fail("format: %r\n");
- if(tmparse(&tt, "W MMM DD hh:mm:ss Z YYYY", buf, us_arizona) == nil)
+ if(tmparse(&tt, "W MMM DD hh:mm:ss Z YYYY", buf, us_arizona, nil) == nil)
fail("parse: %r\n");
- if(tm.abs != tt.abs)
- fail("parse: wrong time (%lld != %lld)\n", tm.abs, tt.abs);
+ if(tmnorm(&tm) != tmnorm(&tt))
+ fail("parse: wrong time (%lld != %lld)\n", tmnorm(&tm), tmnorm(&tt));
tm = tt;
tmnorm(&tm);
testtm("norm-rt", tt.year + 1900, tt.mon, tt.mday, tt.hour, tt.min, tt.sec, &tm);
@@ -221,12 +221,12 @@
h = 12;
snprint(buf, sizeof(buf), "2021 02 01 %d:14:30 -0400", i);
snprint(buf1, sizeof(buf1), "2021 02 01 %d:14:30 -0400 %s", h, (i < 12) ? "AM" : "PM");
- if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", buf, nil) == nil)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", buf, nil, nil) == nil)
fail("parse: %r\n");
- if(tmparse(&tt, "YYYY MM DD hh:mm:ss ZZZ A", buf1, nil) == nil)
+ if(tmparse(&tt, "YYYY MM DD hh:mm:ss ZZZ A", buf1, nil, nil) == nil)
fail("parse: %r\n");
- if(tm.abs != tt.abs)
- print("bad am/pm parsed: %s != %s (%lld != %lld)\n", buf, buf1, tm.abs, tt.abs);
+ if(tmnorm(&tm) != tmnorm(&tt))
+ print("bad am/pm parsed: %s != %s (%lld != %lld)\n", buf, buf1, tmnorm(&tm), tmnorm(&tt));
}
/* Time zone boundaries: entering DST */
@@ -235,10 +235,10 @@
if(snprint(buf, sizeof(buf), "%τ", tmfmt(&tm, nil)) == -1)
fail("format: %r");
memset(&tm, 0, sizeof(tm));
- if(tmparse(&tm, "W MMM D hh:mm:ss ZZZ YYYY", buf, nil) == nil)
+ if(tmparse(&tm, "W MMM D hh:mm:ss ZZZ YYYY", buf, nil, nil) == nil)
fail("parse: %r\n");
- if(tm.abs != 1520733600)
- fail("round trip timezone: %lld != 1520733600\n", tm.abs);
+ if(tmnorm(&tm) != 1520733600)
+ fail("round trip timezone: %lld != 1520733600\n", tmnorm(&tm));
/* Time zone boundaries: leaving DST */
if(tmtime(&tm, 1541296800, us_eastern) == nil)
@@ -246,14 +246,14 @@
if(snprint(buf, sizeof(buf), "%τ", tmfmt(&tm, nil)) == -1)
fail("format: %r\n");
memset(&tm, 0, sizeof(tm));
- if(tmparse(&tm, "W MMM D hh:mm:ss ZZZ YYYY", buf, nil) == nil)
+ if(tmparse(&tm, "W MMM D hh:mm:ss ZZZ YYYY", buf, nil, nil) == nil)
fail("parse: %r");
- if(tm.abs != 1541296800)
- fail("round trip timezone: %lld != 1541296800\n", tm.abs);
+ if(tmnorm(&tm) != 1541296800)
+ fail("round trip timezone: %lld != 1541296800\n", tmnorm(&tm));
/* flexible date parsing */
- if(tmparse(&tm, "?YYYY ?MM DD hh:mm:ss ?ZZZ", "89 04 10 20:14:30 -0400", nil) == nil)
+ if(tmparse(&tm, "?YYYY ?MM DD hh:mm:ss ?ZZZ", "89 04 10 20:14:30 -0400", nil, nil) == nil)
fail("failed parse: %r\n");
testtm("flexdates", 1989, 3, 10, 20, 14, 30, &tm);
char **d, *flexdates[] = {
@@ -268,13 +268,13 @@
nil,
};
for(d = flexdates; *d; d++){
- if(tmparse(&tm, "?YYYY ?MM DD hh:mm:ss ?ZZZ", *d, nil) == nil)
+ if(tmparse(&tm, "?YYYY ?MM DD hh:mm:ss ?ZZZ", *d, nil, nil) == nil)
fail("failed parse: %r\n");
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)
+ if(tmparse(&tm, "?YYYY ?MM DD hh:mm:ss ?ZZZ", "2020 04 10 20:14:30 NOPE", nil, nil) == nil)
fail("failed parse: %r\n");
testtm("fuzzy-nonzone", 2020, 3, 10, 20, 14, 30, &tm);
@@ -288,8 +288,8 @@
tm.mon=5;
tm.tz=us_central;
tmnorm(&tm);
- if(tm.abs != 1592802000)
- fail("tmnorm is not using the daylight savings time offset. %lld != 1592809200\n", tm.abs);
+ if(tmnorm(&tm) != 1592802000)
+ fail("tmnorm is not using the daylight savings time offset. %lld != 1592809200\n", tmnorm(&tm));
if(failed)
exits("test failed");