blob: 22a1304d5aae8718394805c97ccf527cf95514ed [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen2ce1edc1999-10-12 15:42:48 +00002/*
3 * Mini date implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * by Matthew Grant <grantma@anathoth.gen.nz>
Eric Andersen2ce1edc1999-10-12 15:42:48 +00006 *
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
Eric Andersencc8ed391999-10-05 16:24:54 +000023#include <stdlib.h>
24#include <errno.h>
25#include <sys/time.h>
26#include <unistd.h>
27#include <time.h>
28#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <string.h>
Eric Andersen88f50b62000-08-10 17:59:11 +000030#include <getopt.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000031#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000032
33
34/* This 'date' command supports only 2 time setting formats,
35 all the GNU strftime stuff (its in libc, lets use it),
36 setting time using UTC and displaying int, as well as
37 an RFC 822 complient date output for shell scripting
38 mail commands */
39
Eric Andersencc8ed391999-10-05 16:24:54 +000040/* Input parsing code is always bulky - used heavy duty libc stuff as
41 much as possible, missed out a lot of bounds checking */
42
43/* Default input handling to save suprising some people */
44
Eric Andersen3e6ff902001-03-09 21:24:12 +000045static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
Erik Andersene49d5ec2000-02-08 19:58:47 +000046{
47 int nr;
Eric Andersencc8ed391999-10-05 16:24:54 +000048
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 nr = sscanf(t_string, "%2d%2d%2d%2d%d",
50 &(tm_time->tm_mon),
51 &(tm_time->tm_mday),
52 &(tm_time->tm_hour),
53 &(tm_time->tm_min), &(tm_time->tm_year));
Eric Andersencc8ed391999-10-05 16:24:54 +000054
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 if (nr < 4 || nr > 5) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000056 error_msg_and_die(invalid_date, t_string);
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 }
Eric Andersencc8ed391999-10-05 16:24:54 +000058
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 /* correct for century - minor Y2K problem here? */
60 if (tm_time->tm_year >= 1900)
61 tm_time->tm_year -= 1900;
62 /* adjust date */
63 tm_time->tm_mon -= 1;
64
65 return (tm_time);
Eric Andersencc8ed391999-10-05 16:24:54 +000066
67}
68
69
70/* The new stuff for LRP */
71
Eric Andersen3e6ff902001-03-09 21:24:12 +000072static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
Erik Andersene49d5ec2000-02-08 19:58:47 +000073{
Eric Andersen7b5d5942000-11-29 22:01:42 +000074 struct tm t;
Eric Andersencc8ed391999-10-05 16:24:54 +000075
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 /* Parse input and assign appropriately to tm_time */
Eric Andersencc8ed391999-10-05 16:24:54 +000077
Eric Andersen7b5d5942000-11-29 22:01:42 +000078 if (t=*tm_time,sscanf(t_string, "%d:%d:%d",
79 &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) {
80 /* no adjustments needed */
Eric Andersencc8ed391999-10-05 16:24:54 +000081
Eric Andersen7b5d5942000-11-29 22:01:42 +000082 } else if (t=*tm_time,sscanf(t_string, "%d:%d",
83 &t.tm_hour, &t.tm_min) == 2) {
84 /* no adjustments needed */
Eric Andersencc8ed391999-10-05 16:24:54 +000085
Eric Andersencc8ed391999-10-05 16:24:54 +000086
Eric Andersen7b5d5942000-11-29 22:01:42 +000087 } else if (t=*tm_time,sscanf(t_string, "%d.%d-%d:%d:%d",
88 &t.tm_mon,
89 &t.tm_mday,
90 &t.tm_hour,
91 &t.tm_min, &t.tm_sec) == 5) {
Eric Andersencc8ed391999-10-05 16:24:54 +000092
Eric Andersen7b5d5942000-11-29 22:01:42 +000093 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
Eric Andersencc8ed391999-10-05 16:24:54 +000094
Eric Andersen7b5d5942000-11-29 22:01:42 +000095 } else if (t=*tm_time,sscanf(t_string, "%d.%d-%d:%d",
96 &t.tm_mon,
97 &t.tm_mday,
98 &t.tm_hour, &t.tm_min) == 4) {
Eric Andersencc8ed391999-10-05 16:24:54 +000099
Eric Andersen7b5d5942000-11-29 22:01:42 +0000100 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
Eric Andersencc8ed391999-10-05 16:24:54 +0000101
Eric Andersen7b5d5942000-11-29 22:01:42 +0000102 } else if (t=*tm_time,sscanf(t_string, "%d.%d.%d-%d:%d:%d",
103 &t.tm_year,
104 &t.tm_mon,
105 &t.tm_mday,
106 &t.tm_hour,
107 &t.tm_min, &t.tm_sec) == 6) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000108
Eric Andersen7b5d5942000-11-29 22:01:42 +0000109 t.tm_year -= 1900; /* Adjust years */
110 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
Eric Andersencc8ed391999-10-05 16:24:54 +0000111
Eric Andersen7b5d5942000-11-29 22:01:42 +0000112 } else if (t=*tm_time,sscanf(t_string, "%d.%d.%d-%d:%d",
113 &t.tm_year,
114 &t.tm_mon,
115 &t.tm_mday,
116 &t.tm_hour, &t.tm_min) == 5) {
117 t.tm_year -= 1900; /* Adjust years */
118 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
Eric Andersencc8ed391999-10-05 16:24:54 +0000119
Eric Andersen7b5d5942000-11-29 22:01:42 +0000120 } else {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000121 error_msg_and_die(invalid_date, t_string);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 }
Eric Andersen7b5d5942000-11-29 22:01:42 +0000123 *tm_time = t;
124 return (tm_time);
Eric Andersencc8ed391999-10-05 16:24:54 +0000125}
126
127
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128int date_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000129{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 char *date_str = NULL;
131 char *date_fmt = NULL;
132 char *t_buff;
Pavel Roskin47d49262000-07-17 16:17:19 +0000133 int c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 int set_time = 0;
135 int rfc822 = 0;
136 int utc = 0;
137 int use_arg = 0;
138 time_t tm;
139 struct tm tm_time;
140
141 /* Interpret command line args */
Eric Andersenadd09fd2000-07-14 18:39:08 +0000142 while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
143 switch (c) {
Eric Andersena683ee82000-11-17 18:51:45 +0000144 case 'R':
145 rfc822 = 1;
146 break;
147 case 's':
148 set_time = 1;
149 if ((date_str != NULL) || ((date_str = optarg) == NULL)) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000150 show_usage();
Eric Andersena683ee82000-11-17 18:51:45 +0000151 }
152 break;
153 case 'u':
154 utc = 1;
155 if (putenv("TZ=UTC0") != 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000156 error_msg_and_die(memory_exhausted);
Eric Andersena683ee82000-11-17 18:51:45 +0000157 break;
158 case 'd':
159 use_arg = 1;
160 if ((date_str != NULL) || ((date_str = optarg) == NULL))
Eric Andersen67991cf2001-02-14 21:23:06 +0000161 show_usage();
Eric Andersena683ee82000-11-17 18:51:45 +0000162 break;
163 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000164 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000167
Eric Andersenadd09fd2000-07-14 18:39:08 +0000168 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
169 date_fmt = &argv[optind][1]; /* Skip over the '+' */
170 else if (date_str == NULL) {
171 set_time = 1;
172 date_str = argv[optind];
Eric Andersena683ee82000-11-17 18:51:45 +0000173 }
174#if 0
175 else {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000176 error_msg("date_str='%s' date_fmt='%s'\n", date_str, date_fmt);
Eric Andersen67991cf2001-02-14 21:23:06 +0000177 show_usage();
Eric Andersenadd09fd2000-07-14 18:39:08 +0000178 }
Eric Andersena683ee82000-11-17 18:51:45 +0000179#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000180
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 /* Now we have parsed all the information except the date format
182 which depends on whether the clock is being set or read */
Eric Andersencc8ed391999-10-05 16:24:54 +0000183
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 time(&tm);
185 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
186 /* Zero out fields - take her back to midnight! */
187 if (date_str != NULL) {
188 tm_time.tm_sec = 0;
189 tm_time.tm_min = 0;
190 tm_time.tm_hour = 0;
191 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000192
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193 /* Process any date input to UNIX time since 1 Jan 1970 */
194 if (date_str != NULL) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000195
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 if (strchr(date_str, ':') != NULL) {
197 date_conv_ftime(&tm_time, date_str);
198 } else {
199 date_conv_time(&tm_time, date_str);
200 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000201
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 /* Correct any day of week and day of year etc fields */
203 tm = mktime(&tm_time);
Erik Andersen1ad302a2000-03-24 00:54:46 +0000204 if (tm < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000205 error_msg_and_die(invalid_date, date_str);
Eric Andersena683ee82000-11-17 18:51:45 +0000206 if ( utc ) {
207 if (putenv("TZ=UTC0") != 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000208 error_msg_and_die(memory_exhausted);
Eric Andersena683ee82000-11-17 18:51:45 +0000209 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000210
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 /* if setting time, set it */
212 if (set_time) {
213 if (stime(&tm) < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000214 perror_msg("cannot set date");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 }
216 }
217 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000218
Erik Andersene49d5ec2000-02-08 19:58:47 +0000219 /* Display output */
Eric Andersencc8ed391999-10-05 16:24:54 +0000220
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221 /* Deal with format string */
222 if (date_fmt == NULL) {
223 date_fmt = (rfc822
224 ? (utc
225 ? "%a, %_d %b %Y %H:%M:%S GMT"
226 : "%a, %_d %b %Y %H:%M:%S %z")
227 : "%a %b %e %H:%M:%S %Z %Y");
Eric Andersencc8ed391999-10-05 16:24:54 +0000228
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 } else if (*date_fmt == '\0') {
230 /* Imitate what GNU 'date' does with NO format string! */
231 printf("\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000232 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000234
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 /* Handle special conversions */
Eric Andersencc8ed391999-10-05 16:24:54 +0000236
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237 if (strncmp(date_fmt, "%f", 2) == 0) {
238 date_fmt = "%Y.%m.%d-%H:%M:%S";
239 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000240
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 /* Print OUTPUT (after ALL that!) */
Erik Andersen0d068a22000-03-21 22:32:57 +0000242 t_buff = xmalloc(201);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000243 strftime(t_buff, 200, date_fmt, &tm_time);
244 printf("%s\n", t_buff);
245
Matt Kraai3e856ce2000-12-01 02:55:13 +0000246 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000247}