3ede1237ca84ec4891152df6e58562f22abc8bca
[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  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21 */
22
23 #include "internal.h"
24 #define BB_DECLARE_EXTERN
25 #define bb_need_invalid_date
26 #define bb_need_memory_exhausted
27 #include "messages.c"
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <sys/time.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include <stdio.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 static const char date_usage[] = "date [OPTION]... [+FORMAT]\n"
43         "  or:  date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n"
44 #ifndef BB_FEATURE_TRIVIAL_HELP
45         "\nDisplays the current time in the given FORMAT, or sets the system date.\n"
46         "\nOptions:\n\t-R\tOutputs RFC-822 compliant date string\n"
47         "\t-s\tSets time described by STRING\n"
48         "\t-u\tPrints or sets Coordinated Universal Time\n"
49 #endif
50         ;
51
52
53 /* Input parsing code is always bulky - used heavy duty libc stuff as
54    much as possible, missed out a lot of bounds checking */
55
56 /* Default input handling to save suprising some people */
57
58 struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
59 {
60         int nr;
61
62         nr = sscanf(t_string, "%2d%2d%2d%2d%d",
63                                 &(tm_time->tm_mon),
64                                 &(tm_time->tm_mday),
65                                 &(tm_time->tm_hour),
66                                 &(tm_time->tm_min), &(tm_time->tm_year));
67
68         if (nr < 4 || nr > 5) {
69                 fatalError(invalid_date, t_string); 
70         }
71
72         /* correct for century  - minor Y2K problem here? */
73         if (tm_time->tm_year >= 1900)
74                 tm_time->tm_year -= 1900;
75         /* adjust date */
76         tm_time->tm_mon -= 1;
77
78         return (tm_time);
79
80 }
81
82
83 /* The new stuff for LRP */
84
85 struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
86 {
87         struct tm itm_time, jtm_time, ktm_time, ltm_time, mtm_time, ntm_time;
88
89         itm_time = *tm_time;
90         jtm_time = *tm_time;
91         ktm_time = *tm_time;
92         ltm_time = *tm_time;
93         mtm_time = *tm_time;
94         ntm_time = *tm_time;
95
96         /* Parse input and assign appropriately to tm_time */
97
98         if (sscanf(t_string, "%d:%d:%d",
99                            &itm_time.tm_hour, &itm_time.tm_min, &itm_time.tm_sec) == 3) {
100
101                 *tm_time = itm_time;
102                 return (tm_time);
103
104         } else if (sscanf(t_string, "%d:%d",
105                                           &jtm_time.tm_hour, &jtm_time.tm_min) == 2) {
106
107                 *tm_time = jtm_time;
108                 return (tm_time);
109
110         } else if (sscanf(t_string, "%d.%d-%d:%d:%d",
111                                           &ktm_time.tm_mon,
112                                           &ktm_time.tm_mday,
113                                           &ktm_time.tm_hour,
114                                           &ktm_time.tm_min, &ktm_time.tm_sec) == 5) {
115
116                 ktm_time.tm_mon -= 1;   /* Adjust dates from 1-12 to 0-11 */
117                 *tm_time = ktm_time;
118                 return (tm_time);
119
120         } else if (sscanf(t_string, "%d.%d-%d:%d",
121                                           &ltm_time.tm_mon,
122                                           &ltm_time.tm_mday,
123                                           &ltm_time.tm_hour, &ltm_time.tm_min) == 4) {
124
125                 ltm_time.tm_mon -= 1;   /* Adjust dates from 1-12 to 0-11 */
126                 *tm_time = ltm_time;
127                 return (tm_time);
128
129         } else if (sscanf(t_string, "%d.%d.%d-%d:%d:%d",
130                                           &mtm_time.tm_year,
131                                           &mtm_time.tm_mon,
132                                           &mtm_time.tm_mday,
133                                           &mtm_time.tm_hour,
134                                           &mtm_time.tm_min, &mtm_time.tm_sec) == 6) {
135
136                 mtm_time.tm_year -= 1900;       /* Adjust years */
137                 mtm_time.tm_mon -= 1;   /* Adjust dates from 1-12 to 0-11 */
138                 *tm_time = mtm_time;
139                 return (tm_time);
140
141         } else if (sscanf(t_string, "%d.%d.%d-%d:%d",
142                                           &ntm_time.tm_year,
143                                           &ntm_time.tm_mon,
144                                           &ntm_time.tm_mday,
145                                           &ntm_time.tm_hour, &ntm_time.tm_min) == 5) {
146                 ntm_time.tm_year -= 1900;       /* Adjust years */
147                 ntm_time.tm_mon -= 1;   /* Adjust dates from 1-12 to 0-11 */
148                 *tm_time = ntm_time;
149                 return (tm_time);
150
151         }
152
153         fatalError(invalid_date, t_string); 
154 }
155
156
157 int date_main(int argc, char **argv)
158 {
159         char *date_str = NULL;
160         char *date_fmt = NULL;
161         char *t_buff;
162         char c;
163         int set_time = 0;
164         int rfc822 = 0;
165         int utc = 0;
166         int use_arg = 0;
167         time_t tm;
168         struct tm tm_time;
169
170         /* Interpret command line args */
171         while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
172                 switch (c) {
173                 case 'R':
174                         rfc822 = 1;
175                         break;
176                 case 's':
177                         set_time = 1;
178                         if ((date_str != NULL) || ((date_str = optarg) == NULL))
179                                 usage(date_usage);
180                         break;
181                 case 'u':
182                         utc = 1;
183                         if (putenv("TZ=UTC0") != 0) 
184                                 fatalError(memory_exhausted);
185                         break;
186                 case 'd':
187                         use_arg = 1;
188                         if ((date_str != NULL) || ((date_str = optarg) == NULL))
189                                 usage(date_usage);
190                         break;
191                 default:
192                         usage(date_usage);
193                 }
194         }
195
196         if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
197                 date_fmt = &argv[optind][1];   /* Skip over the '+' */
198         else if (date_str == NULL) {
199                 set_time = 1;
200                 date_str = argv[optind];
201         } else {
202                 usage(date_usage);
203         }
204
205         /* Now we have parsed all the information except the date format
206            which depends on whether the clock is being set or read */
207
208         time(&tm);
209         memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
210         /* Zero out fields - take her back to midnight! */
211         if (date_str != NULL) {
212                 tm_time.tm_sec = 0;
213                 tm_time.tm_min = 0;
214                 tm_time.tm_hour = 0;
215         }
216
217         /* Process any date input to UNIX time since 1 Jan 1970 */
218         if (date_str != NULL) {
219
220                 if (strchr(date_str, ':') != NULL) {
221                         date_conv_ftime(&tm_time, date_str);
222                 } else {
223                         date_conv_time(&tm_time, date_str);
224                 }
225
226                 /* Correct any day of week and day of year etc fields */
227                 tm = mktime(&tm_time);
228                 if (tm < 0)
229                         fatalError(invalid_date, date_str); 
230
231                 /* if setting time, set it */
232                 if (set_time) {
233                         if (stime(&tm) < 0) {
234                                 fatalError("can't set date.\n");
235                         }
236                 }
237         }
238
239         /* Display output */
240
241         /* Deal with format string */
242         if (date_fmt == NULL) {
243                 date_fmt = (rfc822
244                                         ? (utc
245                                            ? "%a, %_d %b %Y %H:%M:%S GMT"
246                                            : "%a, %_d %b %Y %H:%M:%S %z")
247                                         : "%a %b %e %H:%M:%S %Z %Y");
248
249         } else if (*date_fmt == '\0') {
250                 /* Imitate what GNU 'date' does with NO format string! */
251                 printf("\n");
252                 exit(TRUE);
253         }
254
255         /* Handle special conversions */
256
257         if (strncmp(date_fmt, "%f", 2) == 0) {
258                 date_fmt = "%Y.%m.%d-%H:%M:%S";
259         }
260
261         /* Print OUTPUT (after ALL that!) */
262         t_buff = xmalloc(201);
263         strftime(t_buff, 200, date_fmt, &tm_time);
264         printf("%s\n", t_buff);
265
266         return(TRUE);
267 }