blob: 6e7aa1f0ce98eea3fe7a07fbf76012f42163c8c4 [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>
Robert Griebl6859d762002-08-05 02:57:12 +00006 *
7 * iso-format handling added by Robert Griebl <griebl@gmx.de>
Eric Andersen2ce1edc1999-10-12 15:42:48 +00008 *
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.
13 *
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.
18 *
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
22 *
23*/
24
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include <stdlib.h>
26#include <errno.h>
27#include <sys/time.h>
28#include <unistd.h>
29#include <time.h>
30#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000031#include <string.h>
Eric Andersen88f50b62000-08-10 17:59:11 +000032#include <getopt.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000033#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000034
35
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
40 mail commands */
41
Eric Andersencc8ed391999-10-05 16:24:54 +000042/* Input parsing code is always bulky - used heavy duty libc stuff as
43 much as possible, missed out a lot of bounds checking */
44
45/* Default input handling to save suprising some people */
46
Eric Andersen3e6ff902001-03-09 21:24:12 +000047static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
Erik Andersene49d5ec2000-02-08 19:58:47 +000048{
49 int nr;
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Glenn L McGrath03195fc2002-08-23 05:58:38 +000051 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),
53 &(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) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 bb_error_msg_and_die(bb_msg_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? */
Glenn L McGrath03195fc2002-08-23 05:58:38 +000060 if (tm_time->tm_year >= 1900) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 tm_time->tm_year -= 1900;
Glenn L McGrath03195fc2002-08-23 05:58:38 +000062 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 /* adjust date */
64 tm_time->tm_mon -= 1;
65
66 return (tm_time);
Eric Andersencc8ed391999-10-05 16:24:54 +000067
68}
69
70
71/* The new stuff for LRP */
72
Eric Andersen3e6ff902001-03-09 21:24:12 +000073static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
Erik Andersene49d5ec2000-02-08 19:58:47 +000074{
Eric Andersen7b5d5942000-11-29 22:01:42 +000075 struct tm t;
Eric Andersencc8ed391999-10-05 16:24:54 +000076
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 /* Parse input and assign appropriately to tm_time */
Eric Andersencc8ed391999-10-05 16:24:54 +000078
Glenn L McGrath03195fc2002-08-23 05:58:38 +000079 if (t =
80 *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min,
81 &t.tm_sec) == 3) {
82 /* no adjustments needed */
83 } else if (t =
84 *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour,
85 &t.tm_min) == 2) {
86 /* no adjustments needed */
87 } else if (t =
88 *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon,
89 &t.tm_mday, &t.tm_hour, &t.tm_min,
90 &t.tm_sec) == 5) {
91 /* Adjust dates from 1-12 to 0-11 */
92 t.tm_mon -= 1;
93 } else if (t =
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 */
97 t.tm_mon -= 1;
98 } else if (t =
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,
101 &t.tm_sec) == 6) {
Eric Andersen7b5d5942000-11-29 22:01:42 +0000102 t.tm_year -= 1900; /* Adjust years */
103 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000104 } else if (t =
105 *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year,
106 &t.tm_mon, &t.tm_mday, &t.tm_hour,
107 &t.tm_min) == 5) {
Eric Andersen7b5d5942000-11-29 22:01:42 +0000108 t.tm_year -= 1900; /* Adjust years */
109 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
Eric Andersen7b5d5942000-11-29 22:01:42 +0000110 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 }
Eric Andersen7b5d5942000-11-29 22:01:42 +0000113 *tm_time = t;
114 return (tm_time);
Eric Andersencc8ed391999-10-05 16:24:54 +0000115}
116
117
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118int date_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000119{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 char *date_str = NULL;
121 char *date_fmt = NULL;
122 char *t_buff;
Eric Andersen8876fb22003-06-20 09:01:58 +0000123 int set_time;
124 int rfc822;
125 int utc;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 int use_arg = 0;
127 time_t tm;
Eric Andersen8876fb22003-06-20 09:01:58 +0000128 unsigned long opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 struct tm tm_time;
130
Robert Griebldf039322002-07-30 23:11:00 +0000131#ifdef CONFIG_FEATURE_DATE_ISOFMT
132 int ifmt = 0;
Eric Andersen8876fb22003-06-20 09:01:58 +0000133 char *isofmt_arg;
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000134
135# define GETOPT_ISOFMT "I::"
Robert Griebldf039322002-07-30 23:11:00 +0000136#else
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000137# define GETOPT_ISOFMT
Robert Griebldf039322002-07-30 23:11:00 +0000138#endif
Eric Andersen8876fb22003-06-20 09:01:58 +0000139 bb_opt_complementaly = "d~ds:s~ds";
140 opt = bb_getopt_ulflags(argc, argv, "Rs:ud:" GETOPT_ISOFMT,
141 &date_str, &date_str
142#ifdef CONFIG_FEATURE_DATE_ISOFMT
143 , &isofmt_arg
144#endif
145 );
146 rfc822 = opt & 1;
147 set_time = opt & 2;
148 utc = opt & 4;
149 if(utc) {
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000150 if (putenv("TZ=UTC0") != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersen8876fb22003-06-20 09:01:58 +0000152 }
153 use_arg = opt & 8;
154 if(opt & 0x80000000UL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155 bb_show_usage();
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000156#ifdef CONFIG_FEATURE_DATE_ISOFMT
Eric Andersen8876fb22003-06-20 09:01:58 +0000157 if(opt & 16) {
158 if (!isofmt_arg)
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000159 ifmt = 1;
160 else {
Eric Andersen8876fb22003-06-20 09:01:58 +0000161 int ifmt_len = bb_strlen(isofmt_arg);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000162
163 if ((ifmt_len <= 4)
Eric Andersen8876fb22003-06-20 09:01:58 +0000164 && (strncmp(isofmt_arg, "date", ifmt_len) == 0)) {
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000165 ifmt = 1;
166 } else if ((ifmt_len <= 5)
Eric Andersen8876fb22003-06-20 09:01:58 +0000167 && (strncmp(isofmt_arg, "hours", ifmt_len) == 0)) {
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000168 ifmt = 2;
169 } else if ((ifmt_len <= 7)
Eric Andersen8876fb22003-06-20 09:01:58 +0000170 && (strncmp(isofmt_arg, "minutes", ifmt_len) == 0)) {
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000171 ifmt = 3;
172 } else if ((ifmt_len <= 7)
Eric Andersen8876fb22003-06-20 09:01:58 +0000173 && (strncmp(isofmt_arg, "seconds", ifmt_len) == 0)) {
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000174 ifmt = 4;
175 }
176 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000177 if (!ifmt) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000180 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000181#endif
Robert Griebldf039322002-07-30 23:11:00 +0000182
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000183 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
184 date_fmt = &argv[optind][1]; /* Skip over the '+' */
185 } else if (date_str == NULL) {
Eric Andersenadd09fd2000-07-14 18:39:08 +0000186 set_time = 1;
187 date_str = argv[optind];
Eric Andersenadd09fd2000-07-14 18:39:08 +0000188 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000189
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 /* Now we have parsed all the information except the date format
191 which depends on whether the clock is being set or read */
Eric Andersencc8ed391999-10-05 16:24:54 +0000192
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193 time(&tm);
194 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
195 /* Zero out fields - take her back to midnight! */
196 if (date_str != NULL) {
197 tm_time.tm_sec = 0;
198 tm_time.tm_min = 0;
199 tm_time.tm_hour = 0;
200 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000201
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 /* Process any date input to UNIX time since 1 Jan 1970 */
203 if (date_str != NULL) {
Eric Andersencc8ed391999-10-05 16:24:54 +0000204
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 if (strchr(date_str, ':') != NULL) {
206 date_conv_ftime(&tm_time, date_str);
207 } else {
208 date_conv_time(&tm_time, date_str);
209 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000210
Eric Andersen77d92682001-05-23 20:32:09 +0000211 /* Correct any day of week and day of year etc. fields */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 tm = mktime(&tm_time);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000213 if (tm < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000214 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000215 }
216 if (utc && (putenv("TZ=UTC0") != 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000217 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersena683ee82000-11-17 18:51:45 +0000218 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000219
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 /* if setting time, set it */
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000221 if (set_time && (stime(&tm) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 bb_perror_msg("cannot set date");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223 }
224 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000225
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226 /* Display output */
Eric Andersencc8ed391999-10-05 16:24:54 +0000227
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 /* Deal with format string */
229 if (date_fmt == NULL) {
Robert Griebldf039322002-07-30 23:11:00 +0000230#ifdef CONFIG_FEATURE_DATE_ISOFMT
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000231 switch (ifmt) {
232 case 4:
233 date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
234 break;
235 case 3:
236 date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
237 break;
238 case 2:
239 date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
240 break;
241 case 1:
242 date_fmt = "%Y-%m-%d";
243 break;
244 case 0:
245 default:
Robert Griebldf039322002-07-30 23:11:00 +0000246#endif
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000247 date_fmt =
248 (rfc822
249 ? (utc ? "%a, %e %b %Y %H:%M:%S GMT" :
250 "%a, %e %b %Y %H:%M:%S %z") : "%a %b %e %H:%M:%S %Z %Y");
251
Robert Griebldf039322002-07-30 23:11:00 +0000252#ifdef CONFIG_FEATURE_DATE_ISOFMT
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000253 break;
Robert Griebldf039322002-07-30 23:11:00 +0000254 }
255#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000256 } else if (*date_fmt == '\0') {
257 /* Imitate what GNU 'date' does with NO format string! */
258 printf("\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000259 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000260 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000261
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262 /* Handle special conversions */
Eric Andersencc8ed391999-10-05 16:24:54 +0000263
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264 if (strncmp(date_fmt, "%f", 2) == 0) {
265 date_fmt = "%Y.%m.%d-%H:%M:%S";
266 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000267
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 /* Print OUTPUT (after ALL that!) */
Erik Andersen0d068a22000-03-21 22:32:57 +0000269 t_buff = xmalloc(201);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270 strftime(t_buff, 200, date_fmt, &tm_time);
Matt Kraai59df6f72001-05-16 14:21:09 +0000271 puts(t_buff);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272
Matt Kraai3e856ce2000-12-01 02:55:13 +0000273 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000274}