I insert timestamps in a column of type real. The timestamps are in unixepoch format, eg 1505720496876 which is GMT: Monday, September 18, 2017 7:41:36.876 AM.
However, when I perform the query
select datetime(timestamp, 'unixepoch', 'localtime') from history I get -1413-03-01 14:07:12 as result.
What am I doing wrong?
You are currently using milliseconds but you need to use seconds:
select datetime(timestamp / 1000, 'unixepoch', 'localtime')
from history
From the documentation:
The "unixepoch" modifier (11) only works if it immediately follows a timestring in the DDDDDDDDDD format. This modifier causes the DDDDDDDDDD to be interpreted not as a Julian day number as it normally would be, but as Unix Time - the number of seconds since 1970.
Related
Querying the SQLite database of my Android app, I'm running into a curious problem. I've got a query that checks one epoch time (stored in the database in an integer field) against the current epoch time. To simplify debugging, I wrote a little test query to see how SQLite handles my epoch integer versus the strftime function. Here's my query:
select 1615759200 < strftime('%s', 'now');
The result is:
1
For reference: the value 1615759200 is the date March 14, 2021. The current date at time of writing is April 28, 2020 which is roughly 1588033692 in epoch time. For obvious reasons, I'm expecting the above query to result 0: false, as the date in 2021 is NOT smaller than the date in 2020. And yet, it returns 1: true! It's infuriating! Can anyone tell me what I'm missing here?
The data type that is returned by strftime() is text and you must cast it to an integer so that the comparison will work the way that you expect:
select 1615759200 < cast(strftime('%s', 'now') as int)
or:
select 1615759200 < strftime('%s', 'now') + 0
You can find more about data types and affinities of SQLite: Datatypes In SQLite Version 3
based on the SQLite documentation, there is time() that is equivalent with strftime('%H:%M:%S', ...). SO i tested it with my query
time(time)
but after run the query, all the result become 12:00:00
the data is basically in milliseconds (25000, 5000, 15000)
What is the problem??
The built-in date/time functions interpret numbers as Julian day numbers, unless you use the unixepoch modifier to specify seconds:
SELECT time(time / 1000, 'unixepoch');
I need to calculate the time between a 'fixed' time (usually a few days ago) and now.
Therefore I have googled me to this sql (http://www.sqlite.org/lang_datefunc.html):
SELECT strftime ('%s', '1423143532') - strftime ('%s', 'now') as timediff;
It gives a weird result (122747310831610), and I have no idea why !? :-/
Should I use a different method of calculation, is that where the problem is? If yes, which one would give me the correct result? :)
A bare number is interpreted as a Julian day number:
> SELECT datetime('1423143532');
3891722-46318118-31 12:00:00
> SELECT datetime(2457064);
2015-02-10 12:00:00
To interpret a number as the number of seconds since 1970, you must use the unixepoch modifier:
> SELECT datetime(1423143532, 'unixepoch');
2015-02-05 13:38:52
> SELECT strftime ('%s', 1423143532, 'unixepoch');
1423143532
As you can see, passing this number through strftime is pointless. Just use it directly:
> SELECT 1423143532 - strftime('%s', 'now') as timediff;
-436916
I'm storing my dates in SQLite in a column of data type INTEGER. I'm storing the milliseconds since 1970.
Eg:
date (long) other columns ...
-----------------------------------------
1407297600000 ...
1407211200000 ...
1407124800000 ...
My question is: how can I use strftime() under this circumstances?
If not, I should use TEXT as the column type??
Running this:
select strftime('%Y-%m', date) from my_table;
Is throwing nonesense stuff:
strftime('%Y-%m', date)
-----------------------------------------
1968-19
1968-19
1968-19
Unless you tell it otherwise, strftime() thinks those numbers are Julian day values - very different from Unix epoch milliseconds.
You'll want to convert to seconds, and tell strftime() these are Unix epoch numbers:
select strftime('%Y-%m', date / 1000, 'unixepoch');
See the Modifiers section in the SQLite Date and Time Functions docs.
I've inherited a SQLite DB, in it I've a TIMESTAMP field called ZDATE.
One value is 401,580,000 and I know it correspond to Sept 23rd, 2013.
I've calculated that the 0 of this field is the midnight of Jan 1st, 2001 (?).
However, I didn't find any conversion function to get a formatted date, in fact if I use date() I get:
ZDATE date(zdate)
401580000 1094776-12632211-20
Any help will be appreciated.
> select 401580000 / (julianday('2013-09-23') - julianday('2001-01-01'));
86398.4509466437
> select 60*60*24;
86400
So this timestamp appears to use seconds.
To convert it into a timestamp that SQLite can use directly, i.e., a Unix epoch timestamp, just add the appropriate offset:
> select datetime(401580000 + strftime('%s', '2001-01-01 02:00:00'), 'unixepoch');
2013-09-23 00:00:00