Renamed "internal.h" to the more sensible "busybox.h".
[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 "busybox.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 #include <getopt.h>
35
36
37 /* This 'date' command supports only 2 time setting formats, 
38    all the GNU strftime stuff (its in libc, lets use it),
39    setting time using UTC and displaying int, as well as
40    an RFC 822 complient date output for shell scripting
41    mail commands */
42
43 /* Input parsing code is always bulky - used heavy duty libc stuff as
44    much as possible, missed out a lot of bounds checking */
45
46 /* Default input handling to save suprising some people */
47
48 struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
49 {
50         int nr;
51
52         nr = sscanf(t_string, "%2d%2d%2d%2d%d",
53                                 &(tm_time->tm_mon),
54                                 &(tm_time->tm_mday),
55                                 &(tm_time->tm_hour),
56                                 &(tm_time->tm_min), &(tm_time->tm_year));
57
58         if (nr < 4 || nr > 5) {
59                 fatalError(invalid_date, t_string); 
60         }
61
62         /* correct for century  - minor Y2K problem here? */
63         if (tm_time->tm_year >= 1900)
64                 tm_time->tm_year -= 1900;
65         /* adjust date */
66         tm_time->tm_mon -= 1;
67
68         return (tm_time);
69
70 }
71
72
73 /* The new stuff for LRP */
74
75 struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
76 {
77         struct tm itm_time, jtm_time, ktm_time, ltm_time, mtm_time, ntm_time;
78
79         itm_time = *tm_time;
80         jtm_time = *tm_time;
81         ktm_time = *tm_time;
82         ltm_time = *tm_time;
83         mtm_time = *tm_time;
84         ntm_time = *tm_time;
85
86         /* Parse input and assign appropriately to tm_time */
87
88         if (sscanf(t_string, "%d:%d:%d",
89                            &itm_time.tm_hour, &itm_time.tm_min, &itm_time.tm_sec) == 3) {
90
91                 *tm_time = itm_time;
92                 return (tm_time);
93
94         } else if (sscanf(t_string, "%d:%d",
95                                           &jtm_time.tm_hour, &jtm_time.tm_min) == 2) {
96
97                 *tm_time = jtm_time;
98                 return (tm_time);
99
100         } else if (sscanf(t_string, "%d.%d-%d:%d:%d",
101                                           &ktm_time.tm_mon,
102                                           &ktm_time.tm_mday,
103                                           &ktm_time.tm_hour,
104                                           &ktm_time.tm_min, &ktm_time.tm_sec) == 5) {
105
106                 ktm_time.tm_mon -= 1;   /* Adjust dates from 1-12 to 0-11 */
107                 *tm_time = ktm_time;
108                 return (tm_time);
109
110         } else if (sscanf(t_string, "%d.%d-%d:%d",
111                                           &ltm_time.tm_mon,
112                                           &ltm_time.tm_mday,
113                                           &ltm_time.tm_hour, &ltm_time.tm_min) == 4) {
114
115                 ltm_time.tm_mon -= 1;   /* Adjust dates from 1-12 to 0-11 */
116                 *tm_time = ltm_time;
117                 return (tm_time);
118
119         } else if (sscanf(t_string, "%d.%d.%d-%d:%d:%d",
120                                           &mtm_time.tm_year,
121                                           &mtm_time.tm_mon,
122                                           &mtm_time.tm_mday,
123                                           &mtm_time.tm_hour,
124                                           &mtm_time.tm_min, &mtm_time.tm_sec) == 6) {
125
126                 mtm_time.tm_year -= 1900;       /* Adjust years */
127                 mtm_time.tm_mon -= 1;   /* Adjust dates from 1-12 to 0-11 */
128                 *tm_time = mtm_time;
129                 return (tm_time);
130
131         } else if (sscanf(t_string, "%d.%d.%d-%d:%d",
132                                           &ntm_time.tm_year,
133                                           &ntm_time.tm_mon,
134                                           &ntm_time.tm_mday,
135                                           &ntm_time.tm_hour, &ntm_time.tm_min) == 5) {
136                 ntm_time.tm_year -= 1900;       /* Adjust years */
137                 ntm_time.tm_mon -= 1;   /* Adjust dates from 1-12 to 0-11 */
138                 *tm_time = ntm_time;
139                 return (tm_time);
140
141         }
142
143         fatalError(invalid_date, t_string); 
144 }
145
146
147 int date_main(int argc, char **argv)
148 {
149         char *date_str = NULL;
150         char *date_fmt = NULL;
151         char *t_buff;
152         int c;
153         int set_time = 0;
154         int rfc822 = 0;
155         int utc = 0;
156         int use_arg = 0;
157         time_t tm;
158         struct tm tm_time;
159
160         /* Interpret command line args */
161         while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
162                 switch (c) {
163                 case 'R':
164                         rfc822 = 1;
165                         break;
166                 case 's':
167                         set_time = 1;
168                         if ((date_str != NULL) || ((date_str = optarg) == NULL))
169                                 usage(date_usage);
170                         break;
171                 case 'u':
172                         utc = 1;
173                         if (putenv("TZ=UTC0") != 0) 
174                                 fatalError(memory_exhausted);
175                         break;
176                 case 'd':
177                         use_arg = 1;
178                         if ((date_str != NULL) || ((date_str = optarg) == NULL))
179                                 usage(date_usage);
180                         break;
181                 default:
182                         usage(date_usage);
183                 }
184         }
185
186         if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
187                 date_fmt = &argv[optind][1];   /* Skip over the '+' */
188         else if (date_str == NULL) {
189                 set_time = 1;
190                 date_str = argv[optind];
191         } else {
192                 usage(date_usage);
193         }
194
195         /* Now we have parsed all the information except the date format
196            which depends on whether the clock is being set or read */
197
198         time(&tm);
199         memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
200         /* Zero out fields - take her back to midnight! */
201         if (date_str != NULL) {
202                 tm_time.tm_sec = 0;
203                 tm_time.tm_min = 0;
204                 tm_time.tm_hour = 0;
205         }
206
207         /* Process any date input to UNIX time since 1 Jan 1970 */
208         if (date_str != NULL) {
209
210                 if (strchr(date_str, ':') != NULL) {
211                         date_conv_ftime(&tm_time, date_str);
212                 } else {
213                         date_conv_time(&tm_time, date_str);
214                 }
215
216                 /* Correct any day of week and day of year etc fields */
217                 tm = mktime(&tm_time);
218                 if (tm < 0)
219                         fatalError(invalid_date, date_str); 
220
221                 /* if setting time, set it */
222                 if (set_time) {
223                         if (stime(&tm) < 0) {
224                                 fatalError("can't set date.\n");
225                         }
226                 }
227         }
228
229         /* Display output */
230
231         /* Deal with format string */
232         if (date_fmt == NULL) {
233                 date_fmt = (rfc822
234                                         ? (utc
235                                            ? "%a, %_d %b %Y %H:%M:%S GMT"
236                                            : "%a, %_d %b %Y %H:%M:%S %z")
237                                         : "%a %b %e %H:%M:%S %Z %Y");
238
239         } else if (*date_fmt == '\0') {
240                 /* Imitate what GNU 'date' does with NO format string! */
241                 printf("\n");
242                 exit(TRUE);
243         }
244
245         /* Handle special conversions */
246
247         if (strncmp(date_fmt, "%f", 2) == 0) {
248                 date_fmt = "%Y.%m.%d-%H:%M:%S";
249         }
250
251         /* Print OUTPUT (after ALL that!) */
252         t_buff = xmalloc(201);
253         strftime(t_buff, 200, date_fmt, &tm_time);
254         printf("%s\n", t_buff);
255
256         return(TRUE);
257 }