diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-01-23 23:37:52 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-01-23 23:37:52 +0100 |
commit | 5da9f96ad85a2d9119d92c7a3d89deca7d904210 (patch) | |
tree | 95b00254246c88708421f611c7da4939508dde29 | |
parent | dfd38a480bfa7e699020dc2608688d85dd90d89b (diff) | |
download | busybox-w32-5da9f96ad85a2d9119d92c7a3d89deca7d904210.tar.gz busybox-w32-5da9f96ad85a2d9119d92c7a3d89deca7d904210.tar.bz2 busybox-w32-5da9f96ad85a2d9119d92c7a3d89deca7d904210.zip |
date: introduce FEATURE_DATE_COMPAT; shrink
function old new delta
date_main 889 862 -27
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/Config.in | 16 | ||||
-rw-r--r-- | coreutils/date.c | 59 |
2 files changed, 41 insertions, 34 deletions
diff --git a/coreutils/Config.in b/coreutils/Config.in index 1e32e2664..fe481ffe1 100644 --- a/coreutils/Config.in +++ b/coreutils/Config.in | |||
@@ -115,6 +115,22 @@ config FEATURE_DATE_ISOFMT | |||
115 | Enable option (-I) to output an ISO-8601 compliant | 115 | Enable option (-I) to output an ISO-8601 compliant |
116 | date/time string. | 116 | date/time string. |
117 | 117 | ||
118 | config FEATURE_DATE_COMPAT | ||
119 | bool "Support weird 'date MMDDhhmm[[YY]YY][.ss]' format" | ||
120 | default y | ||
121 | depends on DATE | ||
122 | help | ||
123 | System time can be set by 'date -s DATE' and simply 'date DATE', | ||
124 | but formats of DATE string are different. 'date DATE' accepts | ||
125 | a rather weird MMDDhhmm[[YY]YY][.ss] format with completely | ||
126 | unnatural placement of year between minutes and seconds. | ||
127 | date -s (and other commands like touch -d) use more sensible | ||
128 | formats (for one, ISO format YYYY-MM-DD hh:mm:ss.ssssss). | ||
129 | |||
130 | With this option off, 'date DATE' is 'date -s DATE' support | ||
131 | the same format. With it on, 'date DATE' additionally supports | ||
132 | MMDDhhmm[[YY]YY][.ss] format. | ||
133 | |||
118 | config DD | 134 | config DD |
119 | bool "dd" | 135 | bool "dd" |
120 | default n | 136 | default n |
diff --git a/coreutils/date.c b/coreutils/date.c index 11b63eaeb..4e5b3b0b8 100644 --- a/coreutils/date.c +++ b/coreutils/date.c | |||
@@ -71,12 +71,12 @@ static void maybe_set_utc(int opt) | |||
71 | 71 | ||
72 | #if ENABLE_LONG_OPTS | 72 | #if ENABLE_LONG_OPTS |
73 | static const char date_longopts[] ALIGN1 = | 73 | static const char date_longopts[] ALIGN1 = |
74 | "rfc-822\0" No_argument "R" | 74 | "rfc-822\0" No_argument "R" |
75 | "rfc-2822\0" No_argument "R" | 75 | "rfc-2822\0" No_argument "R" |
76 | "set\0" Required_argument "s" | 76 | "set\0" Required_argument "s" |
77 | "utc\0" No_argument "u" | 77 | "utc\0" No_argument "u" |
78 | /*"universal\0" No_argument "u"*/ | 78 | /* "universal\0" No_argument "u" */ |
79 | "date\0" Required_argument "d" | 79 | "date\0" Required_argument "d" |
80 | "reference\0" Required_argument "r" | 80 | "reference\0" Required_argument "r" |
81 | ; | 81 | ; |
82 | #endif | 82 | #endif |
@@ -85,6 +85,7 @@ int date_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | |||
85 | int date_main(int argc UNUSED_PARAM, char **argv) | 85 | int date_main(int argc UNUSED_PARAM, char **argv) |
86 | { | 86 | { |
87 | struct tm tm_time; | 87 | struct tm tm_time; |
88 | char buf_fmt_dt2str[64]; | ||
88 | time_t tm; | 89 | time_t tm; |
89 | unsigned opt; | 90 | unsigned opt; |
90 | int ifmt = -1; | 91 | int ifmt = -1; |
@@ -108,8 +109,8 @@ int date_main(int argc UNUSED_PARAM, char **argv) | |||
108 | ifmt = 0; /* default is date */ | 109 | ifmt = 0; /* default is date */ |
109 | if (isofmt_arg) { | 110 | if (isofmt_arg) { |
110 | static const char isoformats[] ALIGN1 = | 111 | static const char isoformats[] ALIGN1 = |
111 | "date\0""hours\0""minutes\0""seconds\0"; | 112 | "date\0""hours\0""minutes\0""seconds\0"; /* ns? */ |
112 | ifmt = index_in_strings(isoformats, isofmt_arg); | 113 | ifmt = index_in_substrings(isoformats, isofmt_arg); |
113 | if (ifmt < 0) | 114 | if (ifmt < 0) |
114 | bb_show_usage(); | 115 | bb_show_usage(); |
115 | } | 116 | } |
@@ -117,14 +118,14 @@ int date_main(int argc UNUSED_PARAM, char **argv) | |||
117 | 118 | ||
118 | fmt_dt2str = NULL; | 119 | fmt_dt2str = NULL; |
119 | if (argv[0] && argv[0][0] == '+') { | 120 | if (argv[0] && argv[0][0] == '+') { |
120 | fmt_dt2str = &argv[0][1]; /* Skip over the '+' */ | 121 | fmt_dt2str = &argv[0][1]; /* skip over the '+' */ |
121 | argv++; | 122 | argv++; |
122 | } | 123 | } |
123 | if (!(opt & (OPT_SET | OPT_DATE))) { | 124 | if (!(opt & (OPT_SET | OPT_DATE))) { |
124 | opt |= OPT_SET; | 125 | opt |= OPT_SET; |
125 | date_str = argv[0]; /* can be NULL */ | 126 | date_str = argv[0]; /* can be NULL */ |
126 | if (date_str) { | 127 | if (date_str) { |
127 | #if ENABLE_DESKTOP | 128 | #if ENABLE_FEATURE_DATE_COMPAT |
128 | int len = strspn(date_str, "0123456789"); | 129 | int len = strspn(date_str, "0123456789"); |
129 | if (date_str[len] == '\0' | 130 | if (date_str[len] == '\0' |
130 | || (date_str[len] == '.' | 131 | || (date_str[len] == '.' |
@@ -133,7 +134,7 @@ int date_main(int argc UNUSED_PARAM, char **argv) | |||
133 | && date_str[len+3] == '\0' | 134 | && date_str[len+3] == '\0' |
134 | ) | 135 | ) |
135 | ) { | 136 | ) { |
136 | /* Dreaded [MMDDhhmm[[CC]YY][.ss]] format! | 137 | /* Dreaded MMDDhhmm[[CC]YY][.ss] format! |
137 | * It does not match -d or -s format. | 138 | * It does not match -d or -s format. |
138 | * Some users actually do use it. | 139 | * Some users actually do use it. |
139 | */ | 140 | */ |
@@ -155,7 +156,7 @@ int date_main(int argc UNUSED_PARAM, char **argv) | |||
155 | bb_show_usage(); | 156 | bb_show_usage(); |
156 | 157 | ||
157 | /* Now we have parsed all the information except the date format | 158 | /* Now we have parsed all the information except the date format |
158 | which depends on whether the clock is being set or read */ | 159 | * which depends on whether the clock is being set or read */ |
159 | 160 | ||
160 | if (opt & OPT_REFERENCE) { | 161 | if (opt & OPT_REFERENCE) { |
161 | struct stat statbuf; | 162 | struct stat statbuf; |
@@ -198,37 +199,28 @@ int date_main(int argc UNUSED_PARAM, char **argv) | |||
198 | /* Deal with format string */ | 199 | /* Deal with format string */ |
199 | if (fmt_dt2str == NULL) { | 200 | if (fmt_dt2str == NULL) { |
200 | int i; | 201 | int i; |
201 | fmt_dt2str = xzalloc(32); | 202 | fmt_dt2str = buf_fmt_dt2str; |
202 | if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) { | 203 | if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) { |
203 | strcpy(fmt_dt2str, "%Y-%m-%d"); | 204 | /* -I[SPEC]: 0:date 1:hours 2:minutes 3:seconds */ |
204 | if (ifmt > 0) { | 205 | strcpy(fmt_dt2str, "%Y-%m-%dT%H:%M:%S"); |
205 | i = 8; | 206 | i = 8 + 3 * ifmt; |
206 | fmt_dt2str[i++] = 'T'; | 207 | if (ifmt != 0) { |
207 | fmt_dt2str[i++] = '%'; | 208 | /* TODO: if (ifmt==4) i += sprintf(&fmt_dt2str[i], ",%09u", nanoseconds); */ |
208 | fmt_dt2str[i++] = 'H'; | ||
209 | if (ifmt > 1) { | ||
210 | fmt_dt2str[i++] = ':'; | ||
211 | fmt_dt2str[i++] = '%'; | ||
212 | fmt_dt2str[i++] = 'M'; | ||
213 | if (ifmt > 2) { | ||
214 | fmt_dt2str[i++] = ':'; | ||
215 | fmt_dt2str[i++] = '%'; | ||
216 | fmt_dt2str[i++] = 'S'; | ||
217 | } | ||
218 | } | ||
219 | format_utc: | 209 | format_utc: |
220 | fmt_dt2str[i++] = '%'; | 210 | fmt_dt2str[i++] = '%'; |
221 | fmt_dt2str[i] = (opt & OPT_UTC) ? 'Z' : 'z'; | 211 | fmt_dt2str[i++] = (opt & OPT_UTC) ? 'Z' : 'z'; |
222 | } | 212 | } |
213 | fmt_dt2str[i] = '\0'; | ||
223 | } else if (opt & OPT_RFC2822) { | 214 | } else if (opt & OPT_RFC2822) { |
224 | /* Undo busybox.c for date -R */ | 215 | /* -R. undo busybox.c setlocale */ |
225 | if (ENABLE_LOCALE_SUPPORT) | 216 | if (ENABLE_LOCALE_SUPPORT) |
226 | setlocale(LC_TIME, "C"); | 217 | setlocale(LC_TIME, "C"); |
227 | strcpy(fmt_dt2str, "%a, %d %b %Y %H:%M:%S "); | 218 | strcpy(fmt_dt2str, "%a, %d %b %Y %H:%M:%S "); |
228 | i = 22; | 219 | i = sizeof("%a, %d %b %Y %H:%M:%S ")-1; |
229 | goto format_utc; | 220 | goto format_utc; |
230 | } else /* default case */ | 221 | } else { /* default case */ |
231 | fmt_dt2str = (char*)"%a %b %e %H:%M:%S %Z %Y"; | 222 | fmt_dt2str = (char*)"%a %b %e %H:%M:%S %Z %Y"; |
223 | } | ||
232 | } | 224 | } |
233 | 225 | ||
234 | #define date_buf bb_common_bufsiz1 | 226 | #define date_buf bb_common_bufsiz1 |
@@ -240,7 +232,6 @@ int date_main(int argc UNUSED_PARAM, char **argv) | |||
240 | if (strncmp(fmt_dt2str, "%f", 2) == 0) { | 232 | if (strncmp(fmt_dt2str, "%f", 2) == 0) { |
241 | fmt_dt2str = (char*)"%Y.%m.%d-%H:%M:%S"; | 233 | fmt_dt2str = (char*)"%Y.%m.%d-%H:%M:%S"; |
242 | } | 234 | } |
243 | |||
244 | /* Generate output string */ | 235 | /* Generate output string */ |
245 | strftime(date_buf, sizeof(date_buf), fmt_dt2str, &tm_time); | 236 | strftime(date_buf, sizeof(date_buf), fmt_dt2str, &tm_time); |
246 | } | 237 | } |