1 /* vi: set sw=4 ts=4: */
3 * Mini date implementation for busybox
5 * by Matthew Grant <grantma@anathoth.gen.nz>
7 * iso-format handling added by Robert Griebl <griebl@gmx.de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
42 /* Input parsing code is always bulky - used heavy duty libc stuff as
43 much as possible, missed out a lot of bounds checking */
45 /* Default input handling to save surprising some people */
47 static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
51 nr = sscanf(t_string, "%2d%2d%2d%2d%d", &(tm_time->tm_mon),
52 &(tm_time->tm_mday), &(tm_time->tm_hour), &(tm_time->tm_min),
55 if (nr < 4 || nr > 5) {
56 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
59 /* correct for century - minor Y2K problem here? */
60 if (tm_time->tm_year >= 1900) {
61 tm_time->tm_year -= 1900;
71 /* The new stuff for LRP */
73 static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
77 /* Parse input and assign appropriately to tm_time */
80 *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min,
82 /* no adjustments needed */
84 *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour,
86 /* no adjustments needed */
88 *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon,
89 &t.tm_mday, &t.tm_hour, &t.tm_min,
91 /* Adjust dates from 1-12 to 0-11 */
94 *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon,
95 &t.tm_mday, &t.tm_hour, &t.tm_min) == 4) {
96 /* Adjust dates from 1-12 to 0-11 */
99 *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
100 &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min,
102 t.tm_year -= 1900; /* Adjust years */
103 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
105 *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year,
106 &t.tm_mon, &t.tm_mday, &t.tm_hour,
108 t.tm_year -= 1900; /* Adjust years */
109 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
111 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
117 #define DATE_OPT_RFC2822 0x01
118 #define DATE_OPT_SET 0x02
119 #define DATE_OPT_UTC 0x04
120 #define DATE_OPT_DATE 0x08
121 #define DATE_OPT_REFERENCE 0x10
122 #ifdef CONFIG_FEATURE_DATE_ISOFMT
123 # define DATE_OPT_TIMESPEC 0x20
126 int date_main(int argc, char **argv)
128 char *date_str = NULL;
129 char *date_fmt = NULL;
137 char *filename = NULL;
139 #ifdef CONFIG_FEATURE_DATE_ISOFMT
143 # define GETOPT_ISOFMT "I::"
145 # define GETOPT_ISOFMT
147 bb_opt_complementaly = "d~ds:s~ds";
148 opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:" GETOPT_ISOFMT,
149 &date_str, &date_str, &filename
150 #ifdef CONFIG_FEATURE_DATE_ISOFMT
154 set_time = opt & DATE_OPT_SET;
155 utc = opt & DATE_OPT_UTC;
156 if ((utc) && (putenv("TZ=UTC0") != 0)) {
157 bb_error_msg_and_die(bb_msg_memory_exhausted);
159 use_arg = opt & DATE_OPT_DATE;
160 if(opt & 0x80000000UL)
162 #ifdef CONFIG_FEATURE_DATE_ISOFMT
163 if(opt & DATE_OPT_TIMESPEC) {
167 int ifmt_len = bb_strlen(isofmt_arg);
170 && (strncmp(isofmt_arg, "date", ifmt_len) == 0)) {
172 } else if ((ifmt_len <= 5)
173 && (strncmp(isofmt_arg, "hours", ifmt_len) == 0)) {
175 } else if ((ifmt_len <= 7)
176 && (strncmp(isofmt_arg, "minutes", ifmt_len) == 0)) {
178 } else if ((ifmt_len <= 7)
179 && (strncmp(isofmt_arg, "seconds", ifmt_len) == 0)) {
189 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
190 date_fmt = &argv[optind][1]; /* Skip over the '+' */
191 } else if (date_str == NULL) {
193 date_str = argv[optind];
196 /* Now we have parsed all the information except the date format
197 which depends on whether the clock is being set or read */
201 if(stat(filename,&statbuf))
202 bb_perror_msg_and_die("File '%s' not found.\n",filename);
205 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
206 /* Zero out fields - take her back to midnight! */
207 if (date_str != NULL) {
212 /* Process any date input to UNIX time since 1 Jan 1970 */
213 if (strchr(date_str, ':') != NULL) {
214 date_conv_ftime(&tm_time, date_str);
216 date_conv_time(&tm_time, date_str);
219 /* Correct any day of week and day of year etc. fields */
220 tm_time.tm_isdst = -1; /* Be sure to recheck dst. */
221 tm = mktime(&tm_time);
223 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
225 if (utc && (putenv("TZ=UTC0") != 0)) {
226 bb_error_msg_and_die(bb_msg_memory_exhausted);
229 /* if setting time, set it */
230 if (set_time && (stime(&tm) < 0)) {
231 bb_perror_msg("cannot set date");
237 /* Deal with format string */
238 if (date_fmt == NULL) {
239 #ifdef CONFIG_FEATURE_DATE_ISOFMT
242 date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
245 date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
248 date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
251 date_fmt = "%Y-%m-%d";
256 date_fmt = (opt & DATE_OPT_RFC2822 ?
257 (utc ? "%a, %d %b %Y %H:%M:%S GMT" :
258 "%a, %d %b %Y %H:%M:%S %z") :
259 "%a %b %e %H:%M:%S %Z %Y");
261 #ifdef CONFIG_FEATURE_DATE_ISOFMT
265 } else if (*date_fmt == '\0') {
266 /* Imitate what GNU 'date' does with NO format string! */
271 /* Handle special conversions */
273 if (strncmp(date_fmt, "%f", 2) == 0) {
274 date_fmt = "%Y.%m.%d-%H:%M:%S";
277 /* Print OUTPUT (after ALL that!) */
278 t_buff = xmalloc(201);
279 strftime(t_buff, 200, date_fmt, &tm_time);