in my table in sqlite i have stored a date in timestamp in gmt time zone, in this way:
CREATE TABLE task (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
date INTEGER,
name TEXT
);
when i query the database i want retrieve the date with the local time so:
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
the now timestamp is of my local time zone, so for example if it's possible i want do this:
SELECT id,date,name FROM task WHERE converttolocal(date) = now;
i want know if exist a function in sqlite like for instance converttolocal
to automatically convert the timestamp...
EDIT
i found this:
strftime('%s',date, 'unixepoch', 'localtime')
but if for instance the date gmt in the database is: 1375660800
that is:
GMT: Mon, 05 Aug 2013 00:00:00 GMT
after the function strftime the result is: 1375668000
that correspond at my local time:
(CEST) Mine time zone: 05 agosto 2013 04:00:00
but i want that the result of the strftime function give me this: 1375653600
that is:
Mine time zone: 05 agosto 2013 00:00:00 CEST
Why doesn't give me this result?
To convert from a Unix timestamp to SQLite's default date format, use the unixepoch
modifier.
To convert a time from UTC to local time, use the localtime
modifier.
To convert from SQLite's default date format to a Unix timestamp, use strftime with the %s
format:
SELECT id,date,name FROM task WHERE strftime('%s', date, 'unixepoch', 'localtime') = ?
It might be easier to use SQLite's built-in method to get the current time, which already is in UTC:
SELECT id,date,name FROM task WHERE datetime('%s', date, 'unixepoch') = datetime('now')
However, if you have only dates, you want to ignore the time portion.
In that case, use date
instead of datetime
:
SELECT id,date,name FROM task WHERE date('%s', date, 'unixepoch') = date('now')