我正在使用 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 ];
最佳答案
儒略日不是从午夜开始计算,而是从中午开始计算:
> select julianday('2000-01-01 00:00:00');
2451544.5
> select julianday('2000-01-01 12:00:00');
2451545.0
因此,要获取自午夜以来的时间,您必须计算您的数字与代表午夜的数字之间的差异。
关于sqlite - 格式化儒略日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38284268/