diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-04-25 02:14:07 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-04-25 02:14:07 +0000 |
commit | 4d89a8bd1d6a081be559656b67f4adfc75137740 (patch) | |
tree | 945ac3875df35b589450354fdece8007407cf509 /coreutils/date.c | |
parent | ad6cab18341dc2df832f0b8fea966e3fb57eef03 (diff) | |
download | busybox-w32-4d89a8bd1d6a081be559656b67f4adfc75137740.tar.gz busybox-w32-4d89a8bd1d6a081be559656b67f4adfc75137740.tar.bz2 busybox-w32-4d89a8bd1d6a081be559656b67f4adfc75137740.zip |
date: make it accept ISO date format. Fix help text.
make testsuite actually test something useful.
function old new delta
date_main 1094 1149 +55
Diffstat (limited to 'coreutils/date.c')
-rw-r--r-- | coreutils/date.c | 104 |
1 files changed, 52 insertions, 52 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); |