date.c - 9base - revived minimalist port of Plan 9 userland to Unix
 (HTM) git clone git://git.suckless.org/9base
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       date.c (1207B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 
            4 int uflg, nflg, iflg, tflg;
            5 
            6 char*
            7 isodate(Tm *t)
            8 {
            9         static char c[25]; /* leave room to append isotime */
           10         snprint(c, 11, "%04d-%02d-%02d", 
           11                 t->year + 1900, t->mon + 1, t->mday);
           12         return c;
           13 }
           14 
           15 char*
           16 isotime(Tm *t)
           17 {
           18         int tz;
           19         char *c, *d;
           20         d = isodate(t);
           21         c = d+10;
           22         snprint(c, 10, "T%02d:%02d:%02d",
           23                 t->hour, t->min, t->sec); /* append to isodate */
           24         tz = t->tzoff / 60;
           25         if(t->tzoff) {
           26                 /* localtime */
           27                 if (t->tzoff > 0) {
           28                         c[9] = '+';
           29                 } else {
           30                         c[9] = '-';
           31                         tz = -tz;
           32                 }
           33                 snprint(c+10, 5, "%02d%02d", tz / 60, tz % 60);
           34         } else {
           35                 c[9] = 'Z';
           36                 c[10] = 0;
           37         }
           38         return d;
           39 }
           40 
           41 void
           42 main(int argc, char *argv[])
           43 {
           44         ulong now;
           45         Tm *tm;
           46         ARGBEGIN{
           47         case 'n':        nflg = 1; break;
           48         case 'u':        uflg = 1; break;
           49         case 't':        tflg = 1; /* implies -i */
           50         case 'i':        iflg = 1; break;
           51         default:        fprint(2, "usage: date [-itun] [seconds]\n"); exits("usage");
           52         }ARGEND
           53 
           54         if(argc == 1)
           55                 now = strtoul(*argv, 0, 0);
           56         else
           57                 now = time(0);
           58 
           59         if(nflg)
           60                 print("%ld\n", now);
           61         else {
           62                 tm = uflg ? gmtime(now) : localtime(now);
           63                 if(iflg) {
           64                         if(tflg)
           65                                 print("%s\n", isotime(tm));
           66                         else
           67                                 print("%s\n", isodate(tm));
           68                 } else
           69                         print("%s", asctime(tm));
           70         }
           71         exits(0);
           72 }