blob: 2a82e0413ca74e363a0b6644a8955bbb2bc2f8e1 [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 Andersenc7bda1c2004-03-15 08:29:22 +00006 *
Robert Griebl6859d762002-08-05 02:57:12 +00007 * iso-format handling added by Robert Griebl <griebl@gmx.de>
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +00008 * bugfixes and cleanup by Bernhard Fischer
Eric Andersen2ce1edc1999-10-12 15:42:48 +00009 *
Rob Landleyc5789a62006-02-21 05:06:42 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen2ce1edc1999-10-12 15:42:48 +000011*/
12
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000013#include "busybox.h"
14
Eric Andersenc7bda1c2004-03-15 08:29:22 +000015/* This 'date' command supports only 2 time setting formats,
Eric Andersencc8ed391999-10-05 16:24:54 +000016 all the GNU strftime stuff (its in libc, lets use it),
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000017 setting time using UTC and displaying it, as well as
18 an RFC 2822 compliant date output for shell scripting
Eric Andersencc8ed391999-10-05 16:24:54 +000019 mail commands */
20
Eric Andersencc8ed391999-10-05 16:24:54 +000021/* Input parsing code is always bulky - used heavy duty libc stuff as
22 much as possible, missed out a lot of bounds checking */
23
Eric Andersenaff114c2004-04-14 17:51:38 +000024/* Default input handling to save surprising some people */
Eric Andersencc8ed391999-10-05 16:24:54 +000025
Rob Landley91ed1a42006-07-14 17:59:36 +000026
27#define DATE_OPT_RFC2822 0x01
28#define DATE_OPT_SET 0x02
29#define DATE_OPT_UTC 0x04
30#define DATE_OPT_DATE 0x08
31#define DATE_OPT_REFERENCE 0x10
32#define DATE_OPT_TIMESPEC 0x20
33#define DATE_OPT_HINT 0x40
34
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000035static void maybe_set_utc(int opt)
Erik Andersene49d5ec2000-02-08 19:58:47 +000036{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000037 if ((opt & DATE_OPT_UTC) && putenv("TZ=UTC0") != 0)
38 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersencc8ed391999-10-05 16:24:54 +000039}
40
Erik Andersene49d5ec2000-02-08 19:58:47 +000041int date_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000042{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000043 time_t tm;
44 struct tm tm_time;
45 unsigned long opt;
46 int ifmt = -1;
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 char *date_str = NULL;
48 char *date_fmt = NULL;
Glenn L McGrath01cdb662004-02-17 12:22:21 +000049 char *filename = NULL;
Eric Andersen8876fb22003-06-20 09:01:58 +000050 char *isofmt_arg;
Rob Landleyc5789a62006-02-21 05:06:42 +000051 char *hintfmt_arg;
Glenn L McGrath03195fc2002-08-23 05:58:38 +000052
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000053 bb_opt_complementally = "?:d--s:s--d"
54 USE_FEATURE_DATE_ISOFMT(":R--I:I--R");
Rob Landleyc5789a62006-02-21 05:06:42 +000055 opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000056 USE_FEATURE_DATE_ISOFMT("I::D:"),
Glenn L McGrath01cdb662004-02-17 12:22:21 +000057 &date_str, &date_str, &filename
Rob Landleyc5789a62006-02-21 05:06:42 +000058 USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg));
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000059 maybe_set_utc(opt);
Rob Landleyc5789a62006-02-21 05:06:42 +000060
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000061 if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
Glenn L McGrath5f115412004-02-17 07:51:31 +000062 if (!isofmt_arg) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000063 ifmt = 0; /* default is date */
Glenn L McGrath5f115412004-02-17 07:51:31 +000064 } else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000065 const char * const isoformats[] =
66 {"date", "hours", "minutes", "seconds"};
67
68 for (ifmt = 0; ifmt < 4; ifmt++)
69 if (!strcmp(isofmt_arg, isoformats[ifmt])) {
Rob Landleyc5789a62006-02-21 05:06:42 +000070 break;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000071 }
72 if (ifmt == 4) /* parse error */
73 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 }
Robert Griebldf039322002-07-30 23:11:00 +000076
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000077 /* XXX, date_fmt == NULL from this always */
Glenn L McGrath03195fc2002-08-23 05:58:38 +000078 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
79 date_fmt = &argv[optind][1]; /* Skip over the '+' */
80 } else if (date_str == NULL) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000081 opt |= DATE_OPT_SET;
Eric Andersenadd09fd2000-07-14 18:39:08 +000082 date_str = argv[optind];
Eric Andersenadd09fd2000-07-14 18:39:08 +000083 }
Eric Andersencc8ed391999-10-05 16:24:54 +000084
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 /* Now we have parsed all the information except the date format
86 which depends on whether the clock is being set or read */
Eric Andersencc8ed391999-10-05 16:24:54 +000087
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000088 if (filename) {
Glenn L McGrath01cdb662004-02-17 12:22:21 +000089 struct stat statbuf;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000090 xstat(filename, &statbuf);
91 tm = statbuf.st_mtime;
92 } else
93 time(&tm);
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
95 /* Zero out fields - take her back to midnight! */
96 if (date_str != NULL) {
97 tm_time.tm_sec = 0;
98 tm_time.tm_min = 0;
99 tm_time.tm_hour = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000100
Glenn L McGrath5f115412004-02-17 07:51:31 +0000101 /* Process any date input to UNIX time since 1 Jan 1970 */
Rob Landleyc5789a62006-02-21 05:06:42 +0000102 if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) {
103 strptime(date_str, hintfmt_arg, &tm_time);
104 } else if (strchr(date_str, ':') != NULL) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000105 /* Parse input and assign appropriately to tm_time */
106
107 if (sscanf(date_str, "%d:%d:%d", &tm_time.tm_hour, &tm_time.tm_min,
108 &tm_time.tm_sec) == 3) {
109 /* no adjustments needed */
110 } else if (sscanf(date_str, "%d:%d", &tm_time.tm_hour,
111 &tm_time.tm_min) == 2) {
112 /* no adjustments needed */
113 } else if (sscanf(date_str, "%d.%d-%d:%d:%d", &tm_time.tm_mon,
114 &tm_time.tm_mday, &tm_time.tm_hour,
115 &tm_time.tm_min, &tm_time.tm_sec) == 5) {
116 /* Adjust dates from 1-12 to 0-11 */
117 tm_time.tm_mon -= 1;
118 } else if (sscanf(date_str, "%d.%d-%d:%d", &tm_time.tm_mon,
119 &tm_time.tm_mday,
120 &tm_time.tm_hour, &tm_time.tm_min) == 4) {
121 /* Adjust dates from 1-12 to 0-11 */
122 tm_time.tm_mon -= 1;
123 } else if (sscanf(date_str, "%d.%d.%d-%d:%d:%d", &tm_time.tm_year,
124 &tm_time.tm_mon, &tm_time.tm_mday,
125 &tm_time.tm_hour, &tm_time.tm_min,
126 &tm_time.tm_sec) == 6) {
127 tm_time.tm_year -= 1900; /* Adjust years */
128 tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
129 } else if (sscanf(date_str, "%d.%d.%d-%d:%d", &tm_time.tm_year,
130 &tm_time.tm_mon, &tm_time.tm_mday,
131 &tm_time.tm_hour, &tm_time.tm_min) == 5) {
132 tm_time.tm_year -= 1900; /* Adjust years */
133 tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
134 } else {
135 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
136 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 } else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000138 int nr;
139 char *cp;
140
141 nr = sscanf(date_str, "%2d%2d%2d%2d%d", &tm_time.tm_mon,
142 &tm_time.tm_mday, &tm_time.tm_hour, &tm_time.tm_min,
143 &tm_time.tm_year);
144
145 if (nr < 4 || nr > 5) {
146 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
147 }
148
149 cp = strchr(date_str, '.');
150 if (cp) {
151 nr = sscanf(cp + 1, "%2d", &tm_time.tm_sec);
152 if (nr != 1) {
153 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
154 }
155 }
156
157 /* correct for century - minor Y2K problem here? */
158 if (tm_time.tm_year >= 1900) {
159 tm_time.tm_year -= 1900;
160 }
161 /* adjust date */
162 tm_time.tm_mon -= 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000164
Eric Andersen77d92682001-05-23 20:32:09 +0000165 /* Correct any day of week and day of year etc. fields */
Manuel Novoa III b511f9c2003-07-23 23:24:31 +0000166 tm_time.tm_isdst = -1; /* Be sure to recheck dst. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 tm = mktime(&tm_time);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000168 if (tm < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000170 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000171 maybe_set_utc(opt);
Eric Andersencc8ed391999-10-05 16:24:54 +0000172
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173 /* if setting time, set it */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000174 if ((opt & DATE_OPT_SET) && stime(&tm) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000175 bb_perror_msg("cannot set date");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000176 }
177 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000178
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179 /* Display output */
Eric Andersencc8ed391999-10-05 16:24:54 +0000180
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 /* Deal with format string */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000182
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 if (date_fmt == NULL) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000184 int i;
185 date_fmt = xzalloc(32);
186 if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) {
187 strcpy(date_fmt, "%Y-%m-%d");
188 if (ifmt > 0) {
189 i = 8;
190 date_fmt[i++] = 'T';
191 date_fmt[i++] = '%';
192 date_fmt[i++] = 'H';
193 if (ifmt > 1) {
194 date_fmt[i++] = ':';
195 date_fmt[i++] = '%';
196 date_fmt[i++] = 'M';
197 }
198 if (ifmt > 2) {
199 date_fmt[i++] = ':';
200 date_fmt[i++] = '%';
201 date_fmt[i++] = 'S';
202 }
203format_utc:
204 date_fmt[i++] = '%';
205 date_fmt[i] = (opt & DATE_OPT_UTC) ? 'Z' : 'z';
206 }
207 } else if (opt & DATE_OPT_RFC2822) {
208 strcpy(date_fmt, "%a, %d %b %Y %H:%M:%S ");
209 i = 22;
210 goto format_utc;
211 } else /* default case */
212 date_fmt = "%a %b %e %H:%M:%S %Z %Y";
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000214
Rob Landleyc5789a62006-02-21 05:06:42 +0000215 if (*date_fmt == '\0') {
Rob Landleyc5789a62006-02-21 05:06:42 +0000216 /* With no format string, just print a blank line */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000217 *bb_common_bufsiz1 = 0;
Rob Landleyc5789a62006-02-21 05:06:42 +0000218 } else {
Rob Landleyc5789a62006-02-21 05:06:42 +0000219 /* Handle special conversions */
Eric Andersencc8ed391999-10-05 16:24:54 +0000220
Rob Landleyc5789a62006-02-21 05:06:42 +0000221 if (strncmp(date_fmt, "%f", 2) == 0) {
222 date_fmt = "%Y.%m.%d-%H:%M:%S";
223 }
224
225 /* Generate output string */
"Vladimir N. Oleynik"a2eec602005-10-15 13:45:32 +0000226 strftime(bb_common_bufsiz1, 200, date_fmt, &tm_time);
Rob Landleyf7662da2005-05-28 23:55:26 +0000227 }
Rob Landleyc5789a62006-02-21 05:06:42 +0000228 puts(bb_common_bufsiz1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229
Matt Kraai3e856ce2000-12-01 02:55:13 +0000230 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000231}