summaryrefslogtreecommitdiff
path: root/sys/src/libc/9sys
diff options
context:
space:
mode:
authorgoogle <google@daverabbitz.ath.cx>2012-09-02 23:05:41 +1200
committergoogle <google@daverabbitz.ath.cx>2012-09-02 23:05:41 +1200
commitcdb7bdde96c0147fffe6d568ac7837839f678d8c (patch)
tree07a251e85b5f74287bf65db1d820d8c7fd06fa30 /sys/src/libc/9sys
parent67e047589a4257145c2507f2e7814a512434306a (diff)
Add isotime() and isodate() functions to libc.
Diffstat (limited to 'sys/src/libc/9sys')
-rw-r--r--sys/src/libc/9sys/ctime.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/sys/src/libc/9sys/ctime.c b/sys/src/libc/9sys/ctime.c
index 11340ea41..a99727225 100644
--- a/sys/src/libc/9sys/ctime.c
+++ b/sys/src/libc/9sys/ctime.c
@@ -299,3 +299,50 @@ rd_long(char **f, long *p)
*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;
+}