summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/date.c104
-rw-r--r--include/usage.h26
2 files changed, 68 insertions, 62 deletions
diff --git a/coreutils/date.c b/coreutils/date.c
index a8e3393..064f758 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -39,70 +39,70 @@ static void maybe_set_utc(int opt)
}
int date_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int date_main(int argc, char **argv)
+int date_main(int argc ATTRIBUTE_UNUSED, char **argv)
{
- time_t tm;
struct tm tm_time;
+ time_t tm;
unsigned opt;
int ifmt = -1;
- char *date_str = NULL;
- char *date_fmt = NULL;
- char *filename = NULL;
- char *isofmt_arg;
- char *hintfmt_arg;
+ char *date_str;
+ char *fmt_dt2str;
+ char *fmt_str2dt;
+ char *filename;
+ char *isofmt_arg = NULL;
opt_complementary = "d--s:s--d"
USE_FEATURE_DATE_ISOFMT(":R--I:I--R");
opt = getopt32(argv, "Rs:ud:r:"
USE_FEATURE_DATE_ISOFMT("I::D:"),
&date_str, &date_str, &filename
- USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg));
+ USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt));
+ argv += optind;
maybe_set_utc(opt);
if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
- if (!isofmt_arg) {
- ifmt = 0; /* default is date */
- } else {
- static const char *const isoformats[] = {
- "date", "hours", "minutes", "seconds"
- };
-
- for (ifmt = 0; ifmt < 4; ifmt++)
- if (!strcmp(isofmt_arg, isoformats[ifmt]))
- goto found;
- /* parse error */
- bb_show_usage();
- found: ;
+ ifmt = 0; /* default is date */
+ if (isofmt_arg) {
+ static const char isoformats[] ALIGN1 =
+ "date\0""hours\0""minutes\0""seconds\0";
+ ifmt = index_in_strings(isoformats, isofmt_arg);
+ if (ifmt < 0)
+ bb_show_usage();
}
}
- /* XXX, date_fmt == NULL from this always */
- if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
- date_fmt = &argv[optind][1]; /* Skip over the '+' */
- } else if (date_str == NULL) {
+ fmt_dt2str = NULL;
+ if (argv[0] && argv[0][0] == '+') {
+ fmt_dt2str = &argv[0][1]; /* Skip over the '+' */
+ argv++;
+ }
+ if (!(opt & (DATE_OPT_SET | DATE_OPT_DATE))) {
opt |= DATE_OPT_SET;
- date_str = argv[optind];
+ date_str = argv[0]; /* can be NULL */
}
/* Now we have parsed all the information except the date format
which depends on whether the clock is being set or read */
- if (filename) {
+ if (opt & DATE_OPT_REFERENCE) {
struct stat statbuf;
xstat(filename, &statbuf);
tm = statbuf.st_mtime;
} else
time(&tm);
memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
- /* Zero out fields - take her back to midnight! */
+
+ /* If date string is given, update tm_time, and maybe set date */
if (date_str != NULL) {
+ /* Zero out fields - take her back to midnight! */
tm_time.tm_sec = 0;
tm_time.tm_min = 0;
tm_time.tm_hour = 0;
/* Process any date input to UNIX time since 1 Jan 1970 */
if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) {
- strptime(date_str, hintfmt_arg, &tm_time);
+ if (strptime(date_str, fmt_str2dt, &tm_time) == NULL)
+ bb_error_msg_and_die(bb_msg_invalid_date, date_str);
} else if (strchr(date_str, ':') != NULL) {
/* Parse input and assign appropriately to tm_time */
@@ -181,55 +181,53 @@ int date_main(int argc, char **argv)
/* Display output */
/* Deal with format string */
-
- if (date_fmt == NULL) {
+ if (fmt_dt2str == NULL) {
int i;
- date_fmt = xzalloc(32);
+ fmt_dt2str = xzalloc(32);
if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) {
- strcpy(date_fmt, "%Y-%m-%d");
+ strcpy(fmt_dt2str, "%Y-%m-%d");
if (ifmt > 0) {
i = 8;
- date_fmt[i++] = 'T';
- date_fmt[i++] = '%';
- date_fmt[i++] = 'H';
+ fmt_dt2str[i++] = 'T';
+ fmt_dt2str[i++] = '%';
+ fmt_dt2str[i++] = 'H';
if (ifmt > 1) {
- date_fmt[i++] = ':';
- date_fmt[i++] = '%';
- date_fmt[i++] = 'M';
- }
- if (ifmt > 2) {
- date_fmt[i++] = ':';
- date_fmt[i++] = '%';
- date_fmt[i++] = 'S';
+ fmt_dt2str[i++] = ':';
+ fmt_dt2str[i++] = '%';
+ fmt_dt2str[i++] = 'M';
+ if (ifmt > 2) {
+ fmt_dt2str[i++] = ':';
+ fmt_dt2str[i++] = '%';
+ fmt_dt2str[i++] = 'S';
+ }
}
format_utc:
- date_fmt[i++] = '%';
- date_fmt[i] = (opt & DATE_OPT_UTC) ? 'Z' : 'z';
+ fmt_dt2str[i++] = '%';
+ fmt_dt2str[i] = (opt & DATE_OPT_UTC) ? 'Z' : 'z';
}
} else if (opt & DATE_OPT_RFC2822) {
/* Undo busybox.c for date -R */
if (ENABLE_LOCALE_SUPPORT)
setlocale(LC_TIME, "C");
- strcpy(date_fmt, "%a, %d %b %Y %H:%M:%S ");
+ strcpy(fmt_dt2str, "%a, %d %b %Y %H:%M:%S ");
i = 22;
goto format_utc;
} else /* default case */
- date_fmt = (char*)"%a %b %e %H:%M:%S %Z %Y";
+ fmt_dt2str = (char*)"%a %b %e %H:%M:%S %Z %Y";
}
#define date_buf bb_common_bufsiz1
- if (*date_fmt == '\0') {
+ if (*fmt_dt2str == '\0') {
/* With no format string, just print a blank line */
date_buf[0] = '\0';
} else {
/* Handle special conversions */
-
- if (strncmp(date_fmt, "%f", 2) == 0) {
- date_fmt = (char*)"%Y.%m.%d-%H:%M:%S";
+ if (strncmp(fmt_dt2str, "%f", 2) == 0) {
+ fmt_dt2str = (char*)"%Y.%m.%d-%H:%M:%S";
}
/* Generate output string */
- strftime(date_buf, sizeof(date_buf), date_fmt, &tm_time);
+ strftime(date_buf, sizeof(date_buf), fmt_dt2str, &tm_time);
}
puts(date_buf);
diff --git a/include/usage.h b/include/usage.h
index 450f801..e7e9002 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -558,22 +558,30 @@
"world\n"
#define date_trivial_usage \
- "[OPTION]... [MMDDhhmm[[CC]YY][.ss]] [+FORMAT]"
+ "[OPTION]... [+FMT] [TIME]"
#define date_full_usage \
- "Display current time in the given FORMAT, or set system date\n" \
+ "Display time (using +FMT), or set time\n" \
"\nOptions:" \
+ "\n -u Work in UTC (don't convert to local time)" \
"\n -R Output RFC-822 compliant date string" \
- "\n -d STRING Display time described by STRING, not 'now'" \
USE_FEATURE_DATE_ISOFMT( \
- "\n -I[TIMESPEC] Output an ISO-8601 compliant date/time string" \
- "\n TIMESPEC='date' (or missing) for date only," \
+ "\n -I[SPEC] Output ISO-8601 compliant date string" \
+ "\n SPEC='date' (default) for date only," \
"\n 'hours', 'minutes', or 'seconds' for date and" \
"\n time to the indicated precision" \
- "\n -D hint Use 'hint' as date format, via strptime()" \
) \
- "\n -s STRING Set time described by STRING" \
- "\n -r FILE Display the last modification time of FILE" \
- "\n -u Print or sets Coordinated Universal Time" \
+ "\n -d TIME Display TIME, not 'now'" \
+ "\n -r FILE Display last modification time of FILE" \
+ "\n [-s] TIME Set time to TIME" \
+ USE_FEATURE_DATE_ISOFMT( \
+ "\n -D FMT Use FMT for str->date conversion" \
+ ) \
+ "\n" \
+ "\nRecognized formats for TIME:" \
+ "\n [hh:]mm:ss" \
+ "\n mon.day-hh:mm[:ss]" \
+ "\n year.mon.day-hh:mm[:ss]" \
+ "\n MMDDhhmm[[YY]YY][.ss]" \
#define date_example_usage \
"$ date\n" \