1

I need that every time I make a change to a certain record from a table in SQLite, in the column lastChangedDate to set the time from France. Here is the structure of my table :

CREATE TABLE name(
    id VARCHAR(36) PRIMARY KEY
    , pos_report_id VARCHAR(36)
    , path_name VARCHAR(64)
    , photo_name VARCHAR(64)
    , from_scratch INTEGER DEFAULT 0
    , lastChangedDate DATETIME DEFAULT (DATETIME('now', 'utc', '1 hours'))
)

I see that DATETIME('now') returns 2 hours before my real time and DATETIME('now', 'utc', '1 hours') returns with 3 hours before my time. Why is happening this? I need the application to work in more countries, so I cannot use localtime.

Any idea how to solve this?

4

2 回答 2

5

我有同样的问题(在 Raspbarry Pi 上使用 sqlite)。'utc' 显然只根据位置和时区计算差异。我让它像这样运行:

select datetime(datetime('now', 'localtime'), 'utc');

另请查看我的 VIEW [NOW] 的其他变体以用于传感器记录。

CREATE VIEW [NOW] AS 
SELECT datetime('now', 'localtime') as LOCAL,
datetime(datetime('now', 'localtime'),'utc') as UTC, 
substr(datetime('now', 'localtime'),1,17)||'00' as TimeSlot_1min, 
substr(datetime('now', 'localtime'),1,15)||'0:00' as TimeSlot_10min, 
substr(datetime('now', 'localtime'),1,14)||'00:00' as TimeSlot_1h;
于 2014-04-08T16:59:39.787 回答
2

尝试 datetime('now','localtime') 而不是 DATETIME('now', 'utc', '1 hours') 好的,不要使用 lastChangedDate 的默认时间

CREATE TABLE name(
    id VARCHAR(36) PRIMARY KEY
    , pos_report_id VARCHAR(36)
    , path_name VARCHAR(64)
    , photo_name VARCHAR(64)
    , from_scratch INTEGER DEFAULT 0
    , lastChangedDate DATETIME)
)

然后当您想将记录添加到表中时,您可以计算法国的时间并将此值添加到您的数据库中

//Calendar cal = Calendar.getInstance();
//Log.v("hata",String.valueOf(cal.get(Calendar.HOUR)));

Calendar c = Calendar.getInstance();

// It is local time
Log.v("time",String.valueOf(c.getTime()));

TimeZone z = c.getTimeZone();       
int offset = z.getRawOffset();

if(z.inDaylightTime(new Date())){
    offset = offset + z.getDSTSavings();
}

// france is GMT2 
int offsetHrs = offset / 1000 / 60 / 60;
int offsetMins = offset / 1000 / 60 % 60;

// Offsets
Log.v("time",String.valueOf(offsetHrs));
Log.v("time",String.valueOf(offsetMins));

c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
c.add(Calendar.MINUTE, (-offsetMins));

// FRANCE time
Log.v("time",String.valueOf(c.getTime()));
于 2012-12-07T10:08:45.697 回答