Skip to content

Commit

Permalink
lib/timeutils: add %Y-%m-%dT%H:%M:%S to parse_timestamp()
Browse files Browse the repository at this point in the history
Signed-off-by: Karel Zak <kzak@redhat.com>
  • Loading branch information
karelzak committed Nov 8, 2019
1 parent 8fb9451 commit 7999563
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/timeutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ int parse_timestamp(const char *t, usec_t *usec)
* Allowed syntaxes:
*
* 2012-09-22 16:34:22
* 2012-09-22T16:34:22
* 2012-09-22 16:34 (seconds will be set to 0)
* 2012-09-22 (time will be set to 00:00:00)
* 16:34:22 (date will be set to today)
Expand Down Expand Up @@ -271,6 +272,11 @@ int parse_timestamp(const char *t, usec_t *usec)
if (k && *k == 0)
goto finish;

tm = copy;
k = strptime(t, "%Y-%m-%dT%H:%M:%S", &tm);
if (k && *k == 0)
goto finish;

tm = copy;
k = strptime(t, "%y-%m-%d %H:%M", &tm);
if (k && *k == 0) {
Expand Down Expand Up @@ -570,13 +576,21 @@ int main(int argc, char *argv[])
char buf[ISO_BUFSIZ];

if (argc < 2) {
fprintf(stderr, "usage: %s <time> [<usec>]\n", argv[0]);
fprintf(stderr, "usage: %s [<time> [<usec>]] | [--timestamp <str>]\n", argv[0]);
exit(EXIT_FAILURE);
}

tv.tv_sec = strtos64_or_err(argv[1], "failed to parse <time>");
if (argc == 3)
tv.tv_usec = strtos64_or_err(argv[2], "failed to parse <usec>");
if (strcmp(argv[1], "--timestamp") == 0) {
usec_t usec;

parse_timestamp(argv[2], &usec);
tv.tv_sec = (time_t) (usec / 1000000);
tv.tv_usec = usec % 1000000;
} else {
tv.tv_sec = strtos64_or_err(argv[1], "failed to parse <time>");
if (argc == 3)
tv.tv_usec = strtos64_or_err(argv[2], "failed to parse <usec>");
}

strtimeval_iso(&tv, ISO_DATE, buf, sizeof(buf));
printf("Date: '%s'\n", buf);
Expand Down

0 comments on commit 7999563

Please sign in to comment.