diff options
-rw-r--r-- | coreutils/date.c | 104 | ||||
-rw-r--r-- | include/usage.h | 3 | ||||
-rw-r--r-- | testsuite/date/date-works | 45 |
3 files changed, 98 insertions, 54 deletions
diff --git a/coreutils/date.c b/coreutils/date.c index 064f758e2..2aecbaac2 100644 --- a/coreutils/date.c +++ b/coreutils/date.c | |||
@@ -103,67 +103,67 @@ int date_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
103 | if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) { | 103 | if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) { |
104 | if (strptime(date_str, fmt_str2dt, &tm_time) == NULL) | 104 | if (strptime(date_str, fmt_str2dt, &tm_time) == NULL) |
105 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); | 105 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
106 | } else if (strchr(date_str, ':') != NULL) { | 106 | } else { |
107 | /* Parse input and assign appropriately to tm_time */ | 107 | char end = '\0'; |
108 | 108 | const char *last_colon = strrchr(date_str, ':'); | |
109 | if (sscanf(date_str, "%d:%d:%d", &tm_time.tm_hour, &tm_time.tm_min, | 109 | |
110 | &tm_time.tm_sec) == 3) { | 110 | if (last_colon != NULL) { |
111 | /* no adjustments needed */ | 111 | /* Parse input and assign appropriately to tm_time */ |
112 | } else if (sscanf(date_str, "%d:%d", &tm_time.tm_hour, | 112 | |
113 | &tm_time.tm_min) == 2) { | 113 | if (sscanf(date_str, "%u:%u%c", |
114 | /* no adjustments needed */ | 114 | &tm_time.tm_hour, |
115 | } else if (sscanf(date_str, "%d.%d-%d:%d:%d", &tm_time.tm_mon, | 115 | &tm_time.tm_min, |
116 | &tm_time.tm_mday, &tm_time.tm_hour, | 116 | &end) >= 2) { |
117 | &tm_time.tm_min, &tm_time.tm_sec) == 5) { | 117 | /* no adjustments needed */ |
118 | /* Adjust dates from 1-12 to 0-11 */ | 118 | } else if (sscanf(date_str, "%u.%u-%u:%u%c", |
119 | tm_time.tm_mon -= 1; | ||
120 | } else if (sscanf(date_str, "%d.%d-%d:%d", &tm_time.tm_mon, | ||
121 | &tm_time.tm_mday, | ||
122 | &tm_time.tm_hour, &tm_time.tm_min) == 4) { | ||
123 | /* Adjust dates from 1-12 to 0-11 */ | ||
124 | tm_time.tm_mon -= 1; | ||
125 | } else if (sscanf(date_str, "%d.%d.%d-%d:%d:%d", &tm_time.tm_year, | ||
126 | &tm_time.tm_mon, &tm_time.tm_mday, | 119 | &tm_time.tm_mon, &tm_time.tm_mday, |
127 | &tm_time.tm_hour, &tm_time.tm_min, | 120 | &tm_time.tm_hour, &tm_time.tm_min, |
128 | &tm_time.tm_sec) == 6) { | 121 | &end) >= 4) { |
129 | tm_time.tm_year -= 1900; /* Adjust years */ | 122 | /* Adjust dates from 1-12 to 0-11 */ |
130 | tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ | 123 | tm_time.tm_mon -= 1; |
131 | } else if (sscanf(date_str, "%d.%d.%d-%d:%d", &tm_time.tm_year, | 124 | } else if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &tm_time.tm_year, |
132 | &tm_time.tm_mon, &tm_time.tm_mday, | 125 | &tm_time.tm_mon, &tm_time.tm_mday, |
133 | &tm_time.tm_hour, &tm_time.tm_min) == 5) { | 126 | &tm_time.tm_hour, &tm_time.tm_min, |
134 | tm_time.tm_year -= 1900; /* Adjust years */ | 127 | &end) >= 5) { |
135 | tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ | 128 | tm_time.tm_year -= 1900; /* Adjust years */ |
129 | tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ | ||
130 | } else if (sscanf(date_str, "%u-%u-%u %u:%u%c", &tm_time.tm_year, | ||
131 | &tm_time.tm_mon, &tm_time.tm_mday, | ||
132 | &tm_time.tm_hour, &tm_time.tm_min, | ||
133 | &end) >= 5) { | ||
134 | tm_time.tm_year -= 1900; /* Adjust years */ | ||
135 | tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ | ||
136 | //TODO: coreutils 6.9 also accepts "YYYY-MM-DD HH" (no minutes) | ||
137 | } else { | ||
138 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); | ||
139 | } | ||
140 | if (end == ':') { | ||
141 | if (sscanf(last_colon + 1, "%u%c", &tm_time.tm_sec, &end) == 1) | ||
142 | end = '\0'; | ||
143 | /* else end != NUL and we error out */ | ||
144 | } | ||
136 | } else { | 145 | } else { |
137 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); | 146 | if (sscanf(date_str, "%2u%2u%2u%2u%u%c", &tm_time.tm_mon, |
138 | } | 147 | &tm_time.tm_mday, &tm_time.tm_hour, &tm_time.tm_min, |
139 | } else { | 148 | &tm_time.tm_year, &end) < 4) |
140 | int nr; | ||
141 | char *cp; | ||
142 | |||
143 | nr = sscanf(date_str, "%2d%2d%2d%2d%d", &tm_time.tm_mon, | ||
144 | &tm_time.tm_mday, &tm_time.tm_hour, &tm_time.tm_min, | ||
145 | &tm_time.tm_year); | ||
146 | |||
147 | if (nr < 4 || nr > 5) { | ||
148 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); | ||
149 | } | ||
150 | |||
151 | cp = strchr(date_str, '.'); | ||
152 | if (cp) { | ||
153 | nr = sscanf(cp + 1, "%2d", &tm_time.tm_sec); | ||
154 | if (nr != 1) { | ||
155 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); | 149 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
150 | /* correct for century - minor Y2K problem here? */ | ||
151 | if (tm_time.tm_year >= 1900) { | ||
152 | tm_time.tm_year -= 1900; | ||
153 | } | ||
154 | /* adjust date */ | ||
155 | tm_time.tm_mon -= 1; | ||
156 | if (end == '.') { | ||
157 | if (sscanf(strchr(date_str, '.') + 1, "%u%c", | ||
158 | &tm_time.tm_sec, &end) == 1) | ||
159 | end = '\0'; | ||
160 | /* else end != NUL and we error out */ | ||
156 | } | 161 | } |
157 | } | 162 | } |
158 | 163 | if (end != '\0') { | |
159 | /* correct for century - minor Y2K problem here? */ | 164 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
160 | if (tm_time.tm_year >= 1900) { | ||
161 | tm_time.tm_year -= 1900; | ||
162 | } | 165 | } |
163 | /* adjust date */ | ||
164 | tm_time.tm_mon -= 1; | ||
165 | } | 166 | } |
166 | |||
167 | /* Correct any day of week and day of year etc. fields */ | 167 | /* Correct any day of week and day of year etc. fields */ |
168 | tm_time.tm_isdst = -1; /* Be sure to recheck dst. */ | 168 | tm_time.tm_isdst = -1; /* Be sure to recheck dst. */ |
169 | tm = mktime(&tm_time); | 169 | tm = mktime(&tm_time); |
diff --git a/include/usage.h b/include/usage.h index acc72d158..cbc5cb0a2 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -612,8 +612,9 @@ | |||
612 | ) \ | 612 | ) \ |
613 | "\n" \ | 613 | "\n" \ |
614 | "\nRecognized formats for TIME:" \ | 614 | "\nRecognized formats for TIME:" \ |
615 | "\n [hh:]mm:ss" \ | 615 | "\n hh:mm[:ss]" \ |
616 | "\n [YYYY.]MM.DD-hh:mm[:ss]" \ | 616 | "\n [YYYY.]MM.DD-hh:mm[:ss]" \ |
617 | "\n YYYY-MM-DD hh:mm[:ss]" \ | ||
617 | "\n MMDDhhmm[[YY]YY][.ss]" \ | 618 | "\n MMDDhhmm[[YY]YY][.ss]" \ |
618 | 619 | ||
619 | #define date_example_usage \ | 620 | #define date_example_usage \ |
diff --git a/testsuite/date/date-works b/testsuite/date/date-works index 3ecaeea63..f726bab3a 100644 --- a/testsuite/date/date-works +++ b/testsuite/date/date-works | |||
@@ -1 +1,44 @@ | |||
1 | test x"`date`" = x"`busybox date`" | 1 | dt=`busybox date` |
2 | # Expected format: Fri Apr 25 03:47:55 CEST 2008 | ||
3 | dt=`echo "$dt" | sed 's/^[^ ][^ ][^ ] [^ ][^ ][^ ] [ 0123][0-9] [012][0-9]:[0-5][0-9]:[0-6][0-9] [A-Z][A-Z]* [012][0-9][0-9][0-9]$/OK/'` | ||
4 | test x"$dt" = x"OK" | ||
5 | |||
6 | dt=`busybox date -d 1:2` | ||
7 | dt=`echo "$dt" | cut -b1-19` | ||
8 | test x"$dt" = x"Fri Apr 25 01:02:00" | ||
9 | |||
10 | dt=`busybox date -d 1:2:3` | ||
11 | dt=`echo "$dt" | cut -b1-19` | ||
12 | test x"$dt" = x"Fri Apr 25 01:02:03" | ||
13 | |||
14 | dt=`busybox date -d 1.2-3:4` | ||
15 | dt=`echo "$dt" | cut -b1-19` | ||
16 | test x"$dt" = x"Wed Jan 2 03:04:00" | ||
17 | |||
18 | dt=`busybox date -d 1.2-3:4:5` | ||
19 | dt=`echo "$dt" | cut -b1-19` | ||
20 | test x"$dt" = x"Wed Jan 2 03:04:05" | ||
21 | |||
22 | dt=`busybox date -d 1999.1.2-3:4` | ||
23 | dt=`echo "$dt" | cut -b1-19` | ||
24 | test x"$dt" = x"Sat Jan 2 03:04:00" | ||
25 | |||
26 | dt=`busybox date -d 1999.1.2-3:4:5` | ||
27 | dt=`echo "$dt" | cut -b1-19` | ||
28 | test x"$dt" = x"Sat Jan 2 03:04:05" | ||
29 | |||
30 | dt=`busybox date -d '1999-1-2 3:4:5'` | ||
31 | dt=`echo "$dt" | cut -b1-19` | ||
32 | test x"$dt" = x"Sat Jan 2 03:04:05" | ||
33 | |||
34 | dt=`busybox date -d 01231133` | ||
35 | dt=`echo "$dt" | cut -b1-19` | ||
36 | test x"$dt" = x"Wed Jan 23 11:33:00" | ||
37 | |||
38 | dt=`busybox date -d 012311332000` | ||
39 | dt=`echo "$dt" | cut -b1-19` | ||
40 | test x"$dt" = x"Sun Jan 23 11:33:00" | ||
41 | |||
42 | dt=`busybox date -d 012311332000.30` | ||
43 | dt=`echo "$dt" | cut -b1-19` | ||
44 | test x"$dt" = x"Sun Jan 23 11:33:30" | ||