Telegraf Starlark processor - how to convert date time to Unix epoch format

1.8k views Asked by At

I followed the tutorial https://www.influxdata.com/blog/json-to-influxdb-with-telegraf-and-starlark/ to get started with Telegraf and Starlark.

I get data in JSON format, but following tutorial and treating the input as String and then parsing using Starlark.

My problem is, I need to use the datetime stamp that is part of the JSON input. I am using the below code and it works if I set it to some hard coded Unix epoch time

new_metric = Metric("mymetric")
new_metric.time =1615702479866917911

But how do I convert a date that I have in DD-Mon-YYYY H:M:S format (e.g.12-Mar-2021 15:30:00 ) to Unix epoch format in Starlark script. Since, one cannot import any python libraries in Starlark script, not sure how can I accomplish this conversion.

1

There are 1 answers

1
davidib17 On

metric.time = time.parse_time("12-Mar-2021 15:30:00", format="02-Jan-2006 15:04:05").unix

And if you need to set a timezone (the above will parse to UTC), you can do so by adding an argument to the end, like so:

metric.time = time.parse_time("12-Mar-2021 15:30:00", format="02-Jan-2006 15:04:05", location="US/Eastern").unix

And from a regular time object, you can grab the unix time like this:

metric.time = time.now().unix