blob: 2d411ab310d0d190544ca91a18f27513a3b5e127 [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 Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000033
34
Eric Andersenc7bda1c2004-03-15 08:29:22 +000035/* This 'date' command supports only 2 time setting formats,
Eric Andersencc8ed391999-10-05 16:24:54 +000036 all the GNU strftime stuff (its in libc, lets use it),
37 setting time using UTC and displaying int, as well as
38 an RFC 822 complient date output for shell scripting
39 mail commands */
40
Eric Andersencc8ed391999-10-05 16:24:54 +000041/* Input parsing code is always bulky - used heavy duty libc stuff as
42 much as possible, missed out a lot of bounds checking */
43
Eric Andersenaff114c2004-04-14 17:51:38 +000044/* Default input handling to save surprising some people */
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Eric Andersen3e6ff902001-03-09 21:24:12 +000046static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
Erik Andersene49d5ec2000-02-08 19:58:47 +000047{
48 int nr;
Eric Andersen93158422004-10-11 20:52:16 +000049 char *cp;
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
Eric Andersen93158422004-10-11 20:52:16 +000059 cp = strchr(t_string, '.');
60 if (cp) {
61 nr = sscanf(cp + 1, "%2d", &(tm_time->tm_sec));
62 if (nr != 1) {
63 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
64 }
65 }
66
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 /* correct for century - minor Y2K problem here? */
Glenn L McGrath03195fc2002-08-23 05:58:38 +000068 if (tm_time->tm_year >= 1900) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 tm_time->tm_year -= 1900;
Glenn L McGrath03195fc2002-08-23 05:58:38 +000070 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 /* adjust date */
72 tm_time->tm_mon -= 1;
73
74 return (tm_time);
Eric Andersencc8ed391999-10-05 16:24:54 +000075
76}
77
78
79/* The new stuff for LRP */
80
Eric Andersen3e6ff902001-03-09 21:24:12 +000081static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
Erik Andersene49d5ec2000-02-08 19:58:47 +000082{
Eric Andersen7b5d5942000-11-29 22:01:42 +000083 struct tm t;
Eric Andersencc8ed391999-10-05 16:24:54 +000084
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 /* Parse input and assign appropriately to tm_time */
Eric Andersencc8ed391999-10-05 16:24:54 +000086
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000087 if (t = *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min,
Glenn L McGrath03195fc2002-08-23 05:58:38 +000088 &t.tm_sec) == 3) {
89 /* no adjustments needed */
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000090 } else if (t = *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour,
Glenn L McGrath03195fc2002-08-23 05:58:38 +000091 &t.tm_min) == 2) {
92 /* no adjustments needed */
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000093 } else if (t = *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000094 &t.tm_mday, &t.tm_hour,
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000095 &t.tm_min, &t.tm_sec) == 5) {
Glenn L McGrath03195fc2002-08-23 05:58:38 +000096 /* Adjust dates from 1-12 to 0-11 */
97 t.tm_mon -= 1;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000098 } else if (t = *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000099 &t.tm_mday,
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000100 &t.tm_hour, &t.tm_min) == 4) {
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000101 /* Adjust dates from 1-12 to 0-11 */
102 t.tm_mon -= 1;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000103 } else if (t = *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000104 &t.tm_mon, &t.tm_mday,
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000105 &t.tm_hour, &t.tm_min,
106 &t.tm_sec) == 6) {
Eric Andersen7b5d5942000-11-29 22:01:42 +0000107 t.tm_year -= 1900; /* Adjust years */
108 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000109 } else if (t = *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000110 &t.tm_mon, &t.tm_mday,
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000111 &t.tm_hour, &t.tm_min) == 5) {
Eric Andersen7b5d5942000-11-29 22:01:42 +0000112 t.tm_year -= 1900; /* Adjust years */
113 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
Eric Andersen7b5d5942000-11-29 22:01:42 +0000114 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 }
Eric Andersen7b5d5942000-11-29 22:01:42 +0000117 *tm_time = t;
118 return (tm_time);
Eric Andersencc8ed391999-10-05 16:24:54 +0000119}
120
Glenn L McGrath5f115412004-02-17 07:51:31 +0000121#define DATE_OPT_RFC2822 0x01
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000122#define DATE_OPT_SET 0x02
123#define DATE_OPT_UTC 0x04
124#define DATE_OPT_DATE 0x08
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000125#define DATE_OPT_REFERENCE 0x10
Glenn L McGrathc2266bd2004-02-17 07:58:04 +0000126#ifdef CONFIG_FEATURE_DATE_ISOFMT
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000127# define DATE_OPT_TIMESPEC 0x20
Glenn L McGrathc2266bd2004-02-17 07:58:04 +0000128#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000129
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130int date_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000131{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 char *date_str = NULL;
133 char *date_fmt = NULL;
Eric Andersen8876fb22003-06-20 09:01:58 +0000134 int set_time;
Eric Andersen8876fb22003-06-20 09:01:58 +0000135 int utc;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000136#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 int use_arg = 0;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000138#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139 time_t tm;
Eric Andersen8876fb22003-06-20 09:01:58 +0000140 unsigned long opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 struct tm tm_time;
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000142 char *filename = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143
Robert Griebldf039322002-07-30 23:11:00 +0000144#ifdef CONFIG_FEATURE_DATE_ISOFMT
145 int ifmt = 0;
Eric Andersen8876fb22003-06-20 09:01:58 +0000146 char *isofmt_arg;
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000147
148# define GETOPT_ISOFMT "I::"
Robert Griebldf039322002-07-30 23:11:00 +0000149#else
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000150# define GETOPT_ISOFMT
Robert Griebldf039322002-07-30 23:11:00 +0000151#endif
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +0000152 bb_opt_complementally = "?:d--s:s--d";
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000153 opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:" GETOPT_ISOFMT,
154 &date_str, &date_str, &filename
Eric Andersen8876fb22003-06-20 09:01:58 +0000155#ifdef CONFIG_FEATURE_DATE_ISOFMT
156 , &isofmt_arg
157#endif
158 );
Glenn L McGrath5f115412004-02-17 07:51:31 +0000159 set_time = opt & DATE_OPT_SET;
160 utc = opt & DATE_OPT_UTC;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000161 if (utc && putenv("TZ=UTC0") != 0) {
Glenn L McGrath5f115412004-02-17 07:51:31 +0000162 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersen8876fb22003-06-20 09:01:58 +0000163 }
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000164#if 0
Glenn L McGrath5f115412004-02-17 07:51:31 +0000165 use_arg = opt & DATE_OPT_DATE;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000166#endif
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000167#ifdef CONFIG_FEATURE_DATE_ISOFMT
Glenn L McGrathc2266bd2004-02-17 07:58:04 +0000168 if(opt & DATE_OPT_TIMESPEC) {
Glenn L McGrath5f115412004-02-17 07:51:31 +0000169 if (!isofmt_arg) {
170 ifmt = 1;
171 } else {
Eric Andersen8876fb22003-06-20 09:01:58 +0000172 int ifmt_len = bb_strlen(isofmt_arg);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000173
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000174 if (ifmt_len <= 4
175 && strncmp(isofmt_arg, "date", ifmt_len) == 0) {
Glenn L McGrath5f115412004-02-17 07:51:31 +0000176 ifmt = 1;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000177 } else if (ifmt_len <= 5
178 && strncmp(isofmt_arg, "hours", ifmt_len) == 0) {
Glenn L McGrath5f115412004-02-17 07:51:31 +0000179 ifmt = 2;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000180 } else if (ifmt_len <= 7
181 && strncmp(isofmt_arg, "minutes", ifmt_len) == 0) {
Glenn L McGrath5f115412004-02-17 07:51:31 +0000182 ifmt = 3;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000183 } else if (ifmt_len <= 7
184 && strncmp(isofmt_arg, "seconds", ifmt_len) == 0) {
Glenn L McGrath5f115412004-02-17 07:51:31 +0000185 ifmt = 4;
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000186 }
Glenn L McGrath5f115412004-02-17 07:51:31 +0000187 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000188 if (!ifmt) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000189 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000192#endif
Robert Griebldf039322002-07-30 23:11:00 +0000193
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000194 /* XXX, date_fmt == NULL from this always */
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000195 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
196 date_fmt = &argv[optind][1]; /* Skip over the '+' */
197 } else if (date_str == NULL) {
Eric Andersenadd09fd2000-07-14 18:39:08 +0000198 set_time = 1;
199 date_str = argv[optind];
Eric Andersenadd09fd2000-07-14 18:39:08 +0000200 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000201
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 /* Now we have parsed all the information except the date format
203 which depends on whether the clock is being set or read */
Eric Andersencc8ed391999-10-05 16:24:54 +0000204
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000205 if(filename) {
206 struct stat statbuf;
207 if(stat(filename,&statbuf))
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000208 bb_perror_msg_and_die("File '%s' not found.", filename);
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000209 tm=statbuf.st_mtime;
210 } else time(&tm);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
212 /* Zero out fields - take her back to midnight! */
213 if (date_str != NULL) {
214 tm_time.tm_sec = 0;
215 tm_time.tm_min = 0;
216 tm_time.tm_hour = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000217
Glenn L McGrath5f115412004-02-17 07:51:31 +0000218 /* Process any date input to UNIX time since 1 Jan 1970 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000219 if (strchr(date_str, ':') != NULL) {
220 date_conv_ftime(&tm_time, date_str);
221 } else {
222 date_conv_time(&tm_time, date_str);
223 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000224
Eric Andersen77d92682001-05-23 20:32:09 +0000225 /* Correct any day of week and day of year etc. fields */
Manuel Novoa III b511f9c2003-07-23 23:24:31 +0000226 tm_time.tm_isdst = -1; /* Be sure to recheck dst. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227 tm = mktime(&tm_time);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000228 if (tm < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000229 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000230 }
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000231 if (utc && putenv("TZ=UTC0") != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000232 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersena683ee82000-11-17 18:51:45 +0000233 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000234
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 /* if setting time, set it */
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000236 if (set_time && stime(&tm) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000237 bb_perror_msg("cannot set date");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 }
239 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000240
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 /* Display output */
Eric Andersencc8ed391999-10-05 16:24:54 +0000242
Erik Andersene49d5ec2000-02-08 19:58:47 +0000243 /* Deal with format string */
244 if (date_fmt == NULL) {
Robert Griebldf039322002-07-30 23:11:00 +0000245#ifdef CONFIG_FEATURE_DATE_ISOFMT
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000246 switch (ifmt) {
247 case 4:
248 date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
249 break;
250 case 3:
251 date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
252 break;
253 case 2:
254 date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
255 break;
256 case 1:
257 date_fmt = "%Y-%m-%d";
258 break;
259 case 0:
260 default:
Robert Griebldf039322002-07-30 23:11:00 +0000261#endif
Eric Andersen39396b92004-04-06 09:38:18 +0000262 date_fmt = (opt & DATE_OPT_RFC2822 ?
263 (utc ? "%a, %d %b %Y %H:%M:%S GMT" :
264 "%a, %d %b %Y %H:%M:%S %z") :
265 "%a %b %e %H:%M:%S %Z %Y");
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000266
Robert Griebldf039322002-07-30 23:11:00 +0000267#ifdef CONFIG_FEATURE_DATE_ISOFMT
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000268 break;
Robert Griebldf039322002-07-30 23:11:00 +0000269 }
270#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271 } else if (*date_fmt == '\0') {
272 /* Imitate what GNU 'date' does with NO format string! */
273 printf("\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000274 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000276
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 /* Handle special conversions */
Eric Andersencc8ed391999-10-05 16:24:54 +0000278
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 if (strncmp(date_fmt, "%f", 2) == 0) {
280 date_fmt = "%Y.%m.%d-%H:%M:%S";
281 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000282
Rob Landleyf7662da2005-05-28 23:55:26 +0000283 {
284 /* Print OUTPUT (after ALL that!) */
"Vladimir N. Oleynik"a2eec602005-10-15 13:45:32 +0000285 strftime(bb_common_bufsiz1, 200, date_fmt, &tm_time);
286 puts(bb_common_bufsiz1);
Rob Landleyf7662da2005-05-28 23:55:26 +0000287 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000288
Matt Kraai3e856ce2000-12-01 02:55:13 +0000289 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000290}