ref: bd2779d6ef7d8ebf5d03825984c529a8d07a816a
parent: fab54886d65ab03e45c85ab10efd26da3d7300b7
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Jun 22 23:44:23 EDT 2020
test tmnorm
--- a/libc/date.c
+++ b/libc/date.c
@@ -50,7 +50,7 @@
main(int, char **)
{
Tm tm, tt;
- Tzone *gmt, *us_arizona, *us_eastern;
+ Tzone *gmt, *us_arizona, *us_eastern, *us_central;
Tm here, there;
Tzone *zl, *zp;
char buf[128], buf1[128];
@@ -57,16 +57,18 @@
int i, h;
tmfmtinstall();
- if((gmt = tmgetzone("GMT")) == nil)
+ if((gmt = tzload("GMT")) == nil)
sysfatal("nil gmt: %r\n");
- if((us_arizona = tmgetzone("US_Arizona")) == nil)
+ if((us_arizona = tzload("US_Arizona")) == nil)
sysfatal("nil us_arizona: %r\n");
- if((us_eastern = tmgetzone("US_Eastern")) == nil)
+ if((us_eastern = tzload("US_Eastern")) == nil)
sysfatal("nil us_eastern: %r\n");
+ if((us_central = tzload("US_Central")) == nil)
+ sysfatal("get zone: %r\n");
- if((zl = tmgetzone("local")) == nil)
+ if((zl = tzload("local")) == nil)
sysfatal("load zone: %r\n");
- if((zp = tmgetzone("US_Pacific")) == nil)
+ if((zp = tzload("US_Pacific")) == nil)
sysfatal("load zone: %r\n");
if(tmnow(&here, zl) == nil)
sysfatal("get time: %r\n");
@@ -157,7 +159,7 @@
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);
- snprint(buf, sizeof(buf), "%τ", tmfmt(&tm, "YYYY MM DD hh:mm:ss ZZZ"));
+ 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);
@@ -252,21 +254,39 @@
fail("failed parse: %r\n");
testtm(200, 1989, 3, 10, 20, 14, 30, &tm);
char **d, *flexdates[] = {
- "2020 4 10 20:14:30 -0400",
- "2020 04 10 20:14:30 -0400",
- "2020 Apr 10 20:14:30 -0400",
- "2020 Apr 10 20:14:30 -04:00"
- "2020 Apr 10 20:14:30 -04:00",
- "2020 Apr 10 20:14:30 -04:00",
- "2020 April 10 20:14:30 EDT",
+ "1920 4 10 20:14:30 -0400",
+ "1920 04 10 20:14:30 -0400",
+ "1920 Apr 10 20:14:30 -0400",
+ "1920 Apr 10 20:14:30 -04:00",
+ "1920 Apr 10 20:14:30 -04:00",
+ "1920 Apr 10 20:14:30 -04:00",
+ "1920 April 10 20:14:30 EDT",
"20 April 10 20:14:30 EDT",
nil,
};
for(d = flexdates; *d; d++){
- 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", *d, nil) == nil)
fail("failed parse: %r\n");
- testtm(201, 2020, 3, 10, 20, 14, 30, &tm);
+ testtm(201, 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);
+
+ /* test tmnorm() offset */
+ memset(&tm, 0, sizeof(Tm));
+ tm.year = 120;
+ tm.sec=0;
+ tm.min=0;
+ tm.hour=0;
+ tm.mday=22;
+ 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(failed)
exits("test failed");