bigmacsetnotenough
bigmacsetnotenough

Reputation: 112

How to Insert Current Local Time with Millisecond Precision into DolphinDB Stream Table from Python?

I’m trying to insert the current system time into a DolphinDB stream table from Python, but there are two issues with timestamps:

  1. Millisecond Precision Loss: When using np.datetime64('now', "ms"), the millisecond part is always 000 (e.g., 2023-10-01T12:34:56.000).
  2. UTC vs Local Time: np.datetime64('now') returns UTC time instead of local time.

I have created the following stream table in DolphinDB databse:

 st=streamTable(
        array(LONG,0) as id,
        array(TIMESTAMP,0) as ts,
        array(DATETIME,0) as dt,
        array(DOUBLE,0) as value 
    )
    share(st,"demoSt")

python code:

import dolphindb as ddb
import numpy as np
import pandas as pd

s = ddb.session()
c=s.connect("192.168.1.125", 8848, "admin", "123456")
s.run("tableInsert{demoSt}", [1,np.datetime64('now',"ms"),np.datetime64('now'),0.01 ])

output:enter image description here

How to get the correct time?

Upvotes: 0

Views: 32

Answers (1)

furas
furas

Reputation: 142985

It may need to get time.time() substract time.timezone and multiply by 1_000 (10**3) to get milliseconds

np.datetime64( int((time.time()-time.timezone)*10**3), 'ms')

Previous answer. It is WRONG because locatime() removes milliseconds, and it needs time.timezone and I forgot *10**3

You can use time.localtime() to get local time (with milliseconds), and use time.mktime() to convert it to epoch which you can use in datetime64()

np.datetime64(int(time.mktime(time.localtime())), 'ms')

Upvotes: 1

Related Questions