Search code examples
bashcurlawksedinfluxdb

Gawk mktime() generates incorrect Timestamp for InfluxDB


I'm working on parsing some data to InfluxDB using curl. The data points are Netflow records and I'm trying to convert the Netflow timestamp to the precision format required by InfluxDB.

My original timestamp looks like:

2016-11-22 04:23:25.0

I'm using sed to replace the '-' and ':' with spaces:

sed 's/-/ /g' | \
sed 's/:/ /g' | \

Which results in:

2016 11 22 04 23 25.0

I'm then using those fields to pass to gawk mktime(). This appears to generate a correct timestamp yet when I look at the raw records with Grafana, they all show a timestamp from 1969. I'm sure there is something glaring I'm overlooking?


Solution

    1. No reason to use Sed:

      $ echo '2016-11-22 04:23:25.0' | awk '{gsub(/[-:]/, FS); print mktime($0)}'
      1479810205
      
    2. Generated timestamp is correct:

      $ date --date=@1479810205
      Tue, Nov 22, 2016  4:23:25 AM