cancel
Showing results for 
Search instead for 
Did you mean: 

What's the difference between `time& and numpy to convert a

hzadonis
New Contributor
Hi, Dear all:
  I have a bunch of timestamp data. I need to pass them from python to kdb+. But I'm confused why the data conversion does not match each other.
  For example, the value of timestamp data is 12477000, the conversion in kdb+ is right, such as:
    q)`time$12477000
    03:27:57.000
  
  But the conversion in python(I use numpy and time to do the work) is not right:
    import numpy
    import time
    timeStamp_time = 12477000
    timeArray = time.gmtime(timeStamp_time)
    otherStyleTime = time.strftime("%H:%M:%S", timeArray)
    print(otherStyleTime)
  output is: 09:50:00

  Can somebody have any suggestion? Thanks so much!

Zheng
 
5 REPLIES 5

hzadonis
New Contributor
In time module, the output of time.strftime is:
    time.struct_time(tm_year=1970, tm_mon=5, tm_mday=25, tm_hour=09, tm_min=50, tm_sec=0, tm_wday=0, tm_yday=145, tm_isdst=0)
So, the key is how to let it know just hour:min:sec is required, year-month-day can be discarded.

User
Not applicable
Hi Zheng,

In kdb time is the number of milliseconds since midnight, in python time.gmtime uses the number of seconds since 1970.

Abdul

hzadonis
New Contributor
Yes, you're right. I noticed it. But how can I let python time.gmtime convert since midnight or other cheat/magic method?

User
Not applicable

You can just divide by 1000

>>> import datetime                                                                                                                                           
>>> datetime.datetime.fromtimestamp(12477000/1000).strftime("%H:%M:%S.%f")                                                                                    
'03:27:57.000000' 


From: personal-kdbplus@googlegroups.com <personal-kdbplus@googlegroups.com> on behalf of hzadonis@gmail.com <hzadonis@gmail.com>
Sent: 04 June 2018 10:15:44
To: Kdb+ Personal Developers
Subject: [personal kdb+] Re: What's the difference between `time& and numpy to convert a timestamp
 
Yes, you're right. I noticed it. But how can I let python time.gmtime convert since midnight or other cheat/magic method?

hzadonis
New Contributor
Hi, Abdul:
  Yes, it works, thank you so much!
   How silly I am, I used to think it should be divided by 3600 yesterday. 

Thanks!
Zheng