remove 1 CR, correct strange bb_opt_complementally, add #if 0 and XXX-comment: pleace...
[oweals/busybox.git] / coreutils / date.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini date implementation for busybox
4  *
5  * by Matthew Grant <grantma@anathoth.gen.nz>
6  *
7  * iso-format handling added by Robert Griebl <griebl@gmx.de>
8  *
9  * This program is free software; you can redistribute it and/or modify
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 */
24
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29 #include <time.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include "busybox.h"
33
34
35 /* This 'date' command supports only 2 time setting formats,
36    all the GNU strftime stuff (its in libc, lets use it),
37    setting time using UTC and displaying int, as well as
38    an RFC 822 complient date output for shell scripting
39    mail commands */
40
41 /* Input parsing code is always bulky - used heavy duty libc stuff as
42    much as possible, missed out a lot of bounds checking */
43
44 /* Default input handling to save surprising some people */
45
46 static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
47 {
48         int nr;
49         char *cp;
50
51         nr = sscanf(t_string, "%2d%2d%2d%2d%d", &(tm_time->tm_mon),
52                                 &(tm_time->tm_mday), &(tm_time->tm_hour), &(tm_time->tm_min),
53                                 &(tm_time->tm_year));
54
55         if (nr < 4 || nr > 5) {
56                 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
57         }
58
59         cp = strchr(t_string, '.');
60         if (cp) {
61                 nr = sscanf(cp + 1, "%2d", &(tm_time->tm_sec));
62                 if (nr != 1) {
63                         bb_error_msg_and_die(bb_msg_invalid_date, t_string);
64                 }
65         }
66
67         /* correct for century  - minor Y2K problem here? */
68         if (tm_time->tm_year >= 1900) {
69                 tm_time->tm_year -= 1900;
70         }
71         /* adjust date */
72         tm_time->tm_mon -= 1;
73
74         return (tm_time);
75
76 }
77
78
79 /* The new stuff for LRP */
80
81 static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
82 {
83         struct tm t;
84
85         /* Parse input and assign appropriately to tm_time */
86
87         if (t = *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min,
88                                                  &t.tm_sec) == 3) {
89                 /* no adjustments needed */
90         } else if (t = *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour,
91                                                                 &t.tm_min) == 2) {
92                 /* no adjustments needed */
93         } else if (t = *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon,
94                                                 &t.tm_mday, &t.tm_hour, 
95                                                 &t.tm_min, &t.tm_sec) == 5) {
96                 /* Adjust dates from 1-12 to 0-11 */
97                 t.tm_mon -= 1;
98         } else if (t = *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon,
99                                                 &t.tm_mday, 
100                                                 &t.tm_hour, &t.tm_min) == 4) {
101                 /* Adjust dates from 1-12 to 0-11 */
102                 t.tm_mon -= 1;
103         } else if (t = *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
104                                                 &t.tm_mon, &t.tm_mday, 
105                                                 &t.tm_hour, &t.tm_min,
106                                                         &t.tm_sec) == 6) {
107                 t.tm_year -= 1900;      /* Adjust years */
108                 t.tm_mon -= 1;  /* Adjust dates from 1-12 to 0-11 */
109         } else if (t = *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year,
110                                                 &t.tm_mon, &t.tm_mday, 
111                                                 &t.tm_hour, &t.tm_min) == 5) {
112                 t.tm_year -= 1900;      /* Adjust years */
113                 t.tm_mon -= 1;  /* Adjust dates from 1-12 to 0-11 */
114         } else {
115                 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
116         }
117         *tm_time = t;
118         return (tm_time);
119 }
120
121 #define DATE_OPT_RFC2822        0x01
122 #define DATE_OPT_SET            0x02
123 #define DATE_OPT_UTC            0x04
124 #define DATE_OPT_DATE           0x08
125 #define DATE_OPT_REFERENCE      0x10
126 #ifdef CONFIG_FEATURE_DATE_ISOFMT
127 # define DATE_OPT_TIMESPEC      0x20
128 #endif
129
130 int date_main(int argc, char **argv)
131 {
132         char *date_str = NULL;
133         char *date_fmt = NULL;
134         int set_time;
135         int utc;
136 #if 0
137         int use_arg = 0;
138 #endif
139         time_t tm;
140         unsigned long opt;
141         struct tm tm_time;
142         char *filename = NULL;
143
144 #ifdef CONFIG_FEATURE_DATE_ISOFMT
145         int ifmt = 0;
146         char *isofmt_arg;
147
148 # define GETOPT_ISOFMT  "I::"
149 #else
150 # define GETOPT_ISOFMT
151 #endif
152         bb_opt_complementally = "?d~s:s~d";
153         opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:" GETOPT_ISOFMT,
154                                         &date_str, &date_str, &filename
155 #ifdef CONFIG_FEATURE_DATE_ISOFMT
156                                         , &isofmt_arg
157 #endif
158                                         );
159         set_time = opt & DATE_OPT_SET;
160         utc = opt & DATE_OPT_UTC;
161         if (utc && putenv("TZ=UTC0") != 0) {
162                 bb_error_msg_and_die(bb_msg_memory_exhausted);
163         }
164 #if 0
165         use_arg = opt & DATE_OPT_DATE;
166 #endif
167 #ifdef CONFIG_FEATURE_DATE_ISOFMT
168         if(opt & DATE_OPT_TIMESPEC) {
169                 if (!isofmt_arg) {
170                         ifmt = 1;
171                 } else {
172                         int ifmt_len = bb_strlen(isofmt_arg);
173
174                         if (ifmt_len <= 4
175                                 && strncmp(isofmt_arg, "date", ifmt_len) == 0) {
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                 }
188                 if (!ifmt) {
189                         bb_show_usage();
190                 }
191         }
192 #endif
193
194         /* XXX, date_fmt == NULL from this always */
195         if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
196                 date_fmt = &argv[optind][1];    /* Skip over the '+' */
197         } else if (date_str == NULL) {
198                 set_time = 1;
199                 date_str = argv[optind];
200         }
201
202         /* Now we have parsed all the information except the date format
203            which depends on whether the clock is being set or read */
204
205         if(filename) {
206                 struct stat statbuf;
207                 if(stat(filename,&statbuf))
208                         bb_perror_msg_and_die("File '%s' not found.", filename);
209                 tm=statbuf.st_mtime;
210         } else time(&tm);
211         memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
212         /* Zero out fields - take her back to midnight! */
213         if (date_str != NULL) {
214                 tm_time.tm_sec = 0;
215                 tm_time.tm_min = 0;
216                 tm_time.tm_hour = 0;
217
218                 /* Process any date input to UNIX time since 1 Jan 1970 */
219                 if (strchr(date_str, ':') != NULL) {
220                         date_conv_ftime(&tm_time, date_str);
221                 } else {
222                         date_conv_time(&tm_time, date_str);
223                 }
224
225                 /* Correct any day of week and day of year etc. fields */
226                 tm_time.tm_isdst = -1;  /* Be sure to recheck dst. */
227                 tm = mktime(&tm_time);
228                 if (tm < 0) {
229                         bb_error_msg_and_die(bb_msg_invalid_date, date_str);
230                 }
231                 if (utc && putenv("TZ=UTC0") != 0) {
232                         bb_error_msg_and_die(bb_msg_memory_exhausted);
233                 }
234
235                 /* if setting time, set it */
236                 if (set_time && stime(&tm) < 0) {
237                         bb_perror_msg("cannot set date");
238                 }
239         }
240
241         /* Display output */
242
243         /* Deal with format string */
244         if (date_fmt == NULL) {
245 #ifdef CONFIG_FEATURE_DATE_ISOFMT
246                 switch (ifmt) {
247                 case 4:
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" :
264                                         "%a, %d %b %Y %H:%M:%S %z") :
265                                         "%a %b %e %H:%M:%S %Z %Y");
266
267 #ifdef CONFIG_FEATURE_DATE_ISOFMT
268                         break;
269                 }
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         }
276
277         /* Handle special conversions */
278
279         if (strncmp(date_fmt, "%f", 2) == 0) {
280                 date_fmt = "%Y.%m.%d-%H:%M:%S";
281         }
282
283         {
284                 /* Print OUTPUT (after ALL that!) */
285                 RESERVE_CONFIG_BUFFER(t_buff, 201);
286                 strftime(t_buff, 200, date_fmt, &tm_time);
287                 puts(t_buff);
288                 RELEASE_CONFIG_BUFFER(t_buff);
289         }
290
291         return EXIT_SUCCESS;
292 }