How can I store the current timestamp in SQLite as ticks?

前端 未结 3 1810
攒了一身酷
攒了一身酷 2020-12-10 13:41

I have a SQLite database where I store the dates as ticks. I am not using the default ISO8601 format. Let\'s say I have a table defined as follows:

CREATE TA         


        
3条回答
  •  天涯浪人
    2020-12-10 14:28

    The following will return the number of milliseconds since the UNIX Epoch:

    SELECT (strftime('%s', 'now') - strftime('%S', 'now') + strftime('%f', 'now')) * 1000 AS ticks
    

    It works by grabbing the number of seconds since the Unix Epoch (%s), subtracting the number of seconds in the current time (%S), adding the number of seconds with decimal places (%f), and multiplying the result by 1000 to convert from seconds to milliseconds.

    The subtraction and addition are to add precision to the value without skewing the result. As stated in the SQLite Documentation, all uses of 'now' within the same step will return the same value.

提交回复
热议问题