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