From 3c8197e4d1d09af1933b0cdf44f1832fba6a37cc Mon Sep 17 00:00:00 2001 From: Oliver Hartkopp Date: Sat, 9 May 2020 18:19:24 +0200 Subject: [PATCH] asc2log: make date time sting milliseconds aware strptime() does not support milliseconds but the latest ASC logfile also provides a fraction of seconds, e.g. date Fri May 8 08:49:04.052 pm 2020 Read the value into the year variable before parsing the real year. We will therefore omit the milliseconds resolution for now. Signed-off-by: Oliver Hartkopp --- asc2log.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/asc2log.c b/asc2log.c index 9d61184..4ed8134 100644 --- a/asc2log.c +++ b/asc2log.c @@ -207,12 +207,12 @@ void eval_canfd(char* buf, struct timeval *date_tvp, char timestamps, char base, int get_date(struct timeval *tv, char *date) { - char ctmp[10]; + char ctmp[14]; int itmp; struct tm tms; - if (sscanf(date, "%9s %d %9s %9s %d", ctmp, &itmp, ctmp, ctmp, &itmp) == 5) { + if (sscanf(date, "%9s %d %13s %3s %d", ctmp, &itmp, ctmp, ctmp, &itmp) == 5) { /* assume EN/US date due to existing am/pm field */ if (!setlocale(LC_TIME, "en_US")) { @@ -220,13 +220,18 @@ int get_date(struct timeval *tv, char *date) { return 1; } - if (!strptime(date, "%B %d %r %Y", &tms)) - return 1; + if (!strptime(date, "%B %d %I:%M:%S %p %Y", &tms)) { + /* The string might contain a milliseconds value which strptime() + does not support. So we read the ms value into the year variable + before parsing the real year value (hack) */ + if (!strptime(date, "%B %d %I:%M:%S.%Y %p %Y", &tms)) + return 1; + } } else { /* assume DE date due to non existing am/pm field */ - if (sscanf(date, "%9s %d %9s %d", ctmp, &itmp, ctmp, &itmp) != 4) + if (sscanf(date, "%9s %d %13s %d", ctmp, &itmp, ctmp, &itmp) != 4) return 1; if (!setlocale(LC_TIME, "de_DE")) { @@ -234,8 +239,13 @@ int get_date(struct timeval *tv, char *date) { return 1; } - if (!strptime(date, "%B %d %T %Y", &tms)) - return 1; + if (!strptime(date, "%B %d %H:%M:%S %Y", &tms)) { + /* The string might contain a milliseconds value which strptime() + does not support. So we read the ms value into the year variable + before parsing the real year value (hack) */ + if (!strptime(date, "%B %d %H:%M:%S.%Y %Y", &tms)) + return 1; + } } //printf("h %d m %d s %d d %d m %d y %d\n",