格式化儒略日

时间:2016-07-09 17:10:46

标签: sqlite julian

我正在使用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 ];

1 个答案:

答案 0 :(得分:1)

朱利安的日子不计入午夜,而是从正午:

> select julianday('2000-01-01 00:00:00');
2451544.5
> select julianday('2000-01-01 12:00:00');
2451545.0

所以为了得到午夜以来的时间,你必须计算你的数字和代表午夜的数字之间的差异。