]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/time-util: move YYYYMMDDHHMMSS to common parser
authorSami Kerola <kerolasa@iki.fi>
Thu, 29 Aug 2013 15:11:41 +0000 (16:11 +0100)
committerSami Kerola <kerolasa@iki.fi>
Thu, 29 Aug 2013 17:14:08 +0000 (18:14 +0100)
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 <kzak@redhat.com>
References: http://markmail.org/message/6baqt4ttkopu7ra6
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
lib/time-util.c
login-utils/last.c

index a0b27bf52f8e0dc24ee5e9b7ff52f0634b384d62..e027bac75c920cdcff6939d0f915624febcd8bd7 100644 (file)
@@ -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:
index df784361d04a5ebbc89bb540dcabfede6a536fa0..410e612107860a713b7b39628ccc80ff66a31a9f 100644 (file)
@@ -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);