how to calculate minutes between 2 datetime - clips

I am trying to implement a def function that should calculate minutes between 2 datetime ,
How can I implement?
"date1": "2020-04-17T20:45:00.000Z"
"date2": "2020-04-17T20:25:00.000Z"
should be
output: 20
Ideally, I would like to convert these datetime values to a second-based repsresentation, such as unixtime, so that I can do the math via simple substraction.

Related

How to find difference between two timestamps in Talend HI

I am new to Talend I want to find the difference between the two timestamps.
I am having two columns start_time and end_time.
I want to make a table in destination that will show the difference in both the timestamps, specifically I want to show hours mins and seconds.
Also I want time in timestamp not in ling format, how can I achieve this
start_time- 2021-06-18 08:27:52.000000
end_time- 2021-06-18 08:29:59.000000
I tried-
creating a variable 'ms' of long type in tmap = TalendDate.diffDate(row181.start_time,row181.end_time,"mm")
for converting into hh:mm:ss
String.format("%02d:%02d:%02d.%d", (Var.ms / (1000 * 60 * 60)) % 24, (Var.ms / (1000 * 60)) % 60, (Var.ms / 1000) % 60, Var.ms % 1000)
if I make table as string I am getting this err-
column "call_duration" is of type bigint but expression is of type character varying
Above T-map expression returning zero also I have to use long in the destination column type, but I want date type
Pattern "MM" refers to months, not minutes. Use "mm" instead.
How could you return a date type for a difference between two dates ? The result is necessarily a number (long/double...) .
If you want your output with hours/mins/seconds, you should use diffDate with "ss" pattern to get a long representing the duration in seconds. Then you'll have to transform this to get hours and minutes (e.g 3700 s would give you 1 hour, 1 minute, 40 seconds) . You also have to determine what kind of output you want (one column for each, a string with the concatenation of hours/minutes/seconds...)
Example : with row1.diffDate being your diffdate in seconds in input of a tMap, you could separate in three different columns. Then you'll only have to concatenate all values in a string. if you want a string output with ":" separator.

prometheus filter range vector by day_of_week

I use sub-queries to get hourly aggregated values for the last week for example: The number of http requests per hour over the whole last week, which will return 168 values in a range vector.
delta(http_server_requests_seconds_count[1h])[1w:1h]
Now I want to filter the value to return only the ones which are for a specific week day, lets say return only the 24 value from Monday.
I found some hints about day_of_week, timestamp, bool expr, but I cannot combine them to get it working or maybe it is not possible? Something like:
delta(http_server_requests_seconds_count[1h])[1w:1h] bool day_of_week() == 1
It'd be more effficient to adjust your start/end time to only over the day, but you could do:
(increase(http_server_requests_seconds_count[1h]) and on () day_of_week() == 1)[1w:1h]

Time duration in Google Data Studio

I have some data I collect regarding length of time that's stored in HH:MM format. The data is in relation to sleep patterns (i.e. sleep duration, time fell asleep, etc...).
I am trying to import the data in Google Data Studio (DS) as a numeric variable, but it appears as text. I can see in DS there is a duration (seconds) numeric format, how can I convert a text variable into a numeric one?
It would be easier to convert the fields in a Google Sheet, but I need them as HH:MM for other calculations.
Try this:
0) Create a new Calculated Field
1) Seconds
Use a formula to convert the Time values to a single value in Seconds, where HH:MM:SS represents the field name:
( CAST(REGEXP_EXTRACT(HH:MM:SS, "^(\\d{2})") AS NUMBER ) * 60 * 60 ) + ( CAST(REGEXP_EXTRACT(HH:MM:SS, ":(\\d{2}):") AS NUMBER ) * 60 ) + CAST(REGEXP_EXTRACT(HH:MM:SS, "(\\d{2})$") AS NUMBER )
2) Change Field Type
- Numeric > Duration (sec.)
Credit to Google support community
You can use the TODATE or MINUTE and SECOND function into a calculated field to extract minutes and second from a date. However don't expect to display minutes and second datapoint on a line chart in Data Studio, timeserie charts only support hour-level data at a minimum.

Is there a way to do datetime addition in pig?

what I would like to do in pig is something that is very common in sql.
I have date field that is of the form yyy-mm-dd hh:mm:ss and I have another field that contains an integer which represents an amount of hours. Is there a way to easily add the integer to the datetime field so that we get a result of what we expect with clock math.
Example: date is 2013-06-01 : 23:12:12.
Then I add 2 hours
I should get 2013-06-02 01:12:12.
With the latest release of Pig(0.11.0) it should be possible. But the amount of hours(time) should be as per ISO8601 Duration Format. It provides class AddDuration which allows us to add a DateTime object with a Duration object. You can find more about AddDuration at this page.
Edit :
Yes, you can add negative hours. I tried this on my Ubuntu box :
Input :
2009-01-07T01:07:01.000Z,PT1S
2008-02-06T02:06:02.000Z,PT1M
2007-03-05T03:05:03.000Z,PT-1H
Query :
grunt> a = LOAD '/pig.txt' USING PigStorage(',') AS (dt:datetime, dr:chararray);
grunt> b = FOREACH a GENERATE AddDuration(dt, dr) AS dt1;
grunt> dump b;
Output :
(2009-01-07T01:07:02.000Z)
(2008-02-06T02:07:02.000Z)
(2007-03-05T02:05:03.000Z)

How can I create DateTime object based on the day?

I want to create an Datetime object based on the number of day in the year.
This number is from the 365 days of the year (for example it can be: 123 or 23 or 344...)
How can I do that?
Thanks
Use the DateTime.ordinal method. Here's an example to get the 100th day of year 2011.
require 'date'
year, day = 2011, 100
DateTime.ordinal(year, day)
# #<DateTime: 2011-04-10T00:00:00+00:00 (4911323/2,0,2299161)>
If you want it as the number of days from now you should do the following:
time = Time.new + (60*60*24)*(numberOfDaysFromNow)
If you want it as the number of days from the start of the year you should do the following
time = Time.new(Time.now.year) + (60*60*24)*(dayOfTheYear-1)

Resources