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> |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 6 | * |
| 7 | * iso-format handling added by Robert Griebl <griebl@gmx.de> |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 8 | * |
| 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | #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 Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 31 | #include <string.h> |
Eric Andersen | 88f50b6 | 2000-08-10 17:59:11 +0000 | [diff] [blame] | 32 | #include <getopt.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 33 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | /* This 'date' command supports only 2 time setting formats, |
| 37 | 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 42 | /* Input parsing code is always bulky - used heavy duty libc stuff as |
| 43 | much as possible, missed out a lot of bounds checking */ |
| 44 | |
| 45 | /* Default input handling to save suprising some people */ |
| 46 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 47 | static struct tm *date_conv_time(struct tm *tm_time, const char *t_string) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | { |
| 49 | int nr; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 50 | |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 51 | 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | if (nr < 4 || nr > 5) { |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 56 | error_msg_and_die(invalid_date, t_string); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 58 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | /* correct for century - minor Y2K problem here? */ |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 60 | if (tm_time->tm_year >= 1900) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | tm_time->tm_year -= 1900; |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 62 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | /* adjust date */ |
| 64 | tm_time->tm_mon -= 1; |
| 65 | |
| 66 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 67 | |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* The new stuff for LRP */ |
| 72 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 73 | static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | { |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 75 | struct tm t; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 76 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | /* Parse input and assign appropriately to tm_time */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 78 | |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 79 | if (t = |
| 80 | *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min, |
| 81 | &t.tm_sec) == 3) { |
| 82 | /* no adjustments needed */ |
| 83 | } else if (t = |
| 84 | *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour, |
| 85 | &t.tm_min) == 2) { |
| 86 | /* no adjustments needed */ |
| 87 | } else if (t = |
| 88 | *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon, |
| 89 | &t.tm_mday, &t.tm_hour, &t.tm_min, |
| 90 | &t.tm_sec) == 5) { |
| 91 | /* Adjust dates from 1-12 to 0-11 */ |
| 92 | t.tm_mon -= 1; |
| 93 | } else if (t = |
| 94 | *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon, |
| 95 | &t.tm_mday, &t.tm_hour, &t.tm_min) == 4) { |
| 96 | /* Adjust dates from 1-12 to 0-11 */ |
| 97 | t.tm_mon -= 1; |
| 98 | } else if (t = |
| 99 | *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year, |
| 100 | &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, |
| 101 | &t.tm_sec) == 6) { |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 102 | t.tm_year -= 1900; /* Adjust years */ |
| 103 | t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 104 | } else if (t = |
| 105 | *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year, |
| 106 | &t.tm_mon, &t.tm_mday, &t.tm_hour, |
| 107 | &t.tm_min) == 5) { |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 108 | t.tm_year -= 1900; /* Adjust years */ |
| 109 | t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 110 | } else { |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 111 | error_msg_and_die(invalid_date, t_string); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 112 | } |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 113 | *tm_time = t; |
| 114 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | int date_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 119 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | char *date_str = NULL; |
| 121 | char *date_fmt = NULL; |
| 122 | char *t_buff; |
Pavel Roskin | 47d4926 | 2000-07-17 16:17:19 +0000 | [diff] [blame] | 123 | int c; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 124 | int set_time = 0; |
| 125 | int rfc822 = 0; |
| 126 | int utc = 0; |
| 127 | int use_arg = 0; |
| 128 | time_t tm; |
| 129 | struct tm tm_time; |
| 130 | |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 131 | #ifdef CONFIG_FEATURE_DATE_ISOFMT |
| 132 | int ifmt = 0; |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 133 | |
| 134 | # define GETOPT_ISOFMT "I::" |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 135 | #else |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 136 | # define GETOPT_ISOFMT |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 137 | #endif |
| 138 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 139 | /* Interpret command line args */ |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 140 | while ((c = getopt(argc, argv, "Rs:ud:" GETOPT_ISOFMT)) != EOF) { |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 141 | switch (c) { |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 142 | case 'R': |
| 143 | rfc822 = 1; |
| 144 | break; |
| 145 | case 's': |
| 146 | set_time = 1; |
| 147 | if ((date_str != NULL) || ((date_str = optarg) == NULL)) { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 148 | show_usage(); |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 149 | } |
| 150 | break; |
| 151 | case 'u': |
| 152 | utc = 1; |
| 153 | if (putenv("TZ=UTC0") != 0) |
| 154 | error_msg_and_die(memory_exhausted); |
| 155 | break; |
| 156 | case 'd': |
| 157 | use_arg = 1; |
| 158 | if ((date_str != NULL) || ((date_str = optarg) == NULL)) |
| 159 | show_usage(); |
| 160 | break; |
| 161 | #ifdef CONFIG_FEATURE_DATE_ISOFMT |
| 162 | case 'I': |
| 163 | if (!optarg) |
| 164 | ifmt = 1; |
| 165 | else { |
| 166 | int ifmt_len = xstrlen(optarg); |
| 167 | |
| 168 | if ((ifmt_len <= 4) |
| 169 | && (strncmp(optarg, "date", ifmt_len) == 0)) { |
| 170 | ifmt = 1; |
| 171 | } else if ((ifmt_len <= 5) |
| 172 | && (strncmp(optarg, "hours", ifmt_len) == 0)) { |
| 173 | ifmt = 2; |
| 174 | } else if ((ifmt_len <= 7) |
| 175 | && (strncmp(optarg, "minutes", ifmt_len) == 0)) { |
| 176 | ifmt = 3; |
| 177 | } else if ((ifmt_len <= 7) |
| 178 | && (strncmp(optarg, "seconds", ifmt_len) == 0)) { |
| 179 | ifmt = 4; |
| 180 | } |
| 181 | } |
| 182 | if (ifmt) { |
| 183 | break; /* else show_usage(); */ |
| 184 | } |
| 185 | #endif |
| 186 | default: |
| 187 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 188 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 189 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 190 | |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 191 | |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 192 | if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) { |
| 193 | date_fmt = &argv[optind][1]; /* Skip over the '+' */ |
| 194 | } else if (date_str == NULL) { |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 195 | set_time = 1; |
| 196 | date_str = argv[optind]; |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 197 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 198 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 199 | /* Now we have parsed all the information except the date format |
| 200 | which depends on whether the clock is being set or read */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 201 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 202 | time(&tm); |
| 203 | memcpy(&tm_time, localtime(&tm), sizeof(tm_time)); |
| 204 | /* Zero out fields - take her back to midnight! */ |
| 205 | if (date_str != NULL) { |
| 206 | tm_time.tm_sec = 0; |
| 207 | tm_time.tm_min = 0; |
| 208 | tm_time.tm_hour = 0; |
| 209 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 210 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 211 | /* Process any date input to UNIX time since 1 Jan 1970 */ |
| 212 | if (date_str != NULL) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 213 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 214 | if (strchr(date_str, ':') != NULL) { |
| 215 | date_conv_ftime(&tm_time, date_str); |
| 216 | } else { |
| 217 | date_conv_time(&tm_time, date_str); |
| 218 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 219 | |
Eric Andersen | 77d9268 | 2001-05-23 20:32:09 +0000 | [diff] [blame] | 220 | /* Correct any day of week and day of year etc. fields */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 221 | tm = mktime(&tm_time); |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 222 | if (tm < 0) { |
| 223 | error_msg_and_die(invalid_date, date_str); |
| 224 | } |
| 225 | if (utc && (putenv("TZ=UTC0") != 0)) { |
| 226 | error_msg_and_die(memory_exhausted); |
Eric Andersen | a683ee8 | 2000-11-17 18:51:45 +0000 | [diff] [blame] | 227 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 228 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 229 | /* if setting time, set it */ |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 230 | if (set_time && (stime(&tm) < 0)) { |
| 231 | perror_msg("cannot set date"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 234 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 235 | /* Display output */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 236 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 237 | /* Deal with format string */ |
| 238 | if (date_fmt == NULL) { |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 239 | #ifdef CONFIG_FEATURE_DATE_ISOFMT |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 240 | switch (ifmt) { |
| 241 | case 4: |
| 242 | date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z"; |
| 243 | break; |
| 244 | case 3: |
| 245 | date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z"; |
| 246 | break; |
| 247 | case 2: |
| 248 | date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z"; |
| 249 | break; |
| 250 | case 1: |
| 251 | date_fmt = "%Y-%m-%d"; |
| 252 | break; |
| 253 | case 0: |
| 254 | default: |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 255 | #endif |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 256 | date_fmt = |
| 257 | (rfc822 |
| 258 | ? (utc ? "%a, %e %b %Y %H:%M:%S GMT" : |
| 259 | "%a, %e %b %Y %H:%M:%S %z") : "%a %b %e %H:%M:%S %Z %Y"); |
| 260 | |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 261 | #ifdef CONFIG_FEATURE_DATE_ISOFMT |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 262 | break; |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 263 | } |
| 264 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 265 | } else if (*date_fmt == '\0') { |
| 266 | /* Imitate what GNU 'date' does with NO format string! */ |
| 267 | printf("\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 268 | return EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 269 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 270 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 271 | /* Handle special conversions */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 272 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 273 | if (strncmp(date_fmt, "%f", 2) == 0) { |
| 274 | date_fmt = "%Y.%m.%d-%H:%M:%S"; |
| 275 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 276 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 277 | /* Print OUTPUT (after ALL that!) */ |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 278 | t_buff = xmalloc(201); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 279 | strftime(t_buff, 200, date_fmt, &tm_time); |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 280 | puts(t_buff); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 281 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 282 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 283 | } |