how to convert currentTimeMillis() to a Q timestamp?

1,859 views
Skip to first unread message

ag

unread,
Oct 3, 2011, 12:51:07 AM10/3/11
to personal...@googlegroups.com
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#currentTimeMillis()

I was able to gather currentTimeMillis() and stick it into a Q table.

q)t2
ts_begin
-------------
1317614494097
1317614503813
1317614513677
1317614535603
1317614618160
1317614667602
1317614668603
1317614670208
1317614671808
1317614691443

I want to be able to do something like 

select (`timestamp$ts_begin) from t2


Manish Patel

unread,
Oct 3, 2011, 6:21:04 AM10/3/11
to personal...@googlegroups.com
You would need to account for the difference in epoch for java (1970.01.01) and kdb (2000.01.01)

q)(2000.01.01-1970.01.01)*24*60*60*1000j
946684800000j
q)`timestamp$1317614494097j-(2000.01.01-1970.01.01)*24*60*60*1000j
2000.01.01D00:06:10.929694097

so you could do

select `timestamp$ts_begin-(2000.01.01-1970.01.01)*24*60*60*1000j) from t2

(judging from your numbers though, you seem to have millis after midnight?)


--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/personal-kdbplus/-/S7xH6ahd6sMJ.
To post to this group, send email to personal...@googlegroups.com.
To unsubscribe from this group, send email to personal-kdbpl...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/personal-kdbplus?hl=en.

ag

unread,
Oct 3, 2011, 10:38:55 PM10/3/11
to personal...@googlegroups.com
Something is not quite right... these should be within 3-4 minutes, not thousands of seconds.

q)select `timestamp$ts_begin-(2000.01.01-1970.01.01)*24*60*60*1000j from t2
ts_begin
-----------------------------
2000.01.01D00:06:10.929694097
2000.01.01D00:06:10.929703813
2000.01.01D00:06:10.929713677
2000.01.01D00:06:10.929735603
2000.01.01D00:06:10.929818160
2000.01.01D00:06:10.929867602
2000.01.01D00:06:10.929868603
2000.01.01D00:06:10.929870208
2000.01.01D00:06:10.929871808
2000.01.01D00:06:10.929891443

Attila Vrabecz

unread,
Oct 4, 2011, 3:16:06 AM10/4/11
to personal...@googlegroups.com
should be datetime which is millis
timestamp is nanoseconds
  Attila


--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/personal-kdbplus/-/vfHI4aK77GoJ.

ag

unread,
Oct 5, 2011, 11:38:49 PM10/5/11
to personal...@googlegroups.com
It's still not working - sorry I just 


q)t2
ts_begin
-------------
1317614494097
1317614503813
1317614513677
1317614535603
1317614618160
1317614667602
1317614668603
1317614670208
1317614671808
1317614691443

q)`datetime$1317614494097j-(2000.01.01-1970.01.01)*24*60*60*1000j
**85.11.07T**:31:23.647

BTW: I will post this on code.kx.com for future reference.

Aaron Davies

unread,
Oct 6, 2011, 1:04:39 AM10/6/11
to personal...@googlegroups.com
> q)`datetime$1317614494097j-(2000.01.01-1970.01.01)*24*60*60*1000j
> **85.11.07T**:31:23.647

q){"p"$1970.01.01D+1000000j*x}1317614494097j
2011.10.03D04:01:34.097000000

i.e. scale up to nanos, change epoch, and cast back

ag

unread,
Oct 6, 2011, 2:22:22 AM10/6/11
to personal...@googlegroups.com
Thank you. I added this example to https://code.kx.com/trac/wiki/Reference/DollarSign

Attila Vrabecz

unread,
Oct 6, 2011, 3:57:20 AM10/6/11
to personal...@googlegroups.com
what i meant when i said datetime is
 
q)f:{`datetime$(z%x)-y}[prd 24 60 60 1000j;(0^0ND)-1970.01.01]
 
q)f 1317614494097j
2011.10.03T04:01:34.097
 
but using timestamp as Aaron's suggests can save you from some headache with datetime (being float and all)
  Attila


On Thu, Oct 6, 2011 at 7:22 AM, ag <anuj....@gmail.com> wrote:
Thank you. I added this example to https://code.kx.com/trac/wiki/Reference/DollarSign

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/personal-kdbplus/-/OUi89kStVpIJ.
Reply all
Reply to author
Forward
0 new messages