Skip to content

Commit

Permalink
added a set_time method to format the logitem->time field
Browse files Browse the repository at this point in the history
  • Loading branch information
millsjustin committed Apr 30, 2018
1 parent ee88475 commit c1a2f32
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,23 @@ set_date (char **fdate, struct tm tm)
return 0;
}

/* Format the broken-down time tm to a numeric time format.
*
* On error, or unable to format the given tm, 1 is returned.
* On success, a malloc'd format is returned. */
static int
set_time (char **ftime, struct tm tm)
{
char buf[TIME_LEN] = "";

memset (buf, 0, sizeof (buf));
if (strftime (buf, TIME_LEN, "%H:%M:%S", &tm) <= 0)
return 1;
*ftime = xstrdup (buf);

return 0;
}

/* Determine the parsing specifier error and construct a message out
* of it.
*
Expand Down Expand Up @@ -1047,12 +1064,11 @@ parse_specifier (GLogItem * logitem, char **str, const char *p, const char *end)
if (!(tkn = parse_string (&(*str), end, 1)))
return spec_err (logitem, SPEC_TOKN_NUL, *p, NULL);

if (str_to_time (tkn, tfmt, &tm) != 0) {
if (str_to_time (tkn, tfmt, &tm) != 0 || set_time(&logitem->time, tm) != 0) {
spec_err (logitem, SPEC_TOKN_INV, *p, tkn);
free (tkn);
return 1;
}
logitem->time = tkn;
break;
/* date/time as decimal, i.e., timestamps, ms/us */
case 'x':
Expand All @@ -1061,12 +1077,11 @@ parse_specifier (GLogItem * logitem, char **str, const char *p, const char *end)
if (!(tkn = parse_string (&(*str), end, 1)))
return spec_err (logitem, SPEC_TOKN_NUL, *p, NULL);

if (str_to_time (tkn, tfmt, &tm) != 0 || set_date (&logitem->date, tm) != 0) {
if (str_to_time (tkn, tfmt, &tm) != 0 || set_date (&logitem->date, tm) != 0 || set_time(&logitem->time, tm) != 0) {
spec_err (logitem, SPEC_TOKN_INV, *p, tkn);
free (tkn);
return 1;
}
logitem->time = tkn;
break;
/* Virtual Host */
case 'v':
Expand Down

0 comments on commit c1a2f32

Please sign in to comment.