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

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.

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

Related

How to convert the UTC date into local date in Moment.js?

I have a date in this format "2020-12-16T02:48:00" that came from the server. How can I convert this into local date and time? I tried some code but couldn't succeed.
Below is the attempt that I had made in angular after receiving date from the server.
response.data.map(date=>{
var centralDate = moment( date).zone("-06:00");
date = moment(centralDate).local().format('YYYY-MM-DD hh:mm:ss');
})
If indeed the value is in UTC (as per the title of your question), and it looks like "2020-12-16T02:48:00", and you want to convert it to local time, then you should do the following:
moment.utc(date).local().format('YYYY-MM-DD HH:mm:ss');
That does the following:
Parses the input in terms of UTC
Converts it to local time
Formats it as a string in the given format
Note also that you had hh in your original format. That is for hours in a 12-hour time format and thus you shouldn't use it without also using either A or a to indicate AM/PM or am/pm. Otherwise HH is for hours in a 24-hour time format.
If your issue is that the timezone doesn't change you can resolve using utcOffset (https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/09-utc-offset/) in this way:
response.data.map(date=>{
date = moment( date).utcOffset(-360);
})
Where 360 is the conversion fo the hours in minutes
var d= new Date();
d = new Date(d+ "Z")
I am not an expert in angular but I guess the trouble in your date is the word “T”. May be using string removal function you can remove the word “T” and then it becomes a proper date time value?

Nifi convert UTC to unix time

I have a flowfile attribute which is a UTC datetime in the format of yyyy-MM-dd HH:mm:ss.SSS
I need to convert this to a unix timestamp.
How can this be done? I know its possible to convert Unix to the above format using Jolt:
"time": "${time:format('yyyy-MM-dd HH:mm:ss.SSS')}"
however, im not sure how to do this in reverse?
Working with attributes in this way uses the NiFi Expression Langauge (not Jolt).
See the docs here https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html
${time:format('yyyy-MM-dd HH:mm:ss.SSS')}
Uses Expression Language to format the time attribute to the given SimpleDateFormat string.
${time:toNumber()}
Uses Expression Language to convert the given Date object to Epoch Millis.
https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html#tonumber

How to change UK date format in LogicApp

Im trying to convert a U.K. input date (dd-MM-yyyy) to format (yyyy-MM-dd)
I tried
"#formatDateTime('15-03-2019','yyyy-MM-dd')" ==> Error
but got error:
'In function 'convertTimeZone', the value provided
for date time string '15-03-2019' was not valid. The datetime
string must match ISO 8601 format.'
How do I go about converting this input date? The input format is (dd-MM-yyyy) and cannot be changed.
I can easily convert from (MM-dd-yyyy) as shown below, but im not able to convert from (dd-MM-yyyy)
"#formatDateTime('03-15-2019','yyyy-MM-dd')" ==> OK
Date and time functions provided by azure logic app cannot recognize the timestamp in dd-MM-yyyy format.
After my research, there is no existing function that can directly solve this problem, but you can use substring and concat to deal with this problem.
The workflow of the logic app looks like this:
The expression of the formatDataTime:
formatDateTime(concat(substring(<your-date-string>,6,4),'-',substring(<your-date-string>,3,2),'-',substring(<your-date-string>,0,2)),'yyyy-MM-dd')

Correct format for converting string to R date-time object

I've a system generated date and time format. It looks something like this, "2017-04-12-02.29.25.000000" . I want to convert this format into a standard one so that my system can read this and later on I can convert it into minutes. Someone please help to provide a code in R.
If you're unsure of the format, the guess_formats function in lubridate is pretty helpful:
w <- "2017-04-12-02.29.25.000000"
> lubridate::guess_formats(w, orders = 'YmdHMS')
YOmdHMS YmdHMS
"%Y-%Om-%d-%H.%M.%OS" "%Y-%m-%d-%H.%M.%OS"
orders is the format you want the function to investigate and it outputs the correct representation. If the second entry in the string is the day you can try YdmHMS.
The difference in the two formats in the output in the above example is based on formatting of the second entry (always with a leading zero or not). Trying the first format gives:
> as.POSIXct(w, format = "%Y-%Om-%d-%H.%M.%OS")
[1] "2017-04-12 02:29:25 EDT"
In the as.POSIXct call you may specify the timezone tz if required.

Format hours, minutes and seconds with moment.js

I get this value from my backend service: 171054. It represents hh:mm:ss. But when I use the formatting options from the docs it gives me back 00:00:00.
Things I've tried:
moment('171054').format('hh-mm-ss')
moment('171054').format('HH-mm-ss')
moment('171054').format('HH-MM-SS')
You are confusing format option with parsing option. Since your input string is not in ISO 8601 format, you have to specify format when parsing.
Here a working example for your use case:
var mom = moment('171054', 'HHmmss');
console.log(mom.format());
console.log(mom.format('HH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
I am not sure if momentjs can read that as date since there is no identifier. I would suggest changing the value for example you have 171054, get each 2 digits since you sure that this is represent as hh:mm:ss then add identifier between then like ":" or "-" then try us momentjs formatting again.

Resources