aboutsummaryrefslogtreecommitdiff
path: root/coreutils/date.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-02-21 05:06:42 +0000
committerRob Landley <rob@landley.net>2006-02-21 05:06:42 +0000
commitc5789a6e234c1ddec204cf65f319babb04ca6a47 (patch)
tree02a1388c5a200f02cec6e228204ce64eb939eb30 /coreutils/date.c
parentbf28ed88349b4a04e3434728087619294dfe8e00 (diff)
downloadbusybox-w32-c5789a6e234c1ddec204cf65f319babb04ca6a47.tar.gz
busybox-w32-c5789a6e234c1ddec204cf65f319babb04ca6a47.tar.bz2
busybox-w32-c5789a6e234c1ddec204cf65f319babb04ca6a47.zip
Walter Harms added -D to date, and I did a code cleanup while I was in the
area. Probably broke something...
Diffstat (limited to 'coreutils/date.c')
-rw-r--r--coreutils/date.c126
1 files changed, 41 insertions, 85 deletions
diff --git a/coreutils/date.c b/coreutils/date.c
index 2d411ab31..c111b6161 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -6,20 +6,7 @@
6 * 6 *
7 * iso-format handling added by Robert Griebl <griebl@gmx.de> 7 * iso-format handling added by Robert Griebl <griebl@gmx.de>
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23*/ 10*/
24 11
25#include <stdlib.h> 12#include <stdlib.h>
@@ -31,7 +18,6 @@
31#include <string.h> 18#include <string.h>
32#include "busybox.h" 19#include "busybox.h"
33 20
34
35/* This 'date' command supports only 2 time setting formats, 21/* This 'date' command supports only 2 time setting formats,
36 all the GNU strftime stuff (its in libc, lets use it), 22 all the GNU strftime stuff (its in libc, lets use it),
37 setting time using UTC and displaying int, as well as 23 setting time using UTC and displaying int, as well as
@@ -123,9 +109,8 @@ static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
123#define DATE_OPT_UTC 0x04 109#define DATE_OPT_UTC 0x04
124#define DATE_OPT_DATE 0x08 110#define DATE_OPT_DATE 0x08
125#define DATE_OPT_REFERENCE 0x10 111#define DATE_OPT_REFERENCE 0x10
126#ifdef CONFIG_FEATURE_DATE_ISOFMT 112#define DATE_OPT_TIMESPEC 0x20
127# define DATE_OPT_TIMESPEC 0x20 113#define DATE_OPT_HINT 0x40
128#endif
129 114
130int date_main(int argc, char **argv) 115int date_main(int argc, char **argv)
131{ 116{
@@ -133,63 +118,39 @@ int date_main(int argc, char **argv)
133 char *date_fmt = NULL; 118 char *date_fmt = NULL;
134 int set_time; 119 int set_time;
135 int utc; 120 int utc;
136#if 0
137 int use_arg = 0;
138#endif
139 time_t tm; 121 time_t tm;
140 unsigned long opt; 122 unsigned long opt;
141 struct tm tm_time; 123 struct tm tm_time;
142 char *filename = NULL; 124 char *filename = NULL;
143 125
144#ifdef CONFIG_FEATURE_DATE_ISOFMT
145 int ifmt = 0; 126 int ifmt = 0;
146 char *isofmt_arg; 127 char *isofmt_arg;
128 char *hintfmt_arg;
147 129
148# define GETOPT_ISOFMT "I::"
149#else
150# define GETOPT_ISOFMT
151#endif
152 bb_opt_complementally = "?:d--s:s--d"; 130 bb_opt_complementally = "?:d--s:s--d";
153 opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:" GETOPT_ISOFMT, 131 opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:"
132 USE_FEATURE_DATE_ISOFMT("I::D:"),
154 &date_str, &date_str, &filename 133 &date_str, &date_str, &filename
155#ifdef CONFIG_FEATURE_DATE_ISOFMT 134 USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg));
156 , &isofmt_arg
157#endif
158 );
159 set_time = opt & DATE_OPT_SET; 135 set_time = opt & DATE_OPT_SET;
160 utc = opt & DATE_OPT_UTC; 136 utc = opt & DATE_OPT_UTC;
161 if (utc && putenv("TZ=UTC0") != 0) { 137 if (utc && putenv("TZ=UTC0") != 0) {
162 bb_error_msg_and_die(bb_msg_memory_exhausted); 138 bb_error_msg_and_die(bb_msg_memory_exhausted);
163 } 139 }
164#if 0 140
165 use_arg = opt & DATE_OPT_DATE; 141 if(ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
166#endif
167#ifdef CONFIG_FEATURE_DATE_ISOFMT
168 if(opt & DATE_OPT_TIMESPEC) {
169 if (!isofmt_arg) { 142 if (!isofmt_arg) {
170 ifmt = 1; 143 ifmt = 1;
171 } else { 144 } else {
172 int ifmt_len = bb_strlen(isofmt_arg); 145 char *isoformats[]={"date","hours","minutes","seconds"};
173 146 for(ifmt = 4; ifmt;)
174 if (ifmt_len <= 4 147 if(!strcmp(isofmt_arg,isoformats[--ifmt]))
175 && strncmp(isofmt_arg, "date", ifmt_len) == 0) { 148 break;
176 ifmt = 1;
177 } else if (ifmt_len <= 5
178 && strncmp(isofmt_arg, "hours", ifmt_len) == 0) {
179 ifmt = 2;
180 } else if (ifmt_len <= 7
181 && strncmp(isofmt_arg, "minutes", ifmt_len) == 0) {
182 ifmt = 3;
183 } else if (ifmt_len <= 7
184 && strncmp(isofmt_arg, "seconds", ifmt_len) == 0) {
185 ifmt = 4;
186 }
187 } 149 }
188 if (!ifmt) { 150 if (!ifmt) {
189 bb_show_usage(); 151 bb_show_usage();
190 } 152 }
191 } 153 }
192#endif
193 154
194 /* XXX, date_fmt == NULL from this always */ 155 /* XXX, date_fmt == NULL from this always */
195 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) { 156 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
@@ -216,7 +177,9 @@ int date_main(int argc, char **argv)
216 tm_time.tm_hour = 0; 177 tm_time.tm_hour = 0;
217 178
218 /* Process any date input to UNIX time since 1 Jan 1970 */ 179 /* Process any date input to UNIX time since 1 Jan 1970 */
219 if (strchr(date_str, ':') != NULL) { 180 if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) {
181 strptime(date_str, hintfmt_arg, &tm_time);
182 } else if (strchr(date_str, ':') != NULL) {
220 date_conv_ftime(&tm_time, date_str); 183 date_conv_ftime(&tm_time, date_str);
221 } else { 184 } else {
222 date_conv_time(&tm_time, date_str); 185 date_conv_time(&tm_time, date_str);
@@ -242,49 +205,42 @@ int date_main(int argc, char **argv)
242 205
243 /* Deal with format string */ 206 /* Deal with format string */
244 if (date_fmt == NULL) { 207 if (date_fmt == NULL) {
245#ifdef CONFIG_FEATURE_DATE_ISOFMT 208 /* Start with the default case */
246 switch (ifmt) { 209
247 case 4: 210 date_fmt = (opt & DATE_OPT_RFC2822 ?
248 date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
249 break;
250 case 3:
251 date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
252 break;
253 case 2:
254 date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
255 break;
256 case 1:
257 date_fmt = "%Y-%m-%d";
258 break;
259 case 0:
260 default:
261#endif
262 date_fmt = (opt & DATE_OPT_RFC2822 ?
263 (utc ? "%a, %d %b %Y %H:%M:%S GMT" : 211 (utc ? "%a, %d %b %Y %H:%M:%S GMT" :
264 "%a, %d %b %Y %H:%M:%S %z") : 212 "%a, %d %b %Y %H:%M:%S %z") :
265 "%a %b %e %H:%M:%S %Z %Y"); 213 "%a %b %e %H:%M:%S %Z %Y");
266 214
267#ifdef CONFIG_FEATURE_DATE_ISOFMT 215 if (ENABLE_FEATURE_DATE_ISOFMT) {
268 break; 216 if (ifmt == 4)
217 date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
218 else if (ifmt == 3)
219 date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
220 else if (ifmt == 2)
221 date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
222 else if (ifmt == 1)
223 date_fmt = "%Y-%m-%d";
269 } 224 }
270#endif
271 } else if (*date_fmt == '\0') {
272 /* Imitate what GNU 'date' does with NO format string! */
273 printf("\n");
274 return EXIT_SUCCESS;
275 } 225 }
226
227 if (*date_fmt == '\0') {
276 228
277 /* Handle special conversions */ 229 /* With no format string, just print a blank line */
230
231 *bb_common_bufsiz1=0;
232 } else {
278 233
279 if (strncmp(date_fmt, "%f", 2) == 0) { 234 /* Handle special conversions */
280 date_fmt = "%Y.%m.%d-%H:%M:%S"; 235
281 } 236 if (strncmp(date_fmt, "%f", 2) == 0) {
237 date_fmt = "%Y.%m.%d-%H:%M:%S";
238 }
282 239
283 { 240 /* Generate output string */
284 /* Print OUTPUT (after ALL that!) */
285 strftime(bb_common_bufsiz1, 200, date_fmt, &tm_time); 241 strftime(bb_common_bufsiz1, 200, date_fmt, &tm_time);
286 puts(bb_common_bufsiz1);
287 } 242 }
243 puts(bb_common_bufsiz1);
288 244
289 return EXIT_SUCCESS; 245 return EXIT_SUCCESS;
290} 246}