2 * Mini date implementation for busybox
4 * by Matthew Grant <grantma@anathoth.gen.nz>
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.
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.
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
31 /* This 'date' command supports only 2 time setting formats,
32 all the GNU strftime stuff (its in libc, lets use it),
33 setting time using UTC and displaying int, as well as
34 an RFC 822 complient date output for shell scripting
37 static const char date_usage[] = "date [OPTION]... [+FORMAT]\n"
38 " or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n"
39 "Display the current time in the given FORMAT, or set the system date.\n"
40 "\nOptions:\n\t-R\t\toutput RFC-822 compliant date string\n"
41 "\t-s\t\tset time described by STRING\n"
42 "\t-u\t\tprint or set Coordinated Universal Time\n";
45 /* Input parsing code is always bulky - used heavy duty libc stuff as
46 much as possible, missed out a lot of bounds checking */
48 /* Default input handling to save suprising some people */
51 date_conv_time(struct tm *tm_time, const char *t_string) {
54 nr = sscanf(t_string, "%2d%2d%2d%2d%d",
61 if(nr < 4 || nr > 5) {
62 fprintf(stderr, "date: invalid date `%s'\n", t_string);
66 /* correct for century - minor Y2K problem here? */
67 if(tm_time->tm_year >= 1900)
68 tm_time->tm_year -= 1900;
77 /* The new stuff for LRP */
80 date_conv_ftime(struct tm *tm_time, const char *t_string) {
81 struct tm itm_time, jtm_time, ktm_time, \
82 ltm_time, mtm_time, ntm_time;
91 /* Parse input and assign appropriately to tm_time */
93 if(sscanf(t_string, "%d:%d:%d",
96 &itm_time.tm_sec) == 3 ) {
101 } else if (sscanf(t_string, "%d:%d",
103 &jtm_time.tm_min) == 2) {
108 } else if (sscanf(t_string, "%d.%d-%d:%d:%d",
113 &ktm_time.tm_sec) == 5) {
115 ktm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
119 } else if (sscanf(t_string, "%d.%d-%d:%d",
123 <m_time.tm_min) == 4) {
125 ltm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
129 } else if (sscanf(t_string, "%d.%d.%d-%d:%d:%d",
135 &mtm_time.tm_sec) == 6) {
137 mtm_time.tm_year -= 1900; /* Adjust years */
138 mtm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
142 } else if (sscanf(t_string, "%d.%d.%d-%d:%d",
147 &ntm_time.tm_min) == 5) {
148 ntm_time.tm_year -= 1900; /* Adjust years */
149 ntm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
155 fprintf(stderr, "date: invalid date `%s'\n", t_string);
163 date_main(int argc, char * * argv)
165 char *date_str = NULL;
166 char *date_fmt = NULL;
176 /* Interpret command line args */
179 while (i > 0 && **argv) {
181 while (i>0 && *++(*argv)) switch (**argv) {
187 if(date_str != NULL) usage ( date_usage);
192 if (putenv ("TZ=UTC0") != 0) {
193 fprintf(stderr,"date: memory exhausted\n");
196 /* Look ma, no break. Don't fix it either. */
199 if(date_str != NULL) usage ( date_usage);
206 if ( (date_fmt == NULL) && (strcmp(*argv, "+")==0) )
208 else if (date_str == NULL) {
220 /* Now we have parsed all the information except the date format
221 which depends on whether the clock is being set or read */
224 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
225 /* Zero out fields - take her back to midnight!*/
226 if(date_str != NULL) {
232 /* Process any date input to UNIX time since 1 Jan 1970 */
233 if(date_str != NULL) {
235 if(strchr(date_str, ':') != NULL) {
236 date_conv_ftime(&tm_time, date_str);
238 date_conv_time(&tm_time, date_str);
241 /* Correct any day of week and day of year etc fields */
242 tm = mktime(&tm_time);
244 fprintf(stderr, "date: invalid date `%s'\n", date_str);
248 /* if setting time, set it */
250 if( stime(&tm) < 0) {
251 fprintf(stderr, "date: can't set date.\n");
259 /* Deal with format string */
260 if(date_fmt == NULL) {
263 ? "%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");
267 } else if ( *date_fmt == '\0' ) {
268 /* Imitate what GNU 'date' does with NO format string! */
273 /* Handle special conversions */
275 if( strncmp( date_fmt, "%f", 2) == 0 ) {
276 date_fmt = "%Y.%m.%d-%H:%M:%S";
279 /* Print OUTPUT (after ALL that!) */
280 t_buff = malloc(201);
281 strftime(t_buff, 200, date_fmt, &tm_time);
282 printf("%s\n", t_buff);