shithub: riscv

Download patch

ref: 2dfe66d72f23e9e4cf24cd3634ebe786c68440e3
parent: 87b6b93257d69ab29c74c278b943f8948761b3f0
author: google <[email protected]>
date: Sat Sep 1 20:33:39 EDT 2012

Add -i flag to date: prints ISO-8601 datestamp
Add -t flag to date: prints ISO-8601 timestamp

--- a/sys/man/1/date
+++ b/sys/man/1/date
@@ -24,6 +24,12 @@
 .B -n
 Report the date as the number of seconds since the
 epoch, 00:00:00 GMT, January 1, 1970.
+.TP
+.B -i
+Report the date as ISO-8601 without time and timezone suffix.
+.TP
+.B -t
+Report the date as ISO-8601 with time and timezone suffix.
 .PP
 The conversion from Greenwich Mean Time to local time depends on the
 .B $timezone
--- a/sys/src/cmd/date.c
+++ b/sys/src/cmd/date.c
@@ -1,17 +1,66 @@
 #include <u.h>
 #include <libc.h>
 
-int uflg, nflg;
+int uflg, nflg, iflg, tflg;
 
+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;
+}
+
 void
 main(int argc, char *argv[])
 {
 	ulong now;
-
+	Tm *tm;
 	ARGBEGIN{
 	case 'n':	nflg = 1; break;
 	case 'u':	uflg = 1; break;
-	default:	fprint(2, "usage: date [-un] [seconds]\n"); exits("usage");
+	case 't':	tflg = 1; /* implies -i */
+	case 'i':	iflg = 1; break;
+	default:	fprint(2, "usage: date [-itun] [seconds]\n"); exits("usage");
 	}ARGEND
 
 	if(argc == 1)
@@ -21,10 +70,17 @@
 
 	if(nflg)
 		print("%ld\n", now);
-	else if(uflg)
-		print("%s", asctime(gmtime(now)));
-	else
-		print("%s", ctime(now));
-	
+	else if(iflg) {
+		tm = uflg ? gmtime(now) : localtime(now);
+		if(tflg)
+			print("%s\n", isotime(tm));
+		else
+			print("%s\n", isodate(tm));
+	} else {
+		if(uflg)
+			print("%s", asctime(gmtime(now)));
+		else
+			print("%s", ctime(now));
+	}
 	exits(0);
 }