Woops, im getting ahead of myself, we dont have the -r (refernece)
[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 <getopt.h>
33 #include "busybox.h"
34
35
36 /* This 'date' command supports only 2 time setting formats, 
37    all the GNU strftime stuff (its in libc, lets use it),
38    setting time using UTC and displaying int, as well as
39    an RFC 822 complient date output for shell scripting
40    mail commands */
41
42 /* Input parsing code is always bulky - used heavy duty libc stuff as
43    much as possible, missed out a lot of bounds checking */
44
45 /* Default input handling to save suprising some people */
46
47 static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
48 {
49         int nr;
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         /* correct for century  - minor Y2K problem here? */
60         if (tm_time->tm_year >= 1900) {
61                 tm_time->tm_year -= 1900;
62         }
63         /* adjust date */
64         tm_time->tm_mon -= 1;
65
66         return (tm_time);
67
68 }
69
70
71 /* The new stuff for LRP */
72
73 static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
74 {
75         struct tm t;
76
77         /* Parse input and assign appropriately to tm_time */
78
79         if (t =
80                 *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min,
81                                                  &t.tm_sec) == 3) {
82                 /* no adjustments needed */
83         } else if (t =
84                            *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour,
85                                                                 &t.tm_min) == 2) {
86                 /* no adjustments needed */
87         } else if (t =
88                            *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon,
89                                                                 &t.tm_mday, &t.tm_hour, &t.tm_min,
90                                                                 &t.tm_sec) == 5) {
91                 /* Adjust dates from 1-12 to 0-11 */
92                 t.tm_mon -= 1;
93         } else if (t =
94                            *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon,
95                                                                 &t.tm_mday, &t.tm_hour, &t.tm_min) == 4) {
96                 /* Adjust dates from 1-12 to 0-11 */
97                 t.tm_mon -= 1;
98         } else if (t =
99                            *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
100                                                                 &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min,
101                                                                 &t.tm_sec) == 6) {
102                 t.tm_year -= 1900;      /* Adjust years */
103                 t.tm_mon -= 1;  /* Adjust dates from 1-12 to 0-11 */
104         } else if (t =
105                            *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year,
106                                                                 &t.tm_mon, &t.tm_mday, &t.tm_hour,
107                                                                 &t.tm_min) == 5) {
108                 t.tm_year -= 1900;      /* Adjust years */
109                 t.tm_mon -= 1;  /* Adjust dates from 1-12 to 0-11 */
110         } else {
111                 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
112         }
113         *tm_time = t;
114         return (tm_time);
115 }
116
117 #define DATE_OPT_RFC2822        0x01
118 #define DATE_OPT_SET            0x02
119 #define DATE_OPT_UTC            0x04
120 #define DATE_OPT_DATE           0x08
121 #ifdef CONFIG_FEATURE_DATE_ISOFMT
122 # define DATE_OPT_TIMESPEC      0x10
123 #endif
124
125 int date_main(int argc, char **argv)
126 {
127         char *date_str = NULL;
128         char *date_fmt = NULL;
129         char *t_buff;
130         int set_time;
131         int utc;
132         int use_arg = 0;
133         time_t tm;
134         unsigned long opt;
135         struct tm tm_time;
136
137 #ifdef CONFIG_FEATURE_DATE_ISOFMT
138         int ifmt = 0;
139         char *isofmt_arg;
140
141 # define GETOPT_ISOFMT  "I::"
142 #else
143 # define GETOPT_ISOFMT
144 #endif
145         bb_opt_complementaly = "d~ds:s~ds";
146         opt = bb_getopt_ulflags(argc, argv, "Rs:ud:" GETOPT_ISOFMT,
147                                         &date_str, &date_str
148 #ifdef CONFIG_FEATURE_DATE_ISOFMT
149                                         , &isofmt_arg
150 #endif
151                                         );
152         set_time = opt & DATE_OPT_SET;
153         utc = opt & DATE_OPT_UTC;
154         if ((utc) && (putenv("TZ=UTC0") != 0)) {
155                 bb_error_msg_and_die(bb_msg_memory_exhausted);
156         }
157         use_arg = opt & DATE_OPT_DATE;
158         if(opt & 0x80000000UL)
159                 bb_show_usage();
160 #ifdef CONFIG_FEATURE_DATE_ISOFMT
161         if(opt & DATE_OPT_TIMESPEC) {
162                 if (!isofmt_arg) {
163                         ifmt = 1;
164                 } else {
165                         int ifmt_len = bb_strlen(isofmt_arg);
166
167                         if ((ifmt_len <= 4)
168                                 && (strncmp(isofmt_arg, "date", ifmt_len) == 0)) {
169                                 ifmt = 1;
170                         } else if ((ifmt_len <= 5)
171                                    && (strncmp(isofmt_arg, "hours", ifmt_len) == 0)) {
172                                 ifmt = 2;
173                         } else if ((ifmt_len <= 7)
174                                    && (strncmp(isofmt_arg, "minutes", ifmt_len) == 0)) {
175                                 ifmt = 3;
176                         } else if ((ifmt_len <= 7)
177                                    && (strncmp(isofmt_arg, "seconds", ifmt_len) == 0)) {
178                                 ifmt = 4;
179                         }
180                 }
181                 if (!ifmt) {
182                         bb_show_usage();
183                 }
184         }
185 #endif
186
187         if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
188                 date_fmt = &argv[optind][1];    /* Skip over the '+' */
189         } else if (date_str == NULL) {
190                 set_time = 1;
191                 date_str = argv[optind];
192         }
193
194         /* Now we have parsed all the information except the date format
195            which depends on whether the clock is being set or read */
196
197         time(&tm);
198         memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
199         /* Zero out fields - take her back to midnight! */
200         if (date_str != NULL) {
201                 tm_time.tm_sec = 0;
202                 tm_time.tm_min = 0;
203                 tm_time.tm_hour = 0;
204
205                 /* Process any date input to UNIX time since 1 Jan 1970 */
206                 if (strchr(date_str, ':') != NULL) {
207                         date_conv_ftime(&tm_time, date_str);
208                 } else {
209                         date_conv_time(&tm_time, date_str);
210                 }
211
212                 /* Correct any day of week and day of year etc. fields */
213                 tm_time.tm_isdst = -1;  /* Be sure to recheck dst. */
214                 tm = mktime(&tm_time);
215                 if (tm < 0) {
216                         bb_error_msg_and_die(bb_msg_invalid_date, date_str);
217                 }
218                 if (utc && (putenv("TZ=UTC0") != 0)) {
219                         bb_error_msg_and_die(bb_msg_memory_exhausted);
220                 }
221
222                 /* if setting time, set it */
223                 if (set_time && (stime(&tm) < 0)) {
224                         bb_perror_msg("cannot set date");
225                 }
226         }
227
228         /* Display output */
229
230         /* Deal with format string */
231         if (date_fmt == NULL) {
232 #ifdef CONFIG_FEATURE_DATE_ISOFMT
233                 switch (ifmt) {
234                 case 4:
235                         date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
236                         break;
237                 case 3:
238                         date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
239                         break;
240                 case 2:
241                         date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
242                         break;
243                 case 1:
244                         date_fmt = "%Y-%m-%d";
245                         break;
246                 case 0:
247                 default:
248 #endif
249                         date_fmt =
250                                 (opt & DATE_OPT_RFC2822
251                                  ? (utc ? "%a, %e %b %Y %H:%M:%S GMT" :
252                                         "%a, %e %b %Y %H:%M:%S %z") : "%a %b %e %H:%M:%S %Z %Y");
253
254 #ifdef CONFIG_FEATURE_DATE_ISOFMT
255                         break;
256                 }
257 #endif
258         } else if (*date_fmt == '\0') {
259                 /* Imitate what GNU 'date' does with NO format string! */
260                 printf("\n");
261                 return EXIT_SUCCESS;
262         }
263
264         /* Handle special conversions */
265
266         if (strncmp(date_fmt, "%f", 2) == 0) {
267                 date_fmt = "%Y.%m.%d-%H:%M:%S";
268         }
269
270         /* Print OUTPUT (after ALL that!) */
271         t_buff = xmalloc(201);
272         strftime(t_buff, 200, date_fmt, &tm_time);
273         puts(t_buff);
274
275         return EXIT_SUCCESS;
276 }