date: rewrite and add manual page
authorAlan Cox <alan@linux.intel.com>
Sat, 21 Jul 2018 23:35:11 +0000 (00:35 +0100)
committerAlan Cox <alan@linux.intel.com>
Sat, 21 Jul 2018 23:35:11 +0000 (00:35 +0100)
We still need to support custom formats but that needs the fancy time format
stuff we don't yet have.

Applications/util/date.1 [new file with mode: 0644]
Applications/util/date.c

diff --git a/Applications/util/date.1 b/Applications/util/date.1
new file mode 100644 (file)
index 0000000..85fa414
--- /dev/null
@@ -0,0 +1,12 @@
+DATE(1)
+## NAME
+*date* - display the current time and date
+## SYNOPSIS
+*date* \[-u\]
+## DESCRIPTION
+The *date* command displays the current date and time. If the *-u* option is
+specified the time and date are shown in UTC, otherwise they are shown in
+local time.
+## STANDARDS
+The *date* command is from POSIX 1003.1P. Fuzix does not currently implement
+custom formats or system time setting via date.
index cbb20de..9f5e036 100644 (file)
@@ -1,15 +1,40 @@
 #include <time.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
-int main(int argc, char *argv[])
+static void usage(void)
 {
-    time_t now;
-    const char *p;
+    write(2, "date: [-u]\n", 11);
+    exit(EXIT_FAILURE);
+}
 
-    now = time(NULL);
-    p = ctime(&now);
-    write(1, p, strlen(p));
+int main(int argc, char *argv[])
+{
+    time_t t;
+    struct tm *tm;
+    const char *ts, *z;
+    int utc = 0;
+    
+    if (argc > 1 && strcmp(argv[1], "-u") == 0) {
+        utc = 1;
+        argc--;
+    }
+    if (argc != 1)
+        usage();
 
+    time(&t);
+    if (utc)
+        tm = gmtime(&t);
+    else
+        tm = localtime(&t);
+    ts = asctime(tm);
+    /* Up to the time zone */
+    write(1, ts, 20);
+    /* Zone */
+    z = tzname[tm->tm_isdst];
+    write(1, z, strlen(z));
+    /* Year */
+    write(1, ts + 19, 6);
     return 0;
 }