i have been trying to convert a string to date using
${test:toString():toDate('dd-MMM-yy HH.mm.ss.SSSSSSSSS'):format('dd-MMM-yy HH.mm.ss.SSSSSSSSS')}
my value for test attribute is like 13-MAR-20 15.50.41.396000000
when i'm using the above mentioned expression to convert the string to Date, it actually is changing the date as below:
test (input value):
13-MAR-20 15.50.41.396000000
time (output value)
18-Mar-20 05.50.41.000000000
please advise!
I ran into a similar issue with date time encoded in ISO 8601.
The problem is, that the digits after the second are defined as fragment of a second, not milliseconds. If it has 3 digits, it equivalent to milliseconds. If more than 3, the toDate() function parse the fragment of a second as milliseconds. In your case 396000000 milliseconds = 4,58333333 days.
I solved my issue with replaceAll() by cutting digits to first 3.
${test:replaceAll('(\.[0-9]{3})([0-9]+)','$1')}
But my value was formatted as 18-06-20T05:50:41.396000000, so maybe you have to adjust the regex.
I want to convert a readable timestamp to UNIX time.
For example: I want to convert 2018-08-24 18:42:16 to 1535136136000.
Here is my syntax:
TO_UNIXTIME('2018-08-24 06:42:16') new_year_ut
My error is:
SYNTAX_ERROR: line 1:77: Unexpected parameters (varchar(19)) for function to_unixtime. Expected: to_unixtime(timestamp) , to_unixtime(timestamp with time zone)
You need to wrap the varchar in a CAST to timestamp:
to_unixtime(CAST('2018-08-24 06:42:16' AS timestamp)) -- note: returns a double
If your timestamp value doesn't have fraction of second (or you are not interested in it), you can cast to bigint to have integral result:
CAST(to_unixtime(CAST('2018-08-24 06:42:16' AS timestamp)) AS BIGINT)
If your readable timestamp value is a string in different format than the above, you would need to use date_parse or parse_datetime for the conversion. See https://trino.io/docs/current/functions/datetime.html for more information.
Note: when dealing with timestamp values, please keep in mind that: https://github.com/trinodb/trino/issues/37
I have a google sheet script that fetches youtube's video durations. The problem is the time data is in the ISO 8601 format.
For example:
PT3M23S
The formula I'm using right now does a good job converting this into a more readable format.
=iferror(REGEXREPLACE(getYoutubeTime(B20),"(PT)(\d+)M(\d+)S","$2:$3"))
It converts the above into a more readable format 3:23
Now the issue at hand is if the duration of the video is exactly 3 minutes or if the video is shorter than 1 minute regexreplace doesn't reformat it.
Instead it reads
PT4M OR PT53S
Is there a way to edit the formula to address each variant that potential could occur?
Where it would format PT4M into 4:00 or PT53S into 0:53
Lastly, if the seconds in the duration are between 1-9 the API returns a single digit value for the seconds. Which means the formula above will look wrong. For example, PT1M1S is formatted into 1:1 when it should read 1:01
It would be great if the formula could account for the first 9 seconds and add a 0 to make it more readable.
Thanks for reading this far, if anyone could help me out I'd very much appreciate it.
Just in case its easier to do this within the script itself here's the custom script that retrieves the video duration.
function getYoutubeTime(videoId){
var url = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=" + videoId;
url = url + "&key=";
var videoListResponse = UrlFetchApp.fetch(url);
var json = JSON.parse(videoListResponse.getContentText());
return json["items"][0]["contentDetails"]["duration"];
}
Is very ugly, but seems to work for the examples provided:
=iferror(left(mid(A1,3,len(A1)-2),find("M",mid(A1,3,len(A1)-2))-1)*60,0)+substitute(REGEXreplace(mid(A1,3,len(A1)-2),"(.+M)",""),"S","")
Outputs seconds, eg 203 from PT3M23S. To change to 00:03:23 wap the above formula in ( ... )/86400 and format result as Time.
Script Solution:
function iso8601HMparse(str) {
return str.replace(/PT(\d+(?=M))?M?(\d+(?=S))?S?/g,function(mm,p1,p2){//regex to get M and S value
return [0,p1,p2].map(function(e){
e = e ? e:0;
return ("00"+e).substr(-2); //fix them to 2 chars
}).join(':');
})
}
Splice it in your script like:
return iso8601HMparse(json["items"][0]["contentDetails"]["duration"].toString());
Spreadsheet Function:
=TEXT(1*REGEXREPLACE(REGEXREPLACE(A1,"PT(\d+M)?(\d+?S)?","00:00$1:00$2"),"[MS]",),"MM:SS")
Late to the party, but here's what I'm using:
=TIMEVALUE(
IFERROR(TEXT(MID(A1,3,FIND("H",A1)-3),"00"),"00")&":"&
IFERROR(TEXT(MID(A1,IFERROR(FIND("H",A1)+1,3),FIND("M",A1)-IFERROR(FIND("H",A1)+1,3)),"00"),"00")&":"&
IFERROR(TEXT(MID(A1,IFERROR(FIND("M",A1)+1,3),FIND("S",A1)-IFERROR(FIND("M",A1)+1,3)),"00"),"00"))
You can also stick ARRAYFORMULA in front of this and change A1 to a a column to get values for a whole list of them.
I have some code that reads a datetime from a sqlite database, the datetime is returned as a string. when I try to convert it to a date using QDateTime::FromString it returns an invalid date. Below is the time as returned from the database and conversion.
Why is this failing to parse?
// -this is the value returned from the DB currentServerTime=2012-01-17 19:20:27.0
QString format("yyyy/MM/dd hh:mm:ss");
QString qCurrentServerTime(currentServerTime);
now = QDateTime::fromString(qCurrentServerTime, format);
No expert in QT, but if QDateTime::fromString() works as one would (reasonably) expect and according to this, you're not using the correct pattern.
You indicate the string read from the sqllite database is like "2012-01-17 19:20:27.0", then your format should be like yyyy-MM-dd HH:mm:ss.z.
In detail:
Your separator should by '-' not '/' (as you show in the example)
The time seems to be in 24 hours format (19 -> 7 p.m.) (so use HH instead of hh)
You have one digit for milliseconds, so add .z.
The following are the timestamps that is present in the timestamp column of the table userdata.My question is that how to write a query such that i get the output as i need the month name i.2,2010-03 and the total time used in the month
userdata.months.filter().order_by('-timestamp')
'2011-03-07 16:03:01'
'2011-03-07 16:07:04'
'2011-03-06 11:03:01'
'2011-03-08 16:03:01'
'2011-03-04 09:03:01'
'2011-05-16 16:03:01'
'2011-05-18 16:03:01'
'2011-07-16 12:03:01'
'2011-07-17 12:03:01'
'2011-07-17 15:03:01'
something like
my_month = 2 (or whatever you need)
userdata.months.filter(timestamp__month=my_month).aggregate(sum('timestamp__time')
i'm not sure about that timestamp__time, you may search a bit about it, but i think it's correct.
otherwise, you can use a raw query (userdata.months.raw('query here'))