I have an input file with timestamps and odometer readings, like so:
2017-09-16 18:14:00,80465
2017-09-19 18:23:00,80898
2017-09-21 08:05:00,81253
2017-09-27 18:20:00,82155
2017-10-03 18:36:00,82902
2017-10-09 18:33:00,83699
... and would like to add the timestamp converted to an epoch, with the following code:
BEGIN {}
{
# change timestamp to epoch (without TZ correction)
epoch_returned = timestamp_to_epoch($1)
printf("%s,%s\n", $0, epoch_returned)
}
function timestamp_to_epoch(timestamp_in) {
FPAT = "[0-9][0-9]"
epoch_out = mktime($1$2" "$3" "$4" "$5" "$6" "$7)
return epoch_out
}
The output produces this:
2017-09-16 18:14:00,80465,-1
2017-09-19 18:23:00,80898,1505809380
2017-09-21 08:05:00,81253,1505945100
2017-09-27 18:20:00,82155,1506500400
2017-10-03 18:36:00,82902,1507019760
2017-10-09 18:33:00,83699,1507537980
The first line always returns -1. I have deleted the first line in the input file, the (then) first line still returns -1. I left only one line in the input. Also returns -1.
If I enter an empty line in the input file, it returns -1, while all other add the epoch as expected.
I have looked at this for quite a while and can't figure it out.
I am using GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0) onLinux Mint 20.1 Cinnamon 4.8.6.
Any hints appreciated.