Reorganise, make it just one function, remove -v option it didnt work properly anyway...
[oweals/busybox.git] / coreutils / date.c
index 8b6a0620d19ee3bb4aeb030619feb66d513e9f0d..d68c04a8dbee8cb2aa3a1bba5ceac4dc6de0b47f 100644 (file)
@@ -3,6 +3,8 @@
  * Mini date implementation for busybox
  *
  * by Matthew Grant <grantma@anathoth.gen.nz>
+ * 
+ * iso-format handling added by Robert Griebl <griebl@gmx.de>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
 */
 
-#include "busybox.h"
-#define BB_DECLARE_EXTERN
-#define bb_need_invalid_date
-#define bb_need_memory_exhausted
-#include "messages.c"
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/time.h>
@@ -33,6 +30,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <getopt.h>
+#include "busybox.h"
 
 
 /* This 'date' command supports only 2 time setting formats, 
 
 /* Default input handling to save suprising some people */
 
-struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
+static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
 {
        int nr;
 
-       nr = sscanf(t_string, "%2d%2d%2d%2d%d",
-                               &(tm_time->tm_mon),
-                               &(tm_time->tm_mday),
-                               &(tm_time->tm_hour),
-                               &(tm_time->tm_min), &(tm_time->tm_year));
+       nr = sscanf(t_string, "%2d%2d%2d%2d%d", &(tm_time->tm_mon),
+                               &(tm_time->tm_mday), &(tm_time->tm_hour), &(tm_time->tm_min),
+                               &(tm_time->tm_year));
 
        if (nr < 4 || nr > 5) {
-               error_msg_and_die(invalid_date, t_string); 
+               error_msg_and_die(invalid_date, t_string);
        }
 
        /* correct for century  - minor Y2K problem here? */
-       if (tm_time->tm_year >= 1900)
+       if (tm_time->tm_year >= 1900) {
                tm_time->tm_year -= 1900;
+       }
        /* adjust date */
        tm_time->tm_mon -= 1;
 
@@ -73,56 +70,45 @@ struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
 
 /* The new stuff for LRP */
 
-struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
+static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
 {
        struct tm t;
 
        /* Parse input and assign appropriately to tm_time */
 
-       if (t=*tm_time,sscanf(t_string, "%d:%d:%d",
-                          &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) {
-                                       /* no adjustments needed */
-
-       } else if (t=*tm_time,sscanf(t_string, "%d:%d",
-                                         &t.tm_hour, &t.tm_min) == 2) {
-                                       /* no adjustments needed */
-
-
-       } else if (t=*tm_time,sscanf(t_string, "%d.%d-%d:%d:%d",
-                                         &t.tm_mon,
-                                         &t.tm_mday,
-                                         &t.tm_hour,
-                                         &t.tm_min, &t.tm_sec) == 5) {
-
-               t.tm_mon -= 1;  /* Adjust dates from 1-12 to 0-11 */
-
-       } else if (t=*tm_time,sscanf(t_string, "%d.%d-%d:%d",
-                                         &t.tm_mon,
-                                         &t.tm_mday,
-                                         &t.tm_hour, &t.tm_min) == 4) {
-
-               t.tm_mon -= 1;  /* Adjust dates from 1-12 to 0-11 */
-
-       } else if (t=*tm_time,sscanf(t_string, "%d.%d.%d-%d:%d:%d",
-                                         &t.tm_year,
-                                         &t.tm_mon,
-                                         &t.tm_mday,
-                                         &t.tm_hour,
-                                         &t.tm_min, &t.tm_sec) == 6) {
-
+       if (t =
+               *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min,
+                                                &t.tm_sec) == 3) {
+               /* no adjustments needed */
+       } else if (t =
+                          *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour,
+                                                               &t.tm_min) == 2) {
+               /* no adjustments needed */
+       } else if (t =
+                          *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon,
+                                                               &t.tm_mday, &t.tm_hour, &t.tm_min,
+                                                               &t.tm_sec) == 5) {
+               /* Adjust dates from 1-12 to 0-11 */
+               t.tm_mon -= 1;
+       } else if (t =
+                          *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon,
+                                                               &t.tm_mday, &t.tm_hour, &t.tm_min) == 4) {
+               /* Adjust dates from 1-12 to 0-11 */
+               t.tm_mon -= 1;
+       } else if (t =
+                          *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
+                                                               &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min,
+                                                               &t.tm_sec) == 6) {
                t.tm_year -= 1900;      /* Adjust years */
                t.tm_mon -= 1;  /* Adjust dates from 1-12 to 0-11 */
-
-       } else if (t=*tm_time,sscanf(t_string, "%d.%d.%d-%d:%d",
-                                         &t.tm_year,
-                                         &t.tm_mon,
-                                         &t.tm_mday,
-                                         &t.tm_hour, &t.tm_min) == 5) {
+       } else if (t =
+                          *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year,
+                                                               &t.tm_mon, &t.tm_mday, &t.tm_hour,
+                                                               &t.tm_min) == 5) {
                t.tm_year -= 1900;      /* Adjust years */
                t.tm_mon -= 1;  /* Adjust dates from 1-12 to 0-11 */
-
        } else {
-               error_msg_and_die(invalid_date, t_string); 
+               error_msg_and_die(invalid_date, t_string);
        }
        *tm_time = t;
        return (tm_time);
@@ -142,45 +128,73 @@ int date_main(int argc, char **argv)
        time_t tm;
        struct tm tm_time;
 
+#ifdef CONFIG_FEATURE_DATE_ISOFMT
+       int ifmt = 0;
+
+# define GETOPT_ISOFMT  "I::"
+#else
+# define GETOPT_ISOFMT
+#endif
+
        /* Interpret command line args */
-       while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
+       while ((c = getopt(argc, argv, "Rs:ud:" GETOPT_ISOFMT)) != EOF) {
                switch (c) {
-                       case 'R':
-                               rfc822 = 1;
-                               break;
-                       case 's':
-                               set_time = 1;
-                               if ((date_str != NULL) || ((date_str = optarg) == NULL)) {
-                                       usage(date_usage);
+               case 'R':
+                       rfc822 = 1;
+                       break;
+               case 's':
+                       set_time = 1;
+                       if ((date_str != NULL) || ((date_str = optarg) == NULL)) {
+                               show_usage();
+                       }
+                       break;
+               case 'u':
+                       utc = 1;
+                       if (putenv("TZ=UTC0") != 0)
+                               error_msg_and_die(memory_exhausted);
+                       break;
+               case 'd':
+                       use_arg = 1;
+                       if ((date_str != NULL) || ((date_str = optarg) == NULL))
+                               show_usage();
+                       break;
+#ifdef CONFIG_FEATURE_DATE_ISOFMT
+               case 'I':
+                       if (!optarg)
+                               ifmt = 1;
+                       else {
+                               int ifmt_len = xstrlen(optarg);
+
+                               if ((ifmt_len <= 4)
+                                       && (strncmp(optarg, "date", ifmt_len) == 0)) {
+                                       ifmt = 1;
+                               } else if ((ifmt_len <= 5)
+                                                  && (strncmp(optarg, "hours", ifmt_len) == 0)) {
+                                       ifmt = 2;
+                               } else if ((ifmt_len <= 7)
+                                                  && (strncmp(optarg, "minutes", ifmt_len) == 0)) {
+                                       ifmt = 3;
+                               } else if ((ifmt_len <= 7)
+                                                  && (strncmp(optarg, "seconds", ifmt_len) == 0)) {
+                                       ifmt = 4;
                                }
-                               break;
-                       case 'u':
-                               utc = 1;
-                               if (putenv("TZ=UTC0") != 0)
-                                       error_msg_and_die(memory_exhausted);
-                               break;
-                       case 'd':
-                               use_arg = 1;
-                               if ((date_str != NULL) || ((date_str = optarg) == NULL))
-                                       usage(date_usage);
-                               break;
-                       default:
-                               usage(date_usage);
+                       }
+                       if (ifmt) {
+                               break;  /* else show_usage(); */
+                       }
+#endif
+               default:
+                       show_usage();
                }
        }
 
-       if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
-               date_fmt = &argv[optind][1];   /* Skip over the '+' */
-       else if (date_str == NULL) {
+
+       if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
+               date_fmt = &argv[optind][1];    /* Skip over the '+' */
+       } else if (date_str == NULL) {
                set_time = 1;
                date_str = argv[optind];
-       } 
-#if 0
-       else {
-               error_msg("date_str='%s'  date_fmt='%s'\n", date_str, date_fmt);
-               usage(date_usage);
        }
-#endif
 
        /* Now we have parsed all the information except the date format
           which depends on whether the clock is being set or read */
@@ -203,20 +217,18 @@ int date_main(int argc, char **argv)
                        date_conv_time(&tm_time, date_str);
                }
 
-               /* Correct any day of week and day of year etc fields */
+               /* Correct any day of week and day of year etc. fields */
                tm = mktime(&tm_time);
-               if (tm < 0)
-                       error_msg_and_die(invalid_date, date_str); 
-               if ( utc ) {
-                       if (putenv("TZ=UTC0") != 0)
-                               error_msg_and_die(memory_exhausted);
+               if (tm < 0) {
+                       error_msg_and_die(invalid_date, date_str);
+               }
+               if (utc && (putenv("TZ=UTC0") != 0)) {
+                       error_msg_and_die(memory_exhausted);
                }
 
                /* if setting time, set it */
-               if (set_time) {
-                       if (stime(&tm) < 0) {
-                               perror_msg("cannot set date");
-                       }
+               if (set_time && (stime(&tm) < 0)) {
+                       perror_msg("cannot set date");
                }
        }
 
@@ -224,12 +236,32 @@ int date_main(int argc, char **argv)
 
        /* Deal with format string */
        if (date_fmt == NULL) {
-               date_fmt = (rfc822
-                                       ? (utc
-                                          ? "%a, %_d %b %Y %H:%M:%S GMT"
-                                          : "%a, %_d %b %Y %H:%M:%S %z")
-                                       : "%a %b %e %H:%M:%S %Z %Y");
+#ifdef CONFIG_FEATURE_DATE_ISOFMT
+               switch (ifmt) {
+               case 4:
+                       date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
+                       break;
+               case 3:
+                       date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
+                       break;
+               case 2:
+                       date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
+                       break;
+               case 1:
+                       date_fmt = "%Y-%m-%d";
+                       break;
+               case 0:
+               default:
+#endif
+                       date_fmt =
+                               (rfc822
+                                ? (utc ? "%a, %e %b %Y %H:%M:%S GMT" :
+                                       "%a, %e %b %Y %H:%M:%S %z") : "%a %b %e %H:%M:%S %Z %Y");
 
+#ifdef CONFIG_FEATURE_DATE_ISOFMT
+                       break;
+               }
+#endif
        } else if (*date_fmt == '\0') {
                /* Imitate what GNU 'date' does with NO format string! */
                printf("\n");
@@ -245,7 +277,7 @@ int date_main(int argc, char **argv)
        /* Print OUTPUT (after ALL that!) */
        t_buff = xmalloc(201);
        strftime(t_buff, 200, date_fmt, &tm_time);
-       printf("%s\n", t_buff);
+       puts(t_buff);
 
        return EXIT_SUCCESS;
 }