When FEATURE_DATE_COMPAT is enabled, also permit POSIX-style
MMDDHHMM[[CC]YY] formats to be used with -d.

This form is used by ancient versions of hwclock.
---
 coreutils/date.c                 | 50 +++++++++++++++++---------------
 testsuite/date/date-compat-works |  7 +++++
 2 files changed, 34 insertions(+), 23 deletions(-)
 create mode 100644 testsuite/date/date-compat-works

diff --git a/coreutils/date.c b/coreutils/date.c
index d84e1c31a..cdc5a2d0f 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -224,33 +224,37 @@ int date_main(int argc UNUSED_PARAM, char **argv)
                opt |= OPT_SET;
                date_str = argv[0]; /* can be NULL */
                if (date_str) {
+                       argv++;
+               }
+       }
+
 #if ENABLE_FEATURE_DATE_COMPAT
-                       int len = strspn(date_str, "0123456789");
-                       if (date_str[len] == '\0'
-                        || (date_str[len] == '.'
-                           && isdigit(date_str[len+1])
-                           && isdigit(date_str[len+2])
-                           && date_str[len+3] == '\0'
-                           )
-                       ) {
-                               /* Dreaded MMDDhhmm[[CC]YY][.ss] format!
-                                * It does not match -d or -s format.
-                                * Some users actually do use it.
-                                */
-                               len -= 8;
-                               if (len < 0 || len > 4 || (len & 1))
-                                       
bb_error_msg_and_die(bb_msg_invalid_date, date_str);
-                               if (len != 0) { /* move YY or CCYY to front */
-                                       char buf[4];
-                                       memcpy(buf, date_str + 8, len);
-                                       memmove(date_str + len, date_str, 8);
-                                       memcpy(date_str, buf, len);
-                               }
+       if (date_str) {
+               int len = strspn(date_str, "0123456789");
+               if (date_str[len] == '\0'
+                || (date_str[len] == '.'
+                   && isdigit(date_str[len+1])
+                   && isdigit(date_str[len+2])
+                   && date_str[len+3] == '\0'
+                   )
+               ) {
+                       /* Dreaded MMDDhhmm[[CC]YY][.ss] format!
+                        * It does not match -d or -s format.
+                        * Some users actually do use it.
+                        */
+                       len -= 8;
+                       if (len < 0 || len > 4 || (len & 1))
+                               bb_error_msg_and_die(bb_msg_invalid_date, 
date_str);
+                       if (len != 0) { /* move YY or CCYY to front */
+                               char buf[4];
+                               memcpy(buf, date_str + 8, len);
+                               memmove(date_str + len, date_str, 8);
+                               memcpy(date_str, buf, len);
                        }
-#endif
-                       argv++;
                }
        }
+#endif
+
        if (*argv)
                bb_show_usage();

diff --git a/testsuite/date/date-compat-works b/testsuite/date/date-compat-works
new file mode 100644
index 000000000..c412428e2
--- /dev/null
+++ b/testsuite/date/date-compat-works
@@ -0,0 +1,7 @@
+# FEATURE: CONFIG_FEATURE_DATE_COMPAT
+
+# MMDDhhmm[[CC]YY][.ss]
+test x0 = x`busybox date -u -d 010100001970 +%s`
+test x2147483647 = x`busybox date -u -d 011903142038.07 +%s`
+test x994982399 = x`busybox date -u -d 1231235999.59 +%s`
+test x915148800 = x`busybox date -u -d 0101000099 +%s`
--
2.25.1

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to