cancel
Showing results for 
Search instead for 
Did you mean: 

DateTime from MicroSeconds As String

planefan
Contributor
Hey Everyone, 
Trying to turn a MicroSeconds timestamp, loaded in originally as a string, into a DateTime. 

ex: 1433149936578160   

I have successfully used "P"$10$string to load the string in and convert it to DateTime, but it's missing precision at the end. 

For example: 

1433149936578160 (using  "P"$10$string) converts to 2015.06.01D09:12:16.000000000 instead of 2015.06.01D09:12:16.578164000. 

Thanks!
3 REPLIES 3

sohagan857
New Contributor
Here's one way:

q)ex:1433149936578160;  1970.01.01D+1000*ex
2015.06.01D09:12:16.578160000

q)ex:"1433149936578160" ; 1970.01.01D+1000*"J"$ex
2015.06.01D09:12:16.578160000

george_winton_h
New Contributor
Your number seems to be the number of microseconds since 1970 (unix time) rather than q's usual epoch of 2000. The tok that you're using only natively supports these for 9 to 11 digits and assumes seconds.

The following would work as an alternative:

q) offset: "j"$1970.01.01D00:00:00.00
q) offset+"p"$1000*"J"$string

Best,

George

This answer is the same as Seans, (and less succinct!), but just to add that sometimes it can be helpful to explicitly multiply your number by a microsecond given that your number is a count of microseconds, i.e. 

q)f:1970.01.01+00:00:00.000001*"J"$
q)f"1433149936578160"
2015.06.01D09:12:16.578160000

Often makes it easier to read and makes it clearer what's happening. 

Terry