kdb+/q:How does one convert a utc timestamp to a datetime? - kdb

How could one convert the following timestamp t:1595779091979 into its equivalent `datetime$() representation (UTC)?
For instance if one tries to do so using the following.
q) `datetime$t
0000.00.00T00:00:00.000
q) `timestamp$t
2000.01.01D00:26:35.779091979
(Both are incorrect, the time should be 2020.07.26D...)
Thanks

The timestamp 1595779091979 looks like milliseconds since 1970 epoch. If you drop the millis - the conversion is simply
q)1970.01.01+0D00:00:01*1595779091
2020.07.26D15:58:11.000000000
or keeping the millis:
q)1970.01.01+0D00:00:00.001*1595779091979
2020.07.26D15:58:11.979000000
Finally, you can add the following definition to your utility library
ts:1970.01.01+0D00:00:00.001*
and use it in your code whenever you need conversion
q)ts 1595779091979
2020.07.26D15:58:11.979000000
Update: A slightly shorter solution can be written as
ts:1970.01.01D+1000000*

Related

How can I create time in proper format to export to a netCDF file in MATLAB?

Data
I am trying to create a time dimension using this:
t1 = datetime(1901,1,1);
t2 = datetime(2016,12,31);
t = t1:t2;
And create a netCDF file using this
nccreate('prec.nc','Prec',...
'Dimensions',{'time' 42369 'lon' 135 'lat' 129},...
'Format', 'netcdf4');
What I have tried
ncwrite('prec.nc', 'time', t);
Error Message
Error using cast
Unsupported data type for conversion: 'datetime'.
Error in internal.matlab.imagesci.nc/write (line 778)
scale_factor = cast(1, class(varData));
Error in ncwrite (line 87)
ncObj.write(varName, varData, start, stride);
Question
How can I create a daily time dimension that I can write out to a netCDF file? What is the proper date type for this conversion?
NetCDF doesn't define a single native way of storing date/time values, but there are established conventions, as desribed here.
There are two strategies for storing a date/time into a netCDF variable. One is to encode it as a numeric value and a unit that includes the reference time, e.g. "seconds since 2001-1-1 0:0:0" or "days since 2001-1-1 0:0:0" . The other is to store it as a String using a standard encoding and Calendar. The former is more compact if you have more than one date, and makes it easier to compute intervals between two dates.
So you could:
a) Use datestr to convert it to a string value. The conventional date string format for data interchange is ISO 8601, which you can get in Matlab with datestr(myDateTime, 'yyyy-mm-ddTHH:MM:SS').
b) Convert it to a numeric value representing seconds or days since a reference "epoch" time. I'd suggest using the Unix epoch, since Matlab provides a convenient conversion function for this already: posixtime(myDateTime). Then specify your units for that variable in the NetCDF file as 'seconds since 1970-01-01 00:00:00'.
You probably want to make sure your datetimes are in UTC before encoding them in the NetCDF, so you don't have to worry about time zone issues.

get difference in minutes in xslt 2.0 minutes-from-duration

i have to dates: xs:date(current-date()) and something like this 2017-11-01T09:17:00Z.
I need to get difference in minutes.
My idea is with minutes-from-duration() function, but i don't know how convert the dates to right input
You can simply substract two dates or dateTimes in XSLT/XPath 2 or later with e.g. current-dateTime() - xs:dateTime('2017-11-01T09:17:00Z') to get a duration, then you can use your function or other operations like e.g. (current-dateTime() - xs:dateTime('2017-11-01T09:17:00Z')) div xs:dayTimeDuration('PT1M') which would divide the duration by 1min.

How to convert date format 'DDMONYYYY:HH....' style date in Hive

I am inserting dates that look like:
'19APR2014:08:42:32.123456'
I am interpreting their format as
'DDMONYYYY:HH24:MI:SS.FFFFFF'
Though I have not seen any times after 12:59:59 I am assuming a 24-hour clock. Hive does not seem to understand what I want to do:
HiveException: Error evaluating unix_timestamp(date_string,'DDMONYYYY:HH24:MI:SS.FFFFFF')
Any ideas what I am doing wrong or what might be wrong with my format string?
Have you tried ddMMMyyyy:HH:mm:ss.SSS? According to Hive manual a pattern string in function unix_timestamp(string date, string pattern) should comply to Java's SimpleDateFormat(see manual and javadocs).

Powershell simplest method to get current time expressed as UTC

I have reviewed the post Creating a DateTime object with a specific UTC DateTime in PowerShell, but it does not seem to directly answer the question I am asking:
What is the most direct method in PowerShell (3.0) to return a sortable string representing "now" as UTC?
I expected the correct answer to be:
Get-Date -Format (Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern
OR
get-date -format u
but this is not the case.
Example: At 1300 hrs (1pm) on September 1st, 2016 in the Pacific Time Zone during DST, I get the response:
2016-09-01 13:00:00Z (the local time with a "Z" appended)
when I was expecting:
2016-09-01 20:00:00Z (correct UTC/GMT time)
So basically, my code is just getting a string representing the "local" time and appending a "Z".
Now, I know I can manipulate to get to that point, but I'm looking for the minimal (simplest, cleanest) way to get here.
Bonus Points (as if they existed): How do I get that same, sortable result, but displaying with "UTC" and/or "GMT" as the suffix. Same minimal requirement.
Probably something like this:
[DateTime]::UtcNow.ToString('u')
Which is equivalent to:
[DateTime]::UtcNow.ToString((Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern)
For the bonus, I think the most straightforward way is just to replace Z with UTC:
[DateTime]::UtcNow.ToString('u').Replace('Z','UTC')
I'm assuming you'll always want UTC since that what it seems like from your question. There doesn't appear to be a format string to get just the 3 letter time zone.
I tried this, and it also gives the result I want:
"[DateTime]::UtcNow.ToString('yyyyMMdd_HHmmss_UTC')"
It is showing time in the format 20180108_152407_UTC
so you can play with the date/time formatting as you wish basically

Problems with query using timespan

I am doing a manual query to my postgresql database (using OrmLiteReadConnectionExtensions.SqlList<T>) that has a TimeSpan argument.
SericeStack.Ormlite is converting TimeSpan to ::time instead of ::interval as I would expect it.
More specifically: TimeSpan.FromDays(3) is converted to ((E'00:00:00.000000')::time)(taken form pg logs).
Is there a work around for this?
My current work-around is to use the C# string.Format for this problematic parameter instead of the safe and recommended™ #paramname supported by SqlList<T>.
This could be considered dangerous, but since the parameter is a double, I'm probably Okay.
The relevant part of the string is:
string.Format(CultureInfo.InvariantCulture, "RESTOFTHEQUERY ('{0:0.####} seconds'::interval) RESTOFTHEQUERY", timespan.TotalSeconds);
Don't forget to use CultureInfo.InvariantCulture.
For what it's worth, you can just cast a time value to interval. Demo
SELECT now()::time::interval
So append ::interval in your manual query and you should be fine - except for intervals > 24 hours of course.