Allow printing the date in compact ISO 8601 format. E.g.: date : Wed Nov 11 14:55:04 MST 2009 date -i : 20091111T145504 The 8601 format allows rc scripts to create compact timestamps for log entries, is easier to manipulate with sed/grep/etc., and is a natural key for sort(1). ('date -n' is compact and sortable, but isn't human readable or regexp friendly.) Reference: /n/sources/patch/maybe/date-iso8601 Date: Wed Nov 11 23:06:17 CET 2009 Signed-off-by: lyndon@orthanc.ca --- /sys/src/cmd/date.c Wed Nov 11 22:54:32 2009 +++ /sys/src/cmd/date.c Thu Nov 12 23:26:27 2009 @@ -1,17 +1,20 @@ #include #include -int uflg, nflg; +int iflg, nflg, uflg; void main(int argc, char *argv[]) { ulong now; + int tzhr, tzmin; + struct Tm *tm; ARGBEGIN{ + case 'i': iflg = 1; break; case 'n': nflg = 1; break; case 'u': uflg = 1; break; - default: fprint(2, "usage: date [-un] [seconds]\n"); exits("usage"); + default: fprint(2, "usage: date [-inu] [seconds]\n"); exits("usage"); }ARGEND if(argc == 1) @@ -21,6 +24,17 @@ if(nflg) print("%ld\n", now); + else if(iflg){ + if(uflg) + tm = gmtime(now); + else + tm = localtime(now); + tzhr = tm->tzoff / 3600; + tzmin = (abs(tm->tzoff) - (abs(tzhr) * 3600)) / 60; + print("%0.4d%0.2d%0.2dT%0.2d%0.2d%0.2d%+.2d%.2d\n", + tm->year + 1900, tm->mon + 1, tm->mday, + tm->hour, tm->min, tm->sec, tzhr, tzmin); + } else if(uflg) print("%s", asctime(gmtime(now))); else --- /sys/man/1/date Wed Nov 11 22:54:36 2009 +++ /sys/man/1/date Wed Nov 11 22:54:34 2009 @@ -11,19 +11,22 @@ .br .B clock .SH DESCRIPTION -Print the date, in the format +Print the date in the format .PP .B Tue Aug 16 17:03:52 CDT 1977 .PP The options are .TP -.B -u -Report Greenwich Mean Time (GMT) rather than local time. +.B -i +Print the date in compact ISO 8601 format. .TP .B -n Report the date as the number of seconds since the epoch, 00:00:00 GMT, January 1, 1970. +.TP +.B -u +Report Greenwich Mean Time (GMT) rather than local time. .PP The conversion from Greenwich Mean Time to local time depends on the .B $timezone @@ -33,7 +36,7 @@ If the optional argument .I seconds is present, it is used as the time to convert rather than -the real time. +the present time. .PP .I Clock draws a simple analog clock in its window.