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 | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | #include "internal.h" |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 24 | #define BB_DECLARE_EXTERN |
| 25 | #define bb_need_invalid_date |
| 26 | #define bb_need_memory_exhausted |
| 27 | #include "messages.c" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <unistd.h> |
| 32 | #include <time.h> |
| 33 | #include <stdio.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 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | static const char date_usage[] = "date [OPTION]... [+FORMAT]\n" |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 43 | " or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n" |
| 44 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 45 | "\nDisplays the current time in the given FORMAT, or sets the system date.\n" |
| 46 | "\nOptions:\n\t-R\tOutputs RFC-822 compliant date string\n" |
| 47 | "\t-s\tSets time described by STRING\n" |
| 48 | "\t-u\tPrints or sets Coordinated Universal Time\n" |
| 49 | #endif |
| 50 | ; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 51 | |
| 52 | |
| 53 | /* Input parsing code is always bulky - used heavy duty libc stuff as |
| 54 | much as possible, missed out a lot of bounds checking */ |
| 55 | |
| 56 | /* Default input handling to save suprising some people */ |
| 57 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | struct tm *date_conv_time(struct tm *tm_time, const char *t_string) |
| 59 | { |
| 60 | int nr; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 61 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | nr = sscanf(t_string, "%2d%2d%2d%2d%d", |
| 63 | &(tm_time->tm_mon), |
| 64 | &(tm_time->tm_mday), |
| 65 | &(tm_time->tm_hour), |
| 66 | &(tm_time->tm_min), &(tm_time->tm_year)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 67 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 68 | if (nr < 4 || nr > 5) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 69 | fatalError(invalid_date, t_string); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 71 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | /* correct for century - minor Y2K problem here? */ |
| 73 | if (tm_time->tm_year >= 1900) |
| 74 | tm_time->tm_year -= 1900; |
| 75 | /* adjust date */ |
| 76 | tm_time->tm_mon -= 1; |
| 77 | |
| 78 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 79 | |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /* The new stuff for LRP */ |
| 84 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 85 | struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string) |
| 86 | { |
| 87 | struct tm itm_time, jtm_time, ktm_time, ltm_time, mtm_time, ntm_time; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 88 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | itm_time = *tm_time; |
| 90 | jtm_time = *tm_time; |
| 91 | ktm_time = *tm_time; |
| 92 | ltm_time = *tm_time; |
| 93 | mtm_time = *tm_time; |
| 94 | ntm_time = *tm_time; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 95 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | /* Parse input and assign appropriately to tm_time */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 97 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | if (sscanf(t_string, "%d:%d:%d", |
| 99 | &itm_time.tm_hour, &itm_time.tm_min, &itm_time.tm_sec) == 3) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 101 | *tm_time = itm_time; |
| 102 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 103 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | } else if (sscanf(t_string, "%d:%d", |
| 105 | &jtm_time.tm_hour, &jtm_time.tm_min) == 2) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 106 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 107 | *tm_time = jtm_time; |
| 108 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 109 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | } else if (sscanf(t_string, "%d.%d-%d:%d:%d", |
| 111 | &ktm_time.tm_mon, |
| 112 | &ktm_time.tm_mday, |
| 113 | &ktm_time.tm_hour, |
| 114 | &ktm_time.tm_min, &ktm_time.tm_sec) == 5) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 115 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 116 | ktm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
| 117 | *tm_time = ktm_time; |
| 118 | return (tm_time); |
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 | } else if (sscanf(t_string, "%d.%d-%d:%d", |
| 121 | <m_time.tm_mon, |
| 122 | <m_time.tm_mday, |
| 123 | <m_time.tm_hour, <m_time.tm_min) == 4) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 124 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 125 | ltm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
| 126 | *tm_time = ltm_time; |
| 127 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 128 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 129 | } else if (sscanf(t_string, "%d.%d.%d-%d:%d:%d", |
| 130 | &mtm_time.tm_year, |
| 131 | &mtm_time.tm_mon, |
| 132 | &mtm_time.tm_mday, |
| 133 | &mtm_time.tm_hour, |
| 134 | &mtm_time.tm_min, &mtm_time.tm_sec) == 6) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 135 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 136 | mtm_time.tm_year -= 1900; /* Adjust years */ |
| 137 | mtm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
| 138 | *tm_time = mtm_time; |
| 139 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 140 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 141 | } else if (sscanf(t_string, "%d.%d.%d-%d:%d", |
| 142 | &ntm_time.tm_year, |
| 143 | &ntm_time.tm_mon, |
| 144 | &ntm_time.tm_mday, |
| 145 | &ntm_time.tm_hour, &ntm_time.tm_min) == 5) { |
| 146 | ntm_time.tm_year -= 1900; /* Adjust years */ |
| 147 | ntm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
| 148 | *tm_time = ntm_time; |
| 149 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 150 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 153 | fatalError(invalid_date, t_string); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | int date_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 158 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | char *date_str = NULL; |
| 160 | char *date_fmt = NULL; |
| 161 | char *t_buff; |
| 162 | int i; |
| 163 | int set_time = 0; |
| 164 | int rfc822 = 0; |
| 165 | int utc = 0; |
| 166 | int use_arg = 0; |
| 167 | time_t tm; |
| 168 | struct tm tm_time; |
| 169 | |
| 170 | /* Interpret command line args */ |
| 171 | i = --argc; |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 172 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 173 | while (i > 0 && **argv) { |
| 174 | if (**argv == '-') { |
| 175 | while (i > 0 && *++(*argv)) |
| 176 | switch (**argv) { |
| 177 | case 'R': |
| 178 | rfc822 = 1; |
| 179 | break; |
| 180 | case 's': |
| 181 | set_time = 1; |
| 182 | if (date_str != NULL) |
| 183 | usage(date_usage); |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 184 | date_str = *argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 185 | break; |
| 186 | case 'u': |
| 187 | utc = 1; |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 188 | if (putenv("TZ=UTC0") != 0) |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 189 | fatalError(memory_exhausted); |
Eric Andersen | 1f6262b | 2000-06-06 22:17:43 +0000 | [diff] [blame] | 190 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | case 'd': |
| 192 | use_arg = 1; |
| 193 | if (date_str != NULL) |
| 194 | usage(date_usage); |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 195 | date_str = *argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 196 | break; |
| 197 | case '-': |
| 198 | usage(date_usage); |
| 199 | } |
| 200 | } else { |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 201 | if ((date_fmt == NULL) && (**argv == '+')) |
| 202 | date_fmt = *argv + 1; /* Skip over the '+' */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 203 | else if (date_str == NULL) { |
| 204 | set_time = 1; |
| 205 | date_str = *argv; |
| 206 | } else { |
| 207 | usage(date_usage); |
| 208 | } |
| 209 | } |
| 210 | i--; |
| 211 | argv++; |
| 212 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 213 | |
| 214 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 215 | /* Now we have parsed all the information except the date format |
| 216 | which depends on whether the clock is being set or read */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 217 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 218 | time(&tm); |
| 219 | memcpy(&tm_time, localtime(&tm), sizeof(tm_time)); |
| 220 | /* Zero out fields - take her back to midnight! */ |
| 221 | if (date_str != NULL) { |
| 222 | tm_time.tm_sec = 0; |
| 223 | tm_time.tm_min = 0; |
| 224 | tm_time.tm_hour = 0; |
| 225 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 226 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 227 | /* Process any date input to UNIX time since 1 Jan 1970 */ |
| 228 | if (date_str != NULL) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 229 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 230 | if (strchr(date_str, ':') != NULL) { |
| 231 | date_conv_ftime(&tm_time, date_str); |
| 232 | } else { |
| 233 | date_conv_time(&tm_time, date_str); |
| 234 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 235 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 236 | /* Correct any day of week and day of year etc fields */ |
| 237 | tm = mktime(&tm_time); |
Erik Andersen | 1ad302a | 2000-03-24 00:54:46 +0000 | [diff] [blame] | 238 | if (tm < 0) |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 239 | fatalError(invalid_date, date_str); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 240 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 241 | /* if setting time, set it */ |
| 242 | if (set_time) { |
| 243 | if (stime(&tm) < 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 244 | fatalError("can't set date.\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 248 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 249 | /* Display output */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 250 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 251 | /* Deal with format string */ |
| 252 | if (date_fmt == NULL) { |
| 253 | date_fmt = (rfc822 |
| 254 | ? (utc |
| 255 | ? "%a, %_d %b %Y %H:%M:%S GMT" |
| 256 | : "%a, %_d %b %Y %H:%M:%S %z") |
| 257 | : "%a %b %e %H:%M:%S %Z %Y"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 258 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 259 | } else if (*date_fmt == '\0') { |
| 260 | /* Imitate what GNU 'date' does with NO format string! */ |
| 261 | printf("\n"); |
| 262 | exit(TRUE); |
| 263 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 264 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 265 | /* Handle special conversions */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 266 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 267 | if (strncmp(date_fmt, "%f", 2) == 0) { |
| 268 | date_fmt = "%Y.%m.%d-%H:%M:%S"; |
| 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 | /* Print OUTPUT (after ALL that!) */ |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 272 | t_buff = xmalloc(201); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 273 | strftime(t_buff, 200, date_fmt, &tm_time); |
| 274 | printf("%s\n", t_buff); |
| 275 | |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 276 | return(TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 277 | } |