blob: a922eb96c54c94bbc70e43bfe9ae13ce8b0bf167 [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 *
Rob Landleyc5789a62006-02-21 05:06:42 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen2ce1edc1999-10-12 15:42:48 +000010*/
11
Eric Andersencc8ed391999-10-05 16:24:54 +000012#include <stdlib.h>
13#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000014#include <unistd.h>
15#include <time.h>
16#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000017#include <string.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000018#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000019
Eric Andersenc7bda1c2004-03-15 08:29:22 +000020/* This 'date' command supports only 2 time setting formats,
Eric Andersencc8ed391999-10-05 16:24:54 +000021 all the GNU strftime stuff (its in libc, lets use it),
22 setting time using UTC and displaying int, as well as
23 an RFC 822 complient date output for shell scripting
24 mail commands */
25
Eric Andersencc8ed391999-10-05 16:24:54 +000026/* Input parsing code is always bulky - used heavy duty libc stuff as
27 much as possible, missed out a lot of bounds checking */
28
Eric Andersenaff114c2004-04-14 17:51:38 +000029/* Default input handling to save surprising some people */
Eric Andersencc8ed391999-10-05 16:24:54 +000030
Eric Andersen3e6ff902001-03-09 21:24:12 +000031static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
Erik Andersene49d5ec2000-02-08 19:58:47 +000032{
33 int nr;
Eric Andersen93158422004-10-11 20:52:16 +000034 char *cp;
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Glenn L McGrath03195fc2002-08-23 05:58:38 +000036 nr = sscanf(t_string, "%2d%2d%2d%2d%d", &(tm_time->tm_mon),
37 &(tm_time->tm_mday), &(tm_time->tm_hour), &(tm_time->tm_min),
38 &(tm_time->tm_year));
Eric Andersencc8ed391999-10-05 16:24:54 +000039
Erik Andersene49d5ec2000-02-08 19:58:47 +000040 if (nr < 4 || nr > 5) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 }
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Eric Andersen93158422004-10-11 20:52:16 +000044 cp = strchr(t_string, '.');
45 if (cp) {
46 nr = sscanf(cp + 1, "%2d", &(tm_time->tm_sec));
47 if (nr != 1) {
48 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
49 }
50 }
51
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 /* correct for century - minor Y2K problem here? */
Glenn L McGrath03195fc2002-08-23 05:58:38 +000053 if (tm_time->tm_year >= 1900) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000054 tm_time->tm_year -= 1900;
Glenn L McGrath03195fc2002-08-23 05:58:38 +000055 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 /* adjust date */
57 tm_time->tm_mon -= 1;
58
59 return (tm_time);
Eric Andersencc8ed391999-10-05 16:24:54 +000060
61}
62
63
64/* The new stuff for LRP */
65
Eric Andersen3e6ff902001-03-09 21:24:12 +000066static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
Erik Andersene49d5ec2000-02-08 19:58:47 +000067{
Eric Andersen7b5d5942000-11-29 22:01:42 +000068 struct tm t;
Eric Andersencc8ed391999-10-05 16:24:54 +000069
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 /* Parse input and assign appropriately to tm_time */
Eric Andersencc8ed391999-10-05 16:24:54 +000071
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000072 if (t = *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min,
Glenn L McGrath03195fc2002-08-23 05:58:38 +000073 &t.tm_sec) == 3) {
74 /* no adjustments needed */
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000075 } else if (t = *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour,
Glenn L McGrath03195fc2002-08-23 05:58:38 +000076 &t.tm_min) == 2) {
77 /* no adjustments needed */
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000078 } else if (t = *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000079 &t.tm_mday, &t.tm_hour,
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000080 &t.tm_min, &t.tm_sec) == 5) {
Glenn L McGrath03195fc2002-08-23 05:58:38 +000081 /* Adjust dates from 1-12 to 0-11 */
82 t.tm_mon -= 1;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000083 } else if (t = *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000084 &t.tm_mday,
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000085 &t.tm_hour, &t.tm_min) == 4) {
Glenn L McGrath03195fc2002-08-23 05:58:38 +000086 /* Adjust dates from 1-12 to 0-11 */
87 t.tm_mon -= 1;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000088 } else if (t = *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000089 &t.tm_mon, &t.tm_mday,
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000090 &t.tm_hour, &t.tm_min,
91 &t.tm_sec) == 6) {
Eric Andersen7b5d5942000-11-29 22:01:42 +000092 t.tm_year -= 1900; /* Adjust years */
93 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000094 } else if (t = *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000095 &t.tm_mon, &t.tm_mday,
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +000096 &t.tm_hour, &t.tm_min) == 5) {
Eric Andersen7b5d5942000-11-29 22:01:42 +000097 t.tm_year -= 1900; /* Adjust years */
98 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
Eric Andersen7b5d5942000-11-29 22:01:42 +000099 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 }
Eric Andersen7b5d5942000-11-29 22:01:42 +0000102 *tm_time = t;
103 return (tm_time);
Eric Andersencc8ed391999-10-05 16:24:54 +0000104}
105
Glenn L McGrath5f115412004-02-17 07:51:31 +0000106#define DATE_OPT_RFC2822 0x01
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000107#define DATE_OPT_SET 0x02
108#define DATE_OPT_UTC 0x04
109#define DATE_OPT_DATE 0x08
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000110#define DATE_OPT_REFERENCE 0x10
Rob Landleyc5789a62006-02-21 05:06:42 +0000111#define DATE_OPT_TIMESPEC 0x20
112#define DATE_OPT_HINT 0x40
Eric Andersencc8ed391999-10-05 16:24:54 +0000113
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114int date_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000115{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 char *date_str = NULL;
117 char *date_fmt = NULL;
Eric Andersen8876fb22003-06-20 09:01:58 +0000118 int set_time;
Eric Andersen8876fb22003-06-20 09:01:58 +0000119 int utc;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 time_t tm;
Eric Andersen8876fb22003-06-20 09:01:58 +0000121 unsigned long opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 struct tm tm_time;
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000123 char *filename = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124
Robert Griebldf039322002-07-30 23:11:00 +0000125 int ifmt = 0;
Eric Andersen8876fb22003-06-20 09:01:58 +0000126 char *isofmt_arg;
Rob Landleyc5789a62006-02-21 05:06:42 +0000127 char *hintfmt_arg;
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000128
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +0000129 bb_opt_complementally = "?:d--s:s--d";
Rob Landleyc5789a62006-02-21 05:06:42 +0000130 opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:"
131 USE_FEATURE_DATE_ISOFMT("I::D:"),
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000132 &date_str, &date_str, &filename
Rob Landleyc5789a62006-02-21 05:06:42 +0000133 USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg));
Glenn L McGrath5f115412004-02-17 07:51:31 +0000134 set_time = opt & DATE_OPT_SET;
135 utc = opt & DATE_OPT_UTC;
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000136 if (utc && putenv("TZ=UTC0") != 0) {
Glenn L McGrath5f115412004-02-17 07:51:31 +0000137 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersen8876fb22003-06-20 09:01:58 +0000138 }
Rob Landleyc5789a62006-02-21 05:06:42 +0000139
140 if(ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
Glenn L McGrath5f115412004-02-17 07:51:31 +0000141 if (!isofmt_arg) {
142 ifmt = 1;
143 } else {
Rob Landleyc5789a62006-02-21 05:06:42 +0000144 char *isoformats[]={"date","hours","minutes","seconds"};
145 for(ifmt = 4; ifmt;)
146 if(!strcmp(isofmt_arg,isoformats[--ifmt]))
147 break;
Glenn L McGrath5f115412004-02-17 07:51:31 +0000148 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000149 if (!ifmt) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000150 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 }
Robert Griebldf039322002-07-30 23:11:00 +0000153
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000154 /* XXX, date_fmt == NULL from this always */
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000155 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
156 date_fmt = &argv[optind][1]; /* Skip over the '+' */
157 } else if (date_str == NULL) {
Eric Andersenadd09fd2000-07-14 18:39:08 +0000158 set_time = 1;
159 date_str = argv[optind];
Eric Andersenadd09fd2000-07-14 18:39:08 +0000160 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000161
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162 /* Now we have parsed all the information except the date format
163 which depends on whether the clock is being set or read */
Eric Andersencc8ed391999-10-05 16:24:54 +0000164
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000165 if(filename) {
166 struct stat statbuf;
Rob Landleyc5b1d4d2006-03-13 15:45:16 +0000167 xstat(filename,&statbuf);
Glenn L McGrath01cdb662004-02-17 12:22:21 +0000168 tm=statbuf.st_mtime;
169 } else time(&tm);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
171 /* Zero out fields - take her back to midnight! */
172 if (date_str != NULL) {
173 tm_time.tm_sec = 0;
174 tm_time.tm_min = 0;
175 tm_time.tm_hour = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000176
Glenn L McGrath5f115412004-02-17 07:51:31 +0000177 /* Process any date input to UNIX time since 1 Jan 1970 */
Rob Landleyc5789a62006-02-21 05:06:42 +0000178 if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) {
179 strptime(date_str, hintfmt_arg, &tm_time);
180 } else if (strchr(date_str, ':') != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 date_conv_ftime(&tm_time, date_str);
182 } else {
183 date_conv_time(&tm_time, date_str);
184 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000185
Eric Andersen77d92682001-05-23 20:32:09 +0000186 /* Correct any day of week and day of year etc. fields */
Manuel Novoa III b511f9c2003-07-23 23:24:31 +0000187 tm_time.tm_isdst = -1; /* Be sure to recheck dst. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188 tm = mktime(&tm_time);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000189 if (tm < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000190 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000191 }
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000192 if (utc && putenv("TZ=UTC0") != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000193 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersena683ee82000-11-17 18:51:45 +0000194 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000195
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 /* if setting time, set it */
"Vladimir N. Oleynik"3ade65f2005-10-12 14:36:42 +0000197 if (set_time && stime(&tm) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000198 bb_perror_msg("cannot set date");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 }
200 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000201
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 /* Display output */
Eric Andersencc8ed391999-10-05 16:24:54 +0000203
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 /* Deal with format string */
205 if (date_fmt == NULL) {
Rob Landleyc5789a62006-02-21 05:06:42 +0000206 /* Start with the default case */
207
208 date_fmt = (opt & DATE_OPT_RFC2822 ?
Eric Andersen39396b92004-04-06 09:38:18 +0000209 (utc ? "%a, %d %b %Y %H:%M:%S GMT" :
210 "%a, %d %b %Y %H:%M:%S %z") :
211 "%a %b %e %H:%M:%S %Z %Y");
Glenn L McGrath03195fc2002-08-23 05:58:38 +0000212
Rob Landleyc5789a62006-02-21 05:06:42 +0000213 if (ENABLE_FEATURE_DATE_ISOFMT) {
214 if (ifmt == 4)
215 date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
216 else if (ifmt == 3)
217 date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
218 else if (ifmt == 2)
219 date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
220 else if (ifmt == 1)
221 date_fmt = "%Y-%m-%d";
Robert Griebldf039322002-07-30 23:11:00 +0000222 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223 }
Rob Landleyc5789a62006-02-21 05:06:42 +0000224
225 if (*date_fmt == '\0') {
Eric Andersencc8ed391999-10-05 16:24:54 +0000226
Rob Landleyc5789a62006-02-21 05:06:42 +0000227 /* With no format string, just print a blank line */
228
229 *bb_common_bufsiz1=0;
230 } else {
Eric Andersencc8ed391999-10-05 16:24:54 +0000231
Rob Landleyc5789a62006-02-21 05:06:42 +0000232 /* Handle special conversions */
Eric Andersencc8ed391999-10-05 16:24:54 +0000233
Rob Landleyc5789a62006-02-21 05:06:42 +0000234 if (strncmp(date_fmt, "%f", 2) == 0) {
235 date_fmt = "%Y.%m.%d-%H:%M:%S";
236 }
237
238 /* Generate output string */
"Vladimir N. Oleynik"a2eec602005-10-15 13:45:32 +0000239 strftime(bb_common_bufsiz1, 200, date_fmt, &tm_time);
Rob Landleyf7662da2005-05-28 23:55:26 +0000240 }
Rob Landleyc5789a62006-02-21 05:06:42 +0000241 puts(bb_common_bufsiz1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000242
Matt Kraai3e856ce2000-12-01 02:55:13 +0000243 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000244}