简体   繁体   中英

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

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 (eg12-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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM