From: Sami Kerola Date: Thu, 29 Aug 2013 15:11:41 +0000 (+0100) Subject: lib/time-util: move YYYYMMDDHHMMSS to common parser X-Git-Tag: v2.24-rc1~314 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=75ab9bf1eb5637e5548f7c570f2086199223a436;p=thirdparty%2Futil-linux.git lib/time-util: move YYYYMMDDHHMMSS to common parser Even while the YYYYMMDDHHMMSS time format it not magnificent it is best to make it to be part of the one, and only, time format parser. Proposed-by: Karel Zak References: http://markmail.org/message/6baqt4ttkopu7ra6 Signed-off-by: Sami Kerola --- diff --git a/lib/time-util.c b/lib/time-util.c index a0b27bf52f..e027bac75c 100644 --- a/lib/time-util.c +++ b/lib/time-util.c @@ -306,6 +306,13 @@ int parse_timestamp(const char *t, usec_t *usec) goto finish; } + tm = copy; + k = strptime(t, "%Y%m%d%H%M%S", &tm); + if (k && *k == 0) { + tm.tm_sec = 0; + goto finish; + } + return -EINVAL; finish: diff --git a/login-utils/last.c b/login-utils/last.c index df784361d0..410e612107 100644 --- a/login-utils/last.c +++ b/login-utils/last.c @@ -440,45 +440,6 @@ static void __attribute__((__noreturn__)) usage(FILE *out) } -static time_t parsetm(char *ts) -{ - struct tm u, origu; - time_t tm; - - memset(&tm, 0, sizeof(tm)); - - if (sscanf(ts, "%4d%2d%2d%2d%2d%2d", &u.tm_year, - &u.tm_mon, &u.tm_mday, &u.tm_hour, &u.tm_min, - &u.tm_sec) != 6) - return (time_t)-1; - - u.tm_year -= 1900; - u.tm_mon -= 1; - u.tm_isdst = -1; - - origu = u; - - if ((tm = mktime(&u)) == (time_t)-1) - return tm; - - /* - * Unfortunately mktime() is much more forgiving than - * it should be. For example, it'll gladly accept - * "30" as a valid month number. This behavior is by - * design, but we don't like it, so we want to detect - * it and complain. - */ - if (u.tm_year != origu.tm_year || - u.tm_mon != origu.tm_mon || - u.tm_mday != origu.tm_mday || - u.tm_hour != origu.tm_hour || - u.tm_min != origu.tm_min || - u.tm_sec != origu.tm_sec) - return (time_t)-1; - - return tm; -} - static void process_wtmp_file(char *ufile, int lastb, int extended, time_t until, time_t present) { @@ -796,17 +757,11 @@ int main(int argc, char **argv) fulltime++; break; case 'p': - present = parsetm(optarg); - if (present != (time_t) -1) - break; if (parse_timestamp(optarg, &p) < 0) errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg); present = (time_t) (p / 1000000); break; case 't': - until = parsetm(optarg); - if (until != (time_t) -1) - break; if (parse_timestamp(optarg, &p) < 0) errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg); until = (time_t) (p / 1000000);