Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini date implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * by Matthew Grant <grantma@anathoth.gen.nz> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 7 | * iso-format handling added by Robert Griebl <griebl@gmx.de> |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 8 | * bugfixes and cleanup by Bernhard Fischer |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 9 | * |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 13 | #include "busybox.h" |
| 14 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 15 | /* This 'date' command supports only 2 time setting formats, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 16 | all the GNU strftime stuff (its in libc, lets use it), |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 17 | setting time using UTC and displaying it, as well as |
| 18 | an RFC 2822 compliant date output for shell scripting |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 19 | mail commands */ |
| 20 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 21 | /* 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 Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 24 | /* Default input handling to save surprising some people */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | |
Rob Landley | 91ed1a4 | 2006-07-14 17:59:36 +0000 | [diff] [blame] | 26 | |
| 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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 35 | static void maybe_set_utc(int opt) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 37 | if ((opt & DATE_OPT_UTC) && putenv("TZ=UTC0") != 0) |
| 38 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | int date_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 42 | { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 43 | time_t tm; |
| 44 | struct tm tm_time; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 45 | unsigned opt; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 46 | int ifmt = -1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | char *date_str = NULL; |
| 48 | char *date_fmt = NULL; |
Glenn L McGrath | 01cdb66 | 2004-02-17 12:22:21 +0000 | [diff] [blame] | 49 | char *filename = NULL; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 50 | char *isofmt_arg; |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 51 | char *hintfmt_arg; |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 53 | opt_complementary = "?:d--s:s--d" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 54 | USE_FEATURE_DATE_ISOFMT(":R--I:I--R"); |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 55 | opt = getopt32(argc, argv, "Rs:ud:r:" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 56 | USE_FEATURE_DATE_ISOFMT("I::D:"), |
Glenn L McGrath | 01cdb66 | 2004-02-17 12:22:21 +0000 | [diff] [blame] | 57 | &date_str, &date_str, &filename |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 58 | USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg)); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 59 | maybe_set_utc(opt); |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 60 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 61 | if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) { |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 62 | if (!isofmt_arg) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 63 | ifmt = 0; /* default is date */ |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 64 | } else { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 65 | 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 Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 70 | break; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 71 | } |
| 72 | if (ifmt == 4) /* parse error */ |
| 73 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | } |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 76 | |
"Vladimir N. Oleynik" | 3ade65f | 2005-10-12 14:36:42 +0000 | [diff] [blame] | 77 | /* XXX, date_fmt == NULL from this always */ |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 78 | 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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 81 | opt |= DATE_OPT_SET; |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 82 | date_str = argv[optind]; |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 83 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 84 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 85 | /* Now we have parsed all the information except the date format |
| 86 | which depends on whether the clock is being set or read */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 88 | if (filename) { |
Glenn L McGrath | 01cdb66 | 2004-02-17 12:22:21 +0000 | [diff] [blame] | 89 | struct stat statbuf; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 90 | xstat(filename, &statbuf); |
| 91 | tm = statbuf.st_mtime; |
| 92 | } else |
| 93 | time(&tm); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 94 | 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 101 | /* Process any date input to UNIX time since 1 Jan 1970 */ |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 102 | 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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 105 | /* 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 137 | } else { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 138 | 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 163 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 164 | |
Eric Andersen | 77d9268 | 2001-05-23 20:32:09 +0000 | [diff] [blame] | 165 | /* Correct any day of week and day of year etc. fields */ |
Manuel Novoa III | b511f9c | 2003-07-23 23:24:31 +0000 | [diff] [blame] | 166 | tm_time.tm_isdst = -1; /* Be sure to recheck dst. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 167 | tm = mktime(&tm_time); |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 168 | if (tm < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 169 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 170 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 171 | maybe_set_utc(opt); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 172 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 173 | /* if setting time, set it */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 174 | if ((opt & DATE_OPT_SET) && stime(&tm) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 175 | bb_perror_msg("cannot set date"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 178 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 179 | /* Display output */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 180 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 181 | /* Deal with format string */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 182 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 183 | if (date_fmt == NULL) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 184 | 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 | } |
| 203 | format_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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 213 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 214 | |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 215 | if (*date_fmt == '\0') { |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 216 | /* With no format string, just print a blank line */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 217 | *bb_common_bufsiz1 = 0; |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 218 | } else { |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 219 | /* Handle special conversions */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 220 | |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 221 | 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" | a2eec60 | 2005-10-15 13:45:32 +0000 | [diff] [blame] | 226 | strftime(bb_common_bufsiz1, 200, date_fmt, &tm_time); |
Rob Landley | f7662da | 2005-05-28 23:55:26 +0000 | [diff] [blame] | 227 | } |
Rob Landley | c5789a6 | 2006-02-21 05:06:42 +0000 | [diff] [blame] | 228 | puts(bb_common_bufsiz1); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 229 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 230 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 231 | } |