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

前端 未结 3 1818
攒了一身酷
攒了一身酷 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条回答
  •  猫巷女王i
    2020-12-10 14:17

    After further study of the SQLite documentation and other information found on date number conversions, I have come up with the following formula, which appears to produce correct results:

    INSERT INTO TestDate(LastModifiedTime)
        VALUES(CAST((((JulianDay('now', 'localtime') - 2440587.5)*86400.0) + 62135596800) * 10000000 AS BIGINT))
    

    Seems like a painful way to produce something that I would expect to be available as a built-in datetime format, especially that the database supports the storing of datetime values in ticks. Hopefully, this becomes useful for others too.

    Update:

    The above formula is not perfect when it comes to daylight savings. See section Caveats And Bugs in SQLite docs regarding local time calculation.

提交回复
热议问题