ref: bee2710c5c6c08c1921adec648a1ef628ad9268c
parent: a1ed2432f3e01e94a49263c1457523a8946180c4
author: Ori Bernstein <[email protected]>
date: Tue Jul 7 23:33:42 EDT 2020
Add more tests.
--- a/libc/date.c
+++ b/libc/date.c
@@ -37,7 +37,7 @@
}
void
-testtm(char *s, int year, int mon, int mday, int hour, int min, int sec, Tm *tm){
+testtm(char *s, int year, int mon, int mday, int hour, int min, int sec, int nsec, 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);
@@ -44,6 +44,7 @@
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);
+ if(tm->nsec != nsec) fail("%s wrong nsec expected=%d actual=%d\n", s, nsec, tm->nsec);
}
void
@@ -55,6 +56,7 @@
Tzone *zl, *zp;
char buf[128], buf1[128];
int i, h;
+ long s;
tmfmtinstall();
if((gmt = tzload("GMT")) == nil)
@@ -76,33 +78,33 @@
sysfatal("shift time: %r\n");
tmtime(&tm, 1586574870, gmt);
- testtm("tmtime-gmt", 2020, 3, 11, 3, 14, 30, &tm);
+ testtm("tmtime-gmt", 2020, 3, 11, 3, 14, 30, 0, &tm);
tmtime(&tm, 1586574870, us_arizona);
- testtm("tmtime-az", 2020, 3, 10, 20, 14, 30, &tm);
+ testtm("tmtime-az", 2020, 3, 10, 20, 14, 30, 0, &tm);
tmtime(&tm, 0, gmt);
- testtm("tmtime-0-gmt", 1970, 0, 1, 0, 0, 0, &tm);
+ testtm("tmtime-0-gmt", 1970, 0, 1, 0, 0, 0, 0, &tm);
tmnorm(&tm);
- testtm("tmnorm-0-gmt", 1970, 0, 1, 0, 0, 0, &tm);
+ testtm("tmnorm-0-gmt", 1970, 0, 1, 0, 0, 0, 0, &tm);
tmtime(&tm, 84061, gmt);
- testtm("tmtime-near0-gmt", 1970, 0, 1, 23, 21, 1, &tm);
+ testtm("tmtime-near0-gmt", 1970, 0, 1, 23, 21, 1, 0, &tm);
tmnorm(&tm);
- testtm("tmnorm-near0-gmt", 1970, 0, 1, 23, 21, 1, &tm);
+ testtm("tmnorm-near0-gmt", 1970, 0, 1, 23, 21, 1, 0, &tm);
tmtime(&tm, 1586574870, us_arizona);
- testtm("tmtime-recent-az", 2020, 3, 10, 20, 14, 30, &tm);
+ testtm("tmtime-recent-az", 2020, 3, 10, 20, 14, 30, 0, &tm);
tmnorm(&tm);
- testtm("tmnorm-recent-az", 2020, 3, 10, 20, 14, 30, &tm);
+ testtm("tmnorm-recent-az", 2020, 3, 10, 20, 14, 30, 0, &tm);
tmtime(&tm, 1586574870, us_eastern);
- testtm("tmtime-recent-est", 2020, 3, 10, 23, 14, 30, &tm);
+ testtm("tmtime-recent-est", 2020, 3, 10, 23, 14, 30, 0, &tm);
tmnorm(&tm);
- testtm("tmnorm-recent-est", 2020, 3, 10, 23, 14, 30, &tm);
+ testtm("tmnorm-recent-est", 2020, 3, 10, 23, 14, 30, 0, &tm);
if(tmparse(&tm, "hhmm", "1600", gmt, nil) == nil)
sysfatal("failed parse: %r\n");
- testtm("hhmm", 1970, 0, 1, 16, 0, 0, &tm);
+ testtm("hhmm", 1970, 0, 1, 16, 0, 0, 0, &tm);
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");
@@ -120,54 +122,76 @@
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);
+ testtm("parse-notz1", 1969, 11, 31, 16, 0, 0, 0, &tm);
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);
+ testtm("parse-notz2", 1970, 0, 1, 4, 0, 0, 0, &tm);
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);
+ testtm("parse-notz3", 1970, 0, 1, 0, 0, 0, 0, &tm);
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);
+ testtm("parse-notz4", 2020, 3, 10, 16, 4, 0, 0, &tm);
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);
+ testtm("parse-notz5", 1970, 11, 31, 16, 0, 0, 0, &tm);
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);
+ testtm("parse-mmdd-hms", 1970, 11, 31, 4, 0, 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) == nil)
fail("failed parse: %r\n");
- testtm("parse-est", 2020, 3, 10, 23, 14, 30, &tm);
+ testtm("parse-est", 2020, 3, 10, 23, 14, 30, 0, &tm);
tmtime(&tm, tmnorm(&tm), 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);
+ testtm("parse-az", 2020, 3, 10, 20, 14, 30, 0, &tm);
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);
+ testtm("parse-tz1", 2020, 3, 10, 17, 14, 30, 0, &tm);
- if(tmparse(&tm, "YYYY MM DD hh:mm:ss ZZZ", "2020 04 10 20:14:30 -0400", nil, nil) == nil)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss Z", "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);
+ testtm("parse-tz2", 2020, 3, 10, 20, 14, 30, 0, &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);
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss.ttt", "2020 04 10 20:14:30.207", nil, nil) == nil)
+ fail("failed parse: %r\n");
+ testtm("parse-milliseconds", 2020, 3, 10, 20, 14, 30, 207*1000*1000, &tm);
+
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss.nnn", "2020 04 10 20:14:30.207", nil, nil) == nil)
+ fail("failed parse: %r\n");
+ testtm("parse-nanoseconds", 2020, 3, 10, 20, 14, 30, 207, &tm);
+
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss.nnn", "2020 04 10 20:14:30.999", nil, nil) == nil)
+ fail("failed parse: %r\n");
+ testtm("parse-nnn2", 2020, 3, 10, 20, 14, 30, 999, &tm);
+ snprint(buf, sizeof(buf), "%τ", tmfmt(&tm, "YYYY MM DD hh:mm:ss.nnn"));
+ if(strcmp(buf, "2020 04 10 20:14:30.999") != 0)
+ fail("failed format: %s != 2020 04 10 20:14:30.999", buf);
+
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss.ttt", "2020 04 10 20:14:30.999", nil, nil) == nil)
+ fail("failed parse: %r\n");
+ testtm("parse-nnn2", 2020, 3, 10, 20, 14, 30, 999*1000*1000, &tm);
+ snprint(buf, sizeof(buf), "%τ", tmfmt(&tm, "YYYY MM DD hh:mm:ss.ttt"));
+ if(strcmp(buf, "2020 04 10 20:14:30.999") != 0)
+ fail("failed format: %s != 2020 04 10 20:14:30.999", 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) == nil)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss Z", "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) != nil)
+ testtm("parse-leapfeb", 2020, 1, 29, 20, 14, 30, 0, &tm);
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss Z", "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 */
@@ -198,7 +222,7 @@
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);
+ testtm("norm-rt", tt.year + 1900, tt.mon, tt.mday, tt.hour, tt.min, tt.sec, 0, &tm);
}
if(tmtime(&tm, -624623143, nil) == nil)
@@ -221,9 +245,9 @@
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) == nil)
+ if(tmparse(&tm, "YYYY MM DD hh:mm:ss Z", buf, nil, nil) == nil)
fail("parse: %r\n");
- if(tmparse(&tt, "YYYY MM DD hh:mm:ss ZZZ A", buf1, nil, nil) == nil)
+ if(tmparse(&tt, "YYYY MM DD hh:mm:ss Z A", buf1, nil, nil) == nil)
fail("parse: %r\n");
if(tmnorm(&tm) != tmnorm(&tt))
print("bad am/pm parsed: %s != %s (%lld != %lld)\n", buf, buf1, tmnorm(&tm), tmnorm(&tt));
@@ -255,7 +279,7 @@
/* flexible date parsing */
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);
+ testtm("flexdates", 1989, 3, 10, 20, 14, 30, 0, &tm);
char **d, *flexdates[] = {
"1920 4 10 20:14:30 -0400",
"1920 04 10 20:14:30 -0400",
@@ -270,13 +294,13 @@
for(d = flexdates; *d; d++){
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);
+ testtm("flexdates", 1920, 3, 10, 20, 14, 30, 0, &tm);
}
/* Fuzzy zone */
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);
+ testtm("fuzzy-nonzone", 2020, 3, 10, 20, 14, 30, 0, &tm);
/* test tmnorm() offset */
memset(&tm, 0, sizeof(Tm));
@@ -290,6 +314,37 @@
tmnorm(&tm);
if(tmnorm(&tm) != 1592802000)
fail("tmnorm is not using the daylight savings time offset. %lld != 1592809200\n", tmnorm(&tm));
+
+ memset(&tm, 0, sizeof(Tm));
+ if(tmnow(&tm, us_central) == nil)
+ fail("tmnow(): %r");
+ tm.year = 120;
+ tm.sec=0;
+ tm.min=0;
+ tm.hour=0;
+ tm.mday=22;
+ tm.mon=5;
+ tm.tz=us_central;
+ s = tmnorm(&tm);
+ if(s != 1592802000)
+ fail("tmnorm is not using the daylight savings time offset. %lld != 1592809200\n", s);
+ tm.year = 120;
+ tm.sec=0;
+ tm.min=0;
+ tm.hour=0;
+ tm.mday=22;
+ tm.mon=0;
+ s = tmnorm(&tm);
+ if(s != 1579672800)
+ fail("tmnorm is not using the daylight savings time offset. %lld != 1579672800\n", s);
+ tm.tz=us_eastern;
+ s = tmnorm(&tm);
+ if(s != 1579669200)
+ fail("tmnorm converted to us_eastern. %lld != 1579669200\n", s);
+ tm.tz=us_central;
+ s = tmnorm(&tm);
+ if(s != 1579672800)
+ fail("tmnorm converted back to us_central. %lld != 1579672800\n", s);
if(failed)
exits("test failed");
--- a/libc/mkfile
+++ b/libc/mkfile
@@ -3,5 +3,6 @@
TEST=\
date\
pow\
+ zones\
<../regress
--- /dev/null
+++ b/libc/zones.rc
@@ -1,0 +1,19 @@
+#!/bin/rc
+
+rfork en
+
+msg=()
+for(f in /adm/timezone/*){
+ if(! ~ $f /adm/timezone/README){
+ cat $f >/env/timezone
+ tm=`{date -m}
+ x=`{date -n}
+ y=`{seconds $"tm}
+ if(! ~ $x $y){
+ echo $f $tm $x $y are not equal
+ msg=($msg $f)
+ }
+ }
+}
+
+exit $"msg