I'm trying to find a way to convert timestamps in log files to a Unix timestamp. The command I have come up with so far is the following:
awk -F'[' '{ print $2}' | awk -F']' '{cmd ="date \"+%s\" -d \""$1"\""; cmd | getline var; print var $2; close(cmd)}'
The command works when the original timestamp includes UTC as the timezone or if no timezone is specified. However, it fails when the timezone is something else. For example, this works:
$ entry="[08-May-2020 15:40:32 UTC] PHP Warning: Illegal string offset 'ID' in /home/example/public_html/wp-content/themes/example/functions.php on line 1290"
$ echo "$entry" | awk -F'[' '{ print $2}' | awk -F']' '{cmd="date \"+%s\" -d \""$1"\""; cmd | getline var; print var $2; close(cmd)}'
1588952432 PHP Warning: Illegal string offset 'ID' in /home/example/public_html/wp-content/themes/example/functions.php on line 1290
And the command also works when no timezone is specified:
$ entry="[08-May-2020 15:40:32] PHP Warning: Illegal string offset 'ID' in /home/example/public_html/wp-content/themes/example/functions.php on line 1290"
$ echo "$entry" | awk -F'[' '{ print $2}' | awk -F']' '{cmd="date \"+%s\" -d \""$1"\""; cmd | getline var; print var $2; close(cmd)}'
1588948832 PHP Warning: Illegal string offset 'ID' in /home/example/public_html/wp-content/themes/example/functions.php on line 1290
However, when the timezone is Europe/London
it fails:
$ entry="[08-May-2020 15:40:32 Europe/London] PHP Warning: Illegal string offset 'ID' in /home/example/public_html/wp-content/themes/example/functions.php on line 1290"
$ echo "$entry" | awk -F'[' '{ print $2}' | awk -F']' '{cmd="date \"+%s\" -d \""$1"\""; cmd | getline var; print var $2; close(cmd)}'
date: invalid date ‘08-May-2020 15:40:32 Europe/London’
PHP Warning: Illegal string offset 'ID' in /home/example/public_html/wp-content/themes/example/functions.php on line 1290
I'm really not sure how to debug awk command. I suspect it may not like the slash in the timezone, but that's just a guess.
date
command is not able to parse the string08-May-2020 15:40:32 Europe/London
as a date.08-May-2020 15:40:32 UTC
? I'm trying to learn more about awk and converting dates.date +"%s" -d"TZ=\"Europe/London\" 08-May-2020 15:40:32"
which is accepted, see Specifying time zone rules.Europe/London
is a perfectly valid Olson database value that GNUdate
is perfectly able to parse (and understand). Please read my answer.