ref: 3723ffc1ded7ba5fdb1466fef009d30af87db52b
parent: 399381090ea2afd020c0fa961cca686fc6d4fd7f
author: google <google@daverabbitz.ath.cx>
date: Sun Sep 2 19:05:41 EDT 2012
Add isotime() and isodate() functions to libc.
--- a/sys/man/2/ctime
+++ b/sys/man/2/ctime
@@ -1,6 +1,6 @@
.TH CTIME 2
.SH NAME
-ctime, localtime, gmtime, asctime, tm2sec, timezone \- convert date and time
+ctime, localtime, gmtime, asctime, tm2sec, timezone, isodate, isotime \- convert date and time
.SH SYNOPSIS
.B #include <u.h>
.br
@@ -23,6 +23,12 @@
long tm2sec(Tm *tm)
.PP
.B
+char* isodate(Tm *tm)
+.PP
+.B
+char* isotime(Tm *tm)
+.PP
+.B
/env/timezone
.SH DESCRIPTION
.I Ctime
@@ -83,6 +89,13 @@
.B zone
is not
.BR GMT .
+.PP
+.I Isotime
+converts a broken-down time to a date and timestamp according to ISO-8601 with timezone, and returns a string of up to 24 characters.
+.PP
+.I Isodate
+converts a broken-down time to a datestamp
+without timezone, and returns a 10 byte string.
.PP
When local time is first requested,
the program consults the
--- a/sys/src/libc/9sys/ctime.c
+++ b/sys/src/libc/9sys/ctime.c
@@ -299,3 +299,50 @@
*p = l;
return 0;
}
+
+char*
+isodate(Tm *t)
+{+ static char c[10+14+1]; /* leave room to append isotime */
+
+ ct_numb(c, t->year / 100 + 119);
+ ct_numb(c+2, t->year % 100 + 100);
+ c[4] = '-';
+ ct_numb(c+5, t->mon + 101);
+ c[7] = '-';
+ ct_numb(c+8, t->mday + 100);
+ c[10] = 0;
+ return c;
+}
+
+char*
+isotime(Tm *t)
+{+ int tz;
+ char *c, *d;
+ d = isodate(t);
+ c = d + 10; /* append to isodate */
+ c[0] = 'T';
+ ct_numb(c+1, t->hour+100);
+ c[3] = ':';
+ ct_numb(c+4, t->min+100);
+ c[6] = ':';
+ ct_numb(c+7, t->sec+100);
+ tz = t->tzoff / 60;
+ if(t->tzoff) {+ /* localtime */
+ if (t->tzoff > 0) {+ c[9] = '+';
+ } else {+ c[9] = '-';
+ tz = -tz;
+ }
+ ct_numb(c+10, tz / 60 + 100);
+ ct_numb(c+12, tz % 60 + 100);
+ c[14] = 0;
+ } else {+ c[9] = 'Z';
+ c[10] = 0;
+ }
+ return d;
+}
--
⑨