【发布时间】:2016-07-10 04:23:57
【问题描述】:
我正在使用 sqlite 记录时间戳
INSERT INTO packets VALUES ( strftime('%%J','now') );
然后提取经过的时间
SELECT strftime('%%J','now') - first_timestamp FROM packets;
效果很好。如果我等待一分钟,结果是 0.0007 ( ~= 1 * 60 / 24 * 60 * 60 )
我希望以小时、分钟和秒为单位查看此内容,但是
sqlite> SELECT time(0.0007);
12:01:00
12 是从哪里来的?
这“有效”
sqlite> SELECT time(0.0007-0.5);
00:01:00
但使用起来似乎太奇怪了。
根据 CL 的解释,我提交了这段代码
std::string TimeSinceFirstPacket()
{
// open database
Open();
// read timestamp of first packet
// this is stored as a Julian day for convenince in calculating and formatting the elapsed time
// Note that for Julian days
// 1.0 is 24 hours
// -0.5 represents the previous midnight
// discussion at http://stackoverflow.com/q/38284268/16582
int dbret = DB.Query(
" SELECT "
" first_timestamp, "
" ( strftime('%%J','now') - first_timestamp ) < 1.0, "
" time( strftime('%%J','now') - first_timestamp - 0.5 ) "
" FROM packets;");
// check for successful db read
if( dbret != 1 )
return "error";
// check that timestamp has been initialized
if( DB.myResultA[ 0 ] == "0" )
return "none";
// check that elapsed time is less than 24 hours
if( DB.myResultA[ 1 ] == "0" )
return ">24hr";
// return human readable hh::mm::ss elapsed time
return DB.myResultA[ 2 ];
【问题讨论】:
标签: sqlite julian-date