I am storing a timestamp field in a SQLite3 column as TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP
and I was wondering if there was any way for it to include milliseconds in the timestamp as well?
-
TIMESTAMP is number of seconds that passed since 1st Jan 1970 so guess you will need another field for miliseconds– fswCommented Jul 10, 2013 at 15:35
-
POSIX time is defined as elapsed milliseconds since midnight 01-Jan-1970 UTC. I would say that using milliseconds as a timestamp is ok as long as you're sure it's POSIX time (i.e. in UTC). Otherwise, the use of milliseconds for local timestamps is very unusual and you should use other numeric temporal formats instead. See: stackoverflow.com/questions/32670064/…– scottbCommented Sep 25, 2015 at 19:47
6 Answers
Instead of CURRENT_TIMESTAMP
, use (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW'))
so that your column definition become:
TIMESTAMP DATETIME DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW'))
For example:
CREATE TABLE IF NOT EXISTS event
(when_ts DATETIME DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')));
-
With Oracle background the
%f
looks weird by itself, it's the same as Oracle'sTO_DATE(... SS.FFF')
Commented Aug 11, 2014 at 23:02 -
...%H:%M:%f
that's weird, why isn't it...%H:%M:%S.%f
for seconds, and milliseconds? thef
indicates seconds is a double truncated to 6 decimal places, rather than an int? ok Commented Dec 16, 2022 at 16:43 -
From sqlite.org/lang_datefunc.html,
%f fractional seconds: SS.SSS
– LS_ᴅᴇᴠCommented Dec 20, 2022 at 10:57
To get number of milliseconds since epoch you can use julianday()
with some additional calculations:
-- Julian time to Epoch MS
SELECT CAST((julianday('now') - 2440587.5)*86400000 AS INTEGER);
-
2Add ROUND to get more accurate thousandths: CAST(ROUND((julianday('now') - 2440587.5)*86400000) As INTEGER)– celoftisCommented Oct 12, 2018 at 20:11
The following method doesn't require any multiplies or divides and should always produce the correct result, as multiple calls to get 'now' in a single query should always return the same result:
SELECT strftime('%s','now') || substr(strftime('%f','now'),4);
The generates the number of seconds and concatenates it to the milliseconds part from the current second+millisecond.
-
1Can it produce a wrong result when the two
strftime
calls happen with a tiny delay and belong to different seconds?– algridCommented Apr 20, 2022 at 16:29 -
1@algrid A query is declarative, so the actual way the query is done is not specified; so in practice (in SQLite) it's not actually making multiple calls at different times.– MichaelCommented Apr 20, 2022 at 18:04
Here's a query that will generate a timestamp as a string with milliseconds:
select strftime("%Y-%m-%d %H:%M:%f", "now");
If you're really bent on using a numeric representation, you could use:
select julianday("now");
-
3fyi, julianday() is not natively comparable with anything based on epoch time, whether seconds or milliseconds. You'll have to convert everywhere in your code.– Ed JCommented May 13, 2018 at 19:16
The accepted answer only gives you UTC. If you need a local time instead of UTC, use this:
strftime('%Y-%m-%d %H:%M:%f', 'now', 'localtime')
Another epoch ms method for sqlite > 3.42.0 which added subsecond
SELECT unixepoch('subsecond') * 1000;
1703231951563.0
The value is a real
, but in an integer
column the value will be cast.
CREATE TABLE "items" (
"created_at" integer default(unixepoch('subsecond') * 1000) not null
);
As in cast(x as int)
SELECT cast(unixepoch('subsecond') * 1000 as int);
1703231955581