blob: 3608df69fceceed08e6967ef16bd97c659e0e8b1 [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>
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
Eric Andersenc7bda1c2004-03-15 08:29:22 +000036/* This 'date' command supports only 2 time setting formats,
Eric Andersencc8ed391999-10-05 16:24:54 +000037 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
Eric Andersenaff114c2004-04-14 17:51:38 +000045/* Default input handling to save surprising some people */
Eric Andersencc8ed391999-10-05 16:24:54 +000046
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 Andersen93158422004-10-11 20:52:16 +000050 char *cp;
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Glenn L McGrath03195fc2002-08-23 05:58:38 +000052 nr = sscanf(t_string, "%2d%2d%2d%2d%d", &(tm_time->tm_mon),
53 &(tm_time->tm_mday), &(tm_time->tm_hour), &(tm_time->tm_min),
54 &(tm_time->tm_year));
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 if (nr < 4 || nr > 5) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 }
Eric Andersencc8ed391999-10-05 16:24:54 +000059
Eric Andersen93158422004-10-11 20:52:16 +000060 cp = strchr(t_string, '.');
61 if (cp) {
62 nr = sscanf(cp + 1, "%2d", &(tm_time->tm_sec));
63 if (nr != 1) {
64 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
65 }
66 }
67
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 /* correct for century - minor Y2K problem here? */
Glenn L McGrath03195fc2002-08-23 05:58:38 +000069 if (tm_time->tm_year >= 1900) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 tm_time->tm_year -= 1900;
Glenn L McGrath03195fc2002-08-23 05:58:38 +000071 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 /* adjust date */
73 tm_time->tm_mon -= 1;
74
75 return (tm_time);
Eric Andersencc8ed391999-10-05 16:24:54 +000076
77}
78
79
80/* The new stuff for LRP */
81
Eric Andersen3e6ff902001-03-09 21:24:12 +000082static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
Erik Andersene49d5ec2000-02-08 19:58:47 +000083{
Eric Andersen7b5d5942000-11-29 22:01:42 +000084 struct tm t;
Eric Andersencc8ed391999-10-05 16:24:54 +000085
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 /* Parse input and assign appropriately to tm_time */
Eric Andersencc8ed391999-10-05 16:24:54 +000087
Glenn L McGrath03195fc2002-08-23 05:58:38 +000088 if (t =
89 *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min,
90 &t.tm_sec) == 3) {
91 /* no adjustments needed */
92 } else if (t =
93 *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour,
94 &t.tm_min) == 2) {
95 /* no adjustments needed */
96 } else if (t =
97 *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon,
98 &t.tm_mday, &t.tm_hour, &t.tm_min,
99 &t.tm_sec) == 5) {
100 /* Adjust dates from 1-12 to 0-11 */
101 t.tm_mon -= 1;
102 } else if (t =
103 *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon,
104 &t.tm_mday, &t.tm_hour, &t.tm_min) == 4) {
105 /* Adjust dates from 1-12 to 0-11 */
106 t.tm_mon -= 1;
107 } else if (t =
108 *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
109 &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min,
110 &t.tm_sec) == 6) {
Eric Andersen7b5d5942000-11-29 22:01:42 +0000111 t.tm_year -= 1900; /* Adjust years */
112 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000113 } else if (t =
114 *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year,
115 &t.tm_mon, &t.tm_mday, &t.tm_hour,
116 &t.tm_min) == 5) {
Eric Andersen7b5d5942000-11-29 22:01:42 +0000117 t.tm_year -= 1900; /* Adjust years */
118 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
Eric Andersen7b5d5942000-11-29 22:01:42 +0000119 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000120 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 }
Eric Andersen7b5d5942000-11-29 22:01:42 +0000122 *tm_time = t;
123 return (tm_time);
Eric Andersencc8ed391999-10-05 16:24:54 +0000124}
125
Glenn L McGrath5f115412004-02-17 07:51:31 +0000126#define DATE_OPT_RFC2822 0x01
127#define DATE_OPT_SET 0x02
128#define DATE_OPT_UTC 0x04
129#define DATE_OPT_DATE 0x08
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000130#define DATE_OPT_REFERENCE 0x10
Glenn L McGrathc2266bd2004-02-17 07:58:04 +0000131#ifdef CONFIG_FEATURE_DATE_ISOFMT
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000132# define DATE_OPT_TIMESPEC 0x20
Glenn L McGrathc2266bd2004-02-17 07:58:04 +0000133#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000134
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135int date_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000136{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 char *date_str = NULL;
138 char *date_fmt = NULL;
139 char *t_buff;
Eric Andersen8876fb22003-06-20 09:01:58 +0000140 int set_time;
Eric Andersen8876fb22003-06-20 09:01:58 +0000141 int utc;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 int use_arg = 0;
143 time_t tm;
Eric Andersen8876fb22003-06-20 09:01:58 +0000144 unsigned long opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 struct tm tm_time;
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000146 char *filename = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147
Robert Griebldf039322002-07-30 23:11:00 +0000148#ifdef CONFIG_FEATURE_DATE_ISOFMT
149 int ifmt = 0;
Eric Andersen8876fb22003-06-20 09:01:58 +0000150 char *isofmt_arg;
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000151
152# define GETOPT_ISOFMT "I::"
Robert Griebldf039322002-07-30 23:11:00 +0000153#else
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000154# define GETOPT_ISOFMT
Robert Griebldf039322002-07-30 23:11:00 +0000155#endif
Eric Andersen8876fb22003-06-20 09:01:58 +0000156 bb_opt_complementaly = "d~ds:s~ds";
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000157 opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:" GETOPT_ISOFMT,
158 &date_str, &date_str, &filename
Eric Andersen8876fb22003-06-20 09:01:58 +0000159#ifdef CONFIG_FEATURE_DATE_ISOFMT
160 , &isofmt_arg
161#endif
162 );
Glenn L McGrath5f115412004-02-17 07:51:31 +0000163 set_time = opt & DATE_OPT_SET;
164 utc = opt & DATE_OPT_UTC;
165 if ((utc) && (putenv("TZ=UTC0") != 0)) {
166 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersen8876fb22003-06-20 09:01:58 +0000167 }
Glenn L McGrath5f115412004-02-17 07:51:31 +0000168 use_arg = opt & DATE_OPT_DATE;
Eric Andersen8876fb22003-06-20 09:01:58 +0000169 if(opt & 0x80000000UL)
Glenn L McGrath5f115412004-02-17 07:51:31 +0000170 bb_show_usage();
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000171#ifdef CONFIG_FEATURE_DATE_ISOFMT
Glenn L McGrathc2266bd2004-02-17 07:58:04 +0000172 if(opt & DATE_OPT_TIMESPEC) {
Glenn L McGrath5f115412004-02-17 07:51:31 +0000173 if (!isofmt_arg) {
174 ifmt = 1;
175 } else {
Eric Andersen8876fb22003-06-20 09:01:58 +0000176 int ifmt_len = bb_strlen(isofmt_arg);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000177
Glenn L McGrath5f115412004-02-17 07:51:31 +0000178 if ((ifmt_len <= 4)
Eric Andersen8876fb22003-06-20 09:01:58 +0000179 && (strncmp(isofmt_arg, "date", ifmt_len) == 0)) {
Glenn L McGrath5f115412004-02-17 07:51:31 +0000180 ifmt = 1;
181 } else if ((ifmt_len <= 5)
182 && (strncmp(isofmt_arg, "hours", ifmt_len) == 0)) {
183 ifmt = 2;
184 } else if ((ifmt_len <= 7)
185 && (strncmp(isofmt_arg, "minutes", ifmt_len) == 0)) {
186 ifmt = 3;
187 } else if ((ifmt_len <= 7)
188 && (strncmp(isofmt_arg, "seconds", ifmt_len) == 0)) {
189 ifmt = 4;
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000190 }
Glenn L McGrath5f115412004-02-17 07:51:31 +0000191 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000192 if (!ifmt) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000193 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000195 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000196#endif
Robert Griebldf039322002-07-30 23:11:00 +0000197
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000198 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
199 date_fmt = &argv[optind][1]; /* Skip over the '+' */
200 } else if (date_str == NULL) {
Eric Andersenadd09fd2000-07-14 18:39:08 +0000201 set_time = 1;
202 date_str = argv[optind];
Eric Andersenadd09fd2000-07-14 18:39:08 +0000203 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000204
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 /* Now we have parsed all the information except the date format
206 which depends on whether the clock is being set or read */
Eric Andersencc8ed391999-10-05 16:24:54 +0000207
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000208 if(filename) {
209 struct stat statbuf;
210 if(stat(filename,&statbuf))
211 bb_perror_msg_and_die("File '%s' not found.\n",filename);
212 tm=statbuf.st_mtime;
213 } else time(&tm);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
215 /* Zero out fields - take her back to midnight! */
216 if (date_str != NULL) {
217 tm_time.tm_sec = 0;
218 tm_time.tm_min = 0;
219 tm_time.tm_hour = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000220
Glenn L McGrath5f115412004-02-17 07:51:31 +0000221 /* Process any date input to UNIX time since 1 Jan 1970 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222 if (strchr(date_str, ':') != NULL) {
223 date_conv_ftime(&tm_time, date_str);
224 } else {
225 date_conv_time(&tm_time, date_str);
226 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000227
Eric Andersen77d92682001-05-23 20:32:09 +0000228 /* Correct any day of week and day of year etc. fields */
Manuel Novoa III b511f9c2003-07-23 23:24:31 +0000229 tm_time.tm_isdst = -1; /* Be sure to recheck dst. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 tm = mktime(&tm_time);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000231 if (tm < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000232 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000233 }
234 if (utc && (putenv("TZ=UTC0") != 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000235 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersena683ee82000-11-17 18:51:45 +0000236 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000237
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 /* if setting time, set it */
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000239 if (set_time && (stime(&tm) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240 bb_perror_msg("cannot set date");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 }
242 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000243
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 /* Display output */
Eric Andersencc8ed391999-10-05 16:24:54 +0000245
Erik Andersene49d5ec2000-02-08 19:58:47 +0000246 /* Deal with format string */
247 if (date_fmt == NULL) {
Robert Griebldf039322002-07-30 23:11:00 +0000248#ifdef CONFIG_FEATURE_DATE_ISOFMT
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000249 switch (ifmt) {
250 case 4:
251 date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
252 break;
253 case 3:
254 date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
255 break;
256 case 2:
257 date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
258 break;
259 case 1:
260 date_fmt = "%Y-%m-%d";
261 break;
262 case 0:
263 default:
Robert Griebldf039322002-07-30 23:11:00 +0000264#endif
Eric Andersen39396b92004-04-06 09:38:18 +0000265 date_fmt = (opt & DATE_OPT_RFC2822 ?
266 (utc ? "%a, %d %b %Y %H:%M:%S GMT" :
267 "%a, %d %b %Y %H:%M:%S %z") :
268 "%a %b %e %H:%M:%S %Z %Y");
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000269
Robert Griebldf039322002-07-30 23:11:00 +0000270#ifdef CONFIG_FEATURE_DATE_ISOFMT
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000271 break;
Robert Griebldf039322002-07-30 23:11:00 +0000272 }
273#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 } else if (*date_fmt == '\0') {
275 /* Imitate what GNU 'date' does with NO format string! */
276 printf("\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000277 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000278 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000279
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280 /* Handle special conversions */
Eric Andersencc8ed391999-10-05 16:24:54 +0000281
Erik Andersene49d5ec2000-02-08 19:58:47 +0000282 if (strncmp(date_fmt, "%f", 2) == 0) {
283 date_fmt = "%Y.%m.%d-%H:%M:%S";
284 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000285
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286 /* Print OUTPUT (after ALL that!) */
Erik Andersen0d068a22000-03-21 22:32:57 +0000287 t_buff = xmalloc(201);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000288 strftime(t_buff, 200, date_fmt, &tm_time);
Matt Kraai59df6f72001-05-16 14:21:09 +0000289 puts(t_buff);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000290
Matt Kraai3e856ce2000-12-01 02:55:13 +0000291 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000292}