Skip to content

Commit

Permalink
Added the ability to process timestamps in milliseconds.
Browse files Browse the repository at this point in the history
It uses %* as --date-format & --time-format..

Fixes allinurl#1983, allinurl#259
  • Loading branch information
allinurl committed Jan 13, 2021
1 parent a77a904 commit f546e56
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
10 changes: 8 additions & 2 deletions goaccess.1
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ They all begin with a percentage (%) sign. See `man strftime`.
.IP
Note that if a timestamp is given in microseconds,
.I %f
must be used as time-format
must be used as time-format.
If the timestamp is given in milliseconds
.I %*
must be used as time-format.
.TP
\fB\-\-date-format=<dateformat>
The date-format variable followed by a space, specifies the log format time
Expand All @@ -195,7 +198,10 @@ They all begin with a percentage (%) sign. See `man strftime`.
Note that if a timestamp is given in microseconds,
.I
%f
must be used as date-format
must be used as date-format.
If the timestamp is given in milliseconds
.I %*
must be used as date-format.
.TP
\fB\-\-log-format=<logformat>
The log-format variable followed by a space or
Expand Down
4 changes: 2 additions & 2 deletions src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,9 @@ set_date_num_format (void) {

/* always add a %Y */
buflen += snprintf (buf + buflen, flen - buflen, "%%Y");
if (strpbrk (fdate, "hbmB"))
if (strpbrk (fdate, "hbmBf*"))
buflen += snprintf (buf + buflen, flen - buflen, "%%m");
if (strpbrk (fdate, "de"))
if (strpbrk (fdate, "def*"))
buflen += snprintf (buf + buflen, flen - buflen, "%%d");

conf.date_num_format = buf;
Expand Down
20 changes: 10 additions & 10 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,25 +500,25 @@ get_visitors_date (const char *odate, const char *from, const char *to) {
int
str_to_time (const char *str, const char *fmt, struct tm *tm) {
char *end = NULL, *sEnd = NULL;
unsigned long long usecs = 0;
unsigned long long ts = 0;
int us = strcmp ("%f", fmt) == 0;
int ms = strcmp ("%*", fmt) == 0;
time_t seconds = 0;

if (str == NULL || *str == '\0' || fmt == NULL || *fmt == '\0')
return 1;

/* check if char string needs to be converted from microseconds */
if (strcmp ("%f", fmt) == 0) {
/* check if char string needs to be converted from milli/micro seconds */
if (us || ms) {
errno = 0;
tm->tm_year = 1970 - 1900;
tm->tm_mday = 1;

usecs = strtoull (str, &sEnd, 10);
ts = strtoull (str, &sEnd, 10);
if (str == sEnd || *sEnd != '\0' || errno == ERANGE)
return 1;

tm->tm_sec = usecs / SECS;
tm->tm_isdst = -1;
if (mktime (tm) == -1)
return 1;
seconds = (us) ? ts / SECS : ts / MILS;
/* if GMT needed, gmtime_r instead of localtime_r. */
localtime_r (&seconds, tm);

return 0;
}
Expand Down

0 comments on commit f546e56

Please sign in to comment.