OSDN Git Service

Fix year parsing in date(1).
authorElliott Hughes <enh@google.com>
Wed, 11 Nov 2015 02:21:51 +0000 (18:21 -0800)
committerRob Landley <rob@landley.net>
Fri, 13 Nov 2015 03:26:56 +0000 (21:26 -0600)
Four-digit years were being mangled by the code for two-digit years.
Move all the two-digit year code into the "we only saw two digits" case.

Add some new tests and fix existing tests.

tests/date.test
toys/posix/date.c

index 94a4157..62c0ed6 100644 (file)
@@ -14,9 +14,13 @@ testing "date -d 06021234" "TZ=UTC date -d 06021234 2>&1" "Sun Jun  2 12:34:00 U
 testing "date -d 060212341982" "TZ=UTC date -d 060212341982 2>&1" "Sun Jun  2 12:34:00 UTC 1982\n" "" ""
 testing "date -d 123" "TZ=UTC date -d 123 2>&1" "date: bad date '123'\n" "" ""
 
+# Test parsing 2- and 4-digit years.
+testing "date -d 1110143115.30" "TZ=UTC date -d 1110143115.30 2>&1" "Sun Nov 10 14:31:30 UTC 1915\n" "" ""
+testing "date -d 111014312015.30" "TZ=UTC date -d 111014312015.30 2>&1" "Sun Nov 10 14:31:30 UTC 2015\n" "" ""
+
 # Accidentally given a Unix time, we should trivially reject that.
 testing "date Unix time missing @" "TZ=UTC date 1438053157 2>&1" \
-  "date: bad date '1438053157'; Tue February 38 05:31:00 UTC 2057 != Sun Mar 10 05:31:00 UTC 2058\n" "" ""
+  "date: bad date '1438053157'; Wed February 38 05:31:00 UTC 2057 != Sun Mar 10 05:31:00 UTC 2058\n" "" ""
 # But some invalid dates are more subtle, like Febuary 29th in a non-leap year.
 testing "date Feb 29th" "TZ=UTC date 022900001975 2>&1" \
-  "date: bad date '022900001975'; Tue Feb 29 00:00:00 UTC 2075 != Fri Mar  1 00:00:00 UTC 2075\n" "" ""
+  "date: bad date '022900001975'; Wed Feb 29 00:00:00 UTC 1975 != Sat Mar  1 00:00:00 UTC 1975\n" "" ""
index a42de50..398b891 100644 (file)
@@ -131,12 +131,11 @@ static int parse_default(char *str, struct tm *tm)
 
   // If year specified, overwrite one we fetched earlier
   if (*str && *str != '.') {
-    unsigned year, r1 = tm->tm_year % 100, r2 = (tm->tm_year + 50) % 100,
-      century = tm->tm_year - r1;
+    unsigned year;
 
     len = 0;
     sscanf(str, "%u%n", &year, &len);
-    if (len == 4) year -= 1900;
+    if (len == 4) tm->tm_year = year - 1900;
     else if (len != 2) return 1;
     str += len;
 
@@ -144,11 +143,14 @@ static int parse_default(char *str, struct tm *tm)
     // A "future" date in past is a century ahead.
     // A non-future date in the future is a century behind.
     if (len == 2) {
+      unsigned r1 = tm->tm_year % 100, r2 = (tm->tm_year + 50) % 100,
+        century = tm->tm_year - r1;
+
       if ((r1 < r2) ? (r1 < year && year < r2) : (year < r1 || year > r2)) {
         if (year < r1) year += 100;
       } else if (year > r1) year -= 100;
+      tm->tm_year = year + century;
     }
-    tm->tm_year = year + century;
   }
   if (*str == '.') {
     len = 0;