aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/date.c238
1 files changed, 112 insertions, 126 deletions
diff --git a/coreutils/date.c b/coreutils/date.c
index a9188a1ce..d68c04a8d 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -48,19 +48,18 @@ static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
{
int nr;
- nr = sscanf(t_string, "%2d%2d%2d%2d%d",
- &(tm_time->tm_mon),
- &(tm_time->tm_mday),
- &(tm_time->tm_hour),
- &(tm_time->tm_min), &(tm_time->tm_year));
+ nr = sscanf(t_string, "%2d%2d%2d%2d%d", &(tm_time->tm_mon),
+ &(tm_time->tm_mday), &(tm_time->tm_hour), &(tm_time->tm_min),
+ &(tm_time->tm_year));
if (nr < 4 || nr > 5) {
- error_msg_and_die(invalid_date, t_string);
+ error_msg_and_die(invalid_date, t_string);
}
/* correct for century - minor Y2K problem here? */
- if (tm_time->tm_year >= 1900)
+ if (tm_time->tm_year >= 1900) {
tm_time->tm_year -= 1900;
+ }
/* adjust date */
tm_time->tm_mon -= 1;
@@ -77,50 +76,39 @@ static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
/* Parse input and assign appropriately to tm_time */
- if (t=*tm_time,sscanf(t_string, "%d:%d:%d",
- &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) {
- /* no adjustments needed */
-
- } else if (t=*tm_time,sscanf(t_string, "%d:%d",
- &t.tm_hour, &t.tm_min) == 2) {
- /* no adjustments needed */
-
-
- } else if (t=*tm_time,sscanf(t_string, "%d.%d-%d:%d:%d",
- &t.tm_mon,
- &t.tm_mday,
- &t.tm_hour,
- &t.tm_min, &t.tm_sec) == 5) {
-
- t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
-
- } else if (t=*tm_time,sscanf(t_string, "%d.%d-%d:%d",
- &t.tm_mon,
- &t.tm_mday,
- &t.tm_hour, &t.tm_min) == 4) {
-
- t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
-
- } else if (t=*tm_time,sscanf(t_string, "%d.%d.%d-%d:%d:%d",
- &t.tm_year,
- &t.tm_mon,
- &t.tm_mday,
- &t.tm_hour,
- &t.tm_min, &t.tm_sec) == 6) {
-
+ if (t =
+ *tm_time, sscanf(t_string, "%d:%d:%d", &t.tm_hour, &t.tm_min,
+ &t.tm_sec) == 3) {
+ /* no adjustments needed */
+ } else if (t =
+ *tm_time, sscanf(t_string, "%d:%d", &t.tm_hour,
+ &t.tm_min) == 2) {
+ /* no adjustments needed */
+ } else if (t =
+ *tm_time, sscanf(t_string, "%d.%d-%d:%d:%d", &t.tm_mon,
+ &t.tm_mday, &t.tm_hour, &t.tm_min,
+ &t.tm_sec) == 5) {
+ /* Adjust dates from 1-12 to 0-11 */
+ t.tm_mon -= 1;
+ } else if (t =
+ *tm_time, sscanf(t_string, "%d.%d-%d:%d", &t.tm_mon,
+ &t.tm_mday, &t.tm_hour, &t.tm_min) == 4) {
+ /* Adjust dates from 1-12 to 0-11 */
+ t.tm_mon -= 1;
+ } else if (t =
+ *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
+ &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min,
+ &t.tm_sec) == 6) {
t.tm_year -= 1900; /* Adjust years */
t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
-
- } else if (t=*tm_time,sscanf(t_string, "%d.%d.%d-%d:%d",
- &t.tm_year,
- &t.tm_mon,
- &t.tm_mday,
- &t.tm_hour, &t.tm_min) == 5) {
+ } else if (t =
+ *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d", &t.tm_year,
+ &t.tm_mon, &t.tm_mday, &t.tm_hour,
+ &t.tm_min) == 5) {
t.tm_year -= 1900; /* Adjust years */
t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
-
} else {
- error_msg_and_die(invalid_date, t_string);
+ error_msg_and_die(invalid_date, t_string);
}
*tm_time = t;
return (tm_time);
@@ -142,70 +130,71 @@ int date_main(int argc, char **argv)
#ifdef CONFIG_FEATURE_DATE_ISOFMT
int ifmt = 0;
-#define GETOPT_ISOFMT "I::"
+
+# define GETOPT_ISOFMT "I::"
#else
-#define GETOPT_ISOFMT
+# define GETOPT_ISOFMT
#endif
/* Interpret command line args */
- while ((c = getopt(argc, argv, "Rs:ud:" GETOPT_ISOFMT )) != EOF) {
+ while ((c = getopt(argc, argv, "Rs:ud:" GETOPT_ISOFMT)) != EOF) {
switch (c) {
- case 'R':
- rfc822 = 1;
- break;
- case 's':
- set_time = 1;
- if ((date_str != NULL) || ((date_str = optarg) == NULL)) {
- show_usage();
- }
- break;
- case 'u':
- utc = 1;
- if (putenv("TZ=UTC0") != 0)
- error_msg_and_die(memory_exhausted);
- break;
- case 'd':
- use_arg = 1;
- if ((date_str != NULL) || ((date_str = optarg) == NULL))
- show_usage();
- break;
+ case 'R':
+ rfc822 = 1;
+ break;
+ case 's':
+ set_time = 1;
+ if ((date_str != NULL) || ((date_str = optarg) == NULL)) {
+ show_usage();
+ }
+ break;
+ case 'u':
+ utc = 1;
+ if (putenv("TZ=UTC0") != 0)
+ error_msg_and_die(memory_exhausted);
+ break;
+ case 'd':
+ use_arg = 1;
+ if ((date_str != NULL) || ((date_str = optarg) == NULL))
+ show_usage();
+ break;
#ifdef CONFIG_FEATURE_DATE_ISOFMT
- case 'I':
- if ( !optarg )
+ case 'I':
+ if (!optarg)
+ ifmt = 1;
+ else {
+ int ifmt_len = xstrlen(optarg);
+
+ if ((ifmt_len <= 4)
+ && (strncmp(optarg, "date", ifmt_len) == 0)) {
ifmt = 1;
- else {
- int ifmt_len = xstrlen ( optarg );
-
- if (( ifmt_len <= 4 ) && ( strncmp ( optarg, "date", ifmt_len ) == 0 ))
- ifmt = 1;
- else if (( ifmt_len <= 5 ) && ( strncmp ( optarg, "hours", ifmt_len ) == 0 ))
- ifmt = 2;
- else if (( ifmt_len <= 7 ) && ( strncmp ( optarg, "minutes", ifmt_len ) == 0 ))
- ifmt = 3;
- else if (( ifmt_len <= 7 ) && ( strncmp ( optarg, "seconds", ifmt_len ) == 0 ))
- ifmt = 4;
- }
- if ( ifmt )
- break; // else show_usage();
+ } else if ((ifmt_len <= 5)
+ && (strncmp(optarg, "hours", ifmt_len) == 0)) {
+ ifmt = 2;
+ } else if ((ifmt_len <= 7)
+ && (strncmp(optarg, "minutes", ifmt_len) == 0)) {
+ ifmt = 3;
+ } else if ((ifmt_len <= 7)
+ && (strncmp(optarg, "seconds", ifmt_len) == 0)) {
+ ifmt = 4;
+ }
+ }
+ if (ifmt) {
+ break; /* else show_usage(); */
+ }
#endif
- default:
- show_usage();
+ default:
+ show_usage();
}
}
- if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
- date_fmt = &argv[optind][1]; /* Skip over the '+' */
- else if (date_str == NULL) {
+ if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
+ date_fmt = &argv[optind][1]; /* Skip over the '+' */
+ } else if (date_str == NULL) {
set_time = 1;
date_str = argv[optind];
- }
-#if 0
- else {
- error_msg("date_str='%s' date_fmt='%s'\n", date_str, date_fmt);
- show_usage();
}
-#endif
/* Now we have parsed all the information except the date format
which depends on whether the clock is being set or read */
@@ -230,18 +219,16 @@ int date_main(int argc, char **argv)
/* Correct any day of week and day of year etc. fields */
tm = mktime(&tm_time);
- if (tm < 0)
- error_msg_and_die(invalid_date, date_str);
- if ( utc ) {
- if (putenv("TZ=UTC0") != 0)
- error_msg_and_die(memory_exhausted);
+ if (tm < 0) {
+ error_msg_and_die(invalid_date, date_str);
+ }
+ if (utc && (putenv("TZ=UTC0") != 0)) {
+ error_msg_and_die(memory_exhausted);
}
/* if setting time, set it */
- if (set_time) {
- if (stime(&tm) < 0) {
- perror_msg("cannot set date");
- }
+ if (set_time && (stime(&tm) < 0)) {
+ perror_msg("cannot set date");
}
}
@@ -250,30 +237,29 @@ int date_main(int argc, char **argv)
/* Deal with format string */
if (date_fmt == NULL) {
#ifdef CONFIG_FEATURE_DATE_ISOFMT
- switch ( ifmt ) {
- case 4:
- date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
- break;
- case 3:
- date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
- break;
- case 2:
- date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
- break;
- case 1:
- date_fmt = "%Y-%m-%d";
- break;
- case 0:
- default:
+ switch (ifmt) {
+ case 4:
+ date_fmt = utc ? "%Y-%m-%dT%H:%M:%SZ" : "%Y-%m-%dT%H:%M:%S%z";
+ break;
+ case 3:
+ date_fmt = utc ? "%Y-%m-%dT%H:%MZ" : "%Y-%m-%dT%H:%M%z";
+ break;
+ case 2:
+ date_fmt = utc ? "%Y-%m-%dT%HZ" : "%Y-%m-%dT%H%z";
+ break;
+ case 1:
+ date_fmt = "%Y-%m-%d";
+ break;
+ case 0:
+ default:
#endif
- date_fmt = (rfc822
- ? (utc
- ? "%a, %e %b %Y %H:%M:%S GMT"
- : "%a, %e %b %Y %H:%M:%S %z")
- : "%a %b %e %H:%M:%S %Z %Y");
-
+ date_fmt =
+ (rfc822
+ ? (utc ? "%a, %e %b %Y %H:%M:%S GMT" :
+ "%a, %e %b %Y %H:%M:%S %z") : "%a %b %e %H:%M:%S %Z %Y");
+
#ifdef CONFIG_FEATURE_DATE_ISOFMT
- break;
+ break;
}
#endif
} else if (*date_fmt == '\0') {