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> |
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 | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | |
| 34 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 35 | /* This 'date' command supports only 2 time setting formats, |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 41 | /* 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 Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 44 | /* Default input handling to save surprising some people */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 45 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 46 | 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] | 47 | { |
| 48 | int nr; |
Eric Andersen | 9315842 | 2004-10-11 20:52:16 +0000 | [diff] [blame] | 49 | char *cp; |
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) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | bb_error_msg_and_die(bb_msg_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 | |
Eric Andersen | 9315842 | 2004-10-11 20:52:16 +0000 | [diff] [blame] | 59 | 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 Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | /* correct for century - minor Y2K problem here? */ |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 68 | if (tm_time->tm_year >= 1900) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | tm_time->tm_year -= 1900; |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 70 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | /* adjust date */ |
| 72 | tm_time->tm_mon -= 1; |
| 73 | |
| 74 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 75 | |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /* The new stuff for LRP */ |
| 80 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 81 | 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] | 82 | { |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 83 | struct tm t; |
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 | /* Parse input and assign appropriately to tm_time */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 86 | |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 87 | if (t = |
| 88 | *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min, |
| 89 | &t.tm_sec) == 3) { |
| 90 | /* no adjustments needed */ |
| 91 | } else if (t = |
| 92 | *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour, |
| 93 | &t.tm_min) == 2) { |
| 94 | /* no adjustments needed */ |
| 95 | } else if (t = |
| 96 | *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon, |
| 97 | &t.tm_mday, &t.tm_hour, &t.tm_min, |
| 98 | &t.tm_sec) == 5) { |
| 99 | /* Adjust dates from 1-12 to 0-11 */ |
| 100 | t.tm_mon -= 1; |
| 101 | } else if (t = |
| 102 | *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon, |
| 103 | &t.tm_mday, &t.tm_hour, &t.tm_min) == 4) { |
| 104 | /* Adjust dates from 1-12 to 0-11 */ |
| 105 | t.tm_mon -= 1; |
| 106 | } else if (t = |
| 107 | *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year, |
| 108 | &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, |
| 109 | &t.tm_sec) == 6) { |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 110 | t.tm_year -= 1900; /* Adjust years */ |
| 111 | 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] | 112 | } else if (t = |
| 113 | *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year, |
| 114 | &t.tm_mon, &t.tm_mday, &t.tm_hour, |
| 115 | &t.tm_min) == 5) { |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 116 | t.tm_year -= 1900; /* Adjust years */ |
| 117 | t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 118 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 119 | bb_error_msg_and_die(bb_msg_invalid_date, t_string); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | } |
Eric Andersen | 7b5d594 | 2000-11-29 22:01:42 +0000 | [diff] [blame] | 121 | *tm_time = t; |
| 122 | return (tm_time); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 125 | #define DATE_OPT_RFC2822 0x01 |
| 126 | #define DATE_OPT_SET 0x02 |
| 127 | #define DATE_OPT_UTC 0x04 |
| 128 | #define DATE_OPT_DATE 0x08 |
Glenn L McGrath | 01cdb66 | 2004-02-17 12:22:21 +0000 | [diff] [blame] | 129 | #define DATE_OPT_REFERENCE 0x10 |
Glenn L McGrath | c2266bd | 2004-02-17 07:58:04 +0000 | [diff] [blame] | 130 | #ifdef CONFIG_FEATURE_DATE_ISOFMT |
Glenn L McGrath | 01cdb66 | 2004-02-17 12:22:21 +0000 | [diff] [blame] | 131 | # define DATE_OPT_TIMESPEC 0x20 |
Glenn L McGrath | c2266bd | 2004-02-17 07:58:04 +0000 | [diff] [blame] | 132 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 133 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 134 | int date_main(int argc, char **argv) |
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 | char *date_str = NULL; |
| 137 | char *date_fmt = NULL; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 138 | int set_time; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 139 | int utc; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 140 | int use_arg = 0; |
| 141 | time_t tm; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 142 | unsigned long opt; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 143 | struct tm tm_time; |
Glenn L McGrath | 01cdb66 | 2004-02-17 12:22:21 +0000 | [diff] [blame] | 144 | char *filename = NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 145 | |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 146 | #ifdef CONFIG_FEATURE_DATE_ISOFMT |
| 147 | int ifmt = 0; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 148 | char *isofmt_arg; |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 149 | |
| 150 | # define GETOPT_ISOFMT "I::" |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 151 | #else |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 152 | # define GETOPT_ISOFMT |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 153 | #endif |
"Vladimir N. Oleynik" | 27421a1 | 2005-09-05 14:46:07 +0000 | [diff] [blame] | 154 | bb_opt_complementally = "!d~ds:s~ds"; |
Glenn L McGrath | 01cdb66 | 2004-02-17 12:22:21 +0000 | [diff] [blame] | 155 | opt = bb_getopt_ulflags(argc, argv, "Rs:ud:r:" GETOPT_ISOFMT, |
| 156 | &date_str, &date_str, &filename |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 157 | #ifdef CONFIG_FEATURE_DATE_ISOFMT |
| 158 | , &isofmt_arg |
| 159 | #endif |
| 160 | ); |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 161 | set_time = opt & DATE_OPT_SET; |
| 162 | utc = opt & DATE_OPT_UTC; |
| 163 | if ((utc) && (putenv("TZ=UTC0") != 0)) { |
| 164 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 165 | } |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 166 | use_arg = opt & DATE_OPT_DATE; |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 167 | #ifdef CONFIG_FEATURE_DATE_ISOFMT |
Glenn L McGrath | c2266bd | 2004-02-17 07:58:04 +0000 | [diff] [blame] | 168 | if(opt & DATE_OPT_TIMESPEC) { |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 169 | if (!isofmt_arg) { |
| 170 | ifmt = 1; |
| 171 | } else { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 172 | int ifmt_len = bb_strlen(isofmt_arg); |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 173 | |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 174 | if ((ifmt_len <= 4) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 175 | && (strncmp(isofmt_arg, "date", ifmt_len) == 0)) { |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 176 | ifmt = 1; |
| 177 | } else if ((ifmt_len <= 5) |
| 178 | && (strncmp(isofmt_arg, "hours", ifmt_len) == 0)) { |
| 179 | ifmt = 2; |
| 180 | } else if ((ifmt_len <= 7) |
| 181 | && (strncmp(isofmt_arg, "minutes", ifmt_len) == 0)) { |
| 182 | ifmt = 3; |
| 183 | } else if ((ifmt_len <= 7) |
| 184 | && (strncmp(isofmt_arg, "seconds", ifmt_len) == 0)) { |
| 185 | ifmt = 4; |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 186 | } |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 187 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 188 | if (!ifmt) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 189 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 190 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 192 | #endif |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 193 | |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 194 | if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) { |
| 195 | date_fmt = &argv[optind][1]; /* Skip over the '+' */ |
| 196 | } else if (date_str == NULL) { |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 197 | set_time = 1; |
| 198 | date_str = argv[optind]; |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 199 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 200 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 201 | /* Now we have parsed all the information except the date format |
| 202 | which depends on whether the clock is being set or read */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 203 | |
Glenn L McGrath | 01cdb66 | 2004-02-17 12:22:21 +0000 | [diff] [blame] | 204 | if(filename) { |
| 205 | struct stat statbuf; |
| 206 | if(stat(filename,&statbuf)) |
| 207 | bb_perror_msg_and_die("File '%s' not found.\n",filename); |
| 208 | tm=statbuf.st_mtime; |
| 209 | } else time(&tm); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 210 | memcpy(&tm_time, localtime(&tm), sizeof(tm_time)); |
| 211 | /* Zero out fields - take her back to midnight! */ |
| 212 | if (date_str != NULL) { |
| 213 | tm_time.tm_sec = 0; |
| 214 | tm_time.tm_min = 0; |
| 215 | tm_time.tm_hour = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 216 | |
Glenn L McGrath | 5f11541 | 2004-02-17 07:51:31 +0000 | [diff] [blame] | 217 | /* Process any date input to UNIX time since 1 Jan 1970 */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 218 | if (strchr(date_str, ':') != NULL) { |
| 219 | date_conv_ftime(&tm_time, date_str); |
| 220 | } else { |
| 221 | date_conv_time(&tm_time, date_str); |
| 222 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 223 | |
Eric Andersen | 77d9268 | 2001-05-23 20:32:09 +0000 | [diff] [blame] | 224 | /* Correct any day of week and day of year etc. fields */ |
Manuel Novoa III | b511f9c | 2003-07-23 23:24:31 +0000 | [diff] [blame] | 225 | tm_time.tm_isdst = -1; /* Be sure to recheck dst. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 226 | tm = mktime(&tm_time); |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 227 | if (tm < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 228 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 229 | } |
| 230 | if (utc && (putenv("TZ=UTC0") != 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 231 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | a683ee8 | 2000-11-17 18:51:45 +0000 | [diff] [blame] | 232 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 233 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 234 | /* if setting time, set it */ |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 235 | if (set_time && (stime(&tm) < 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 236 | bb_perror_msg("cannot set date"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 239 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 240 | /* Display output */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 241 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 242 | /* Deal with format string */ |
| 243 | if (date_fmt == NULL) { |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 244 | #ifdef CONFIG_FEATURE_DATE_ISOFMT |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 245 | switch (ifmt) { |
| 246 | case 4: |
| 247 | date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z"; |
| 248 | break; |
| 249 | case 3: |
| 250 | date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z"; |
| 251 | break; |
| 252 | case 2: |
| 253 | date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z"; |
| 254 | break; |
| 255 | case 1: |
| 256 | date_fmt = "%Y-%m-%d"; |
| 257 | break; |
| 258 | case 0: |
| 259 | default: |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 260 | #endif |
Eric Andersen | 39396b9 | 2004-04-06 09:38:18 +0000 | [diff] [blame] | 261 | date_fmt = (opt & DATE_OPT_RFC2822 ? |
| 262 | (utc ? "%a, %d %b %Y %H:%M:%S GMT" : |
| 263 | "%a, %d %b %Y %H:%M:%S %z") : |
| 264 | "%a %b %e %H:%M:%S %Z %Y"); |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 265 | |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 266 | #ifdef CONFIG_FEATURE_DATE_ISOFMT |
Glenn L McGrath | 03195fc | 2002-08-23 05:58:38 +0000 | [diff] [blame] | 267 | break; |
Robert Griebl | df03932 | 2002-07-30 23:11:00 +0000 | [diff] [blame] | 268 | } |
| 269 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 270 | } else if (*date_fmt == '\0') { |
| 271 | /* Imitate what GNU 'date' does with NO format string! */ |
| 272 | printf("\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 273 | return EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 274 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 275 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 276 | /* Handle special conversions */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 277 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 278 | if (strncmp(date_fmt, "%f", 2) == 0) { |
| 279 | date_fmt = "%Y.%m.%d-%H:%M:%S"; |
| 280 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 281 | |
Rob Landley | f7662da | 2005-05-28 23:55:26 +0000 | [diff] [blame] | 282 | { |
| 283 | /* Print OUTPUT (after ALL that!) */ |
| 284 | RESERVE_CONFIG_BUFFER(t_buff, 201); |
| 285 | strftime(t_buff, 200, date_fmt, &tm_time); |
| 286 | puts(t_buff); |
| 287 | RELEASE_CONFIG_BUFFER(t_buff); |
| 288 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 289 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 290 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 291 | } |