【问题标题】:Formatting Julian days格式化儒略日
【发布时间】: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


    【解决方案1】:

    儒略日不是从午夜开始计算,而是从中午开始计算:

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

    因此,要获取自午夜以来的时间,您必须计算您的数字与代表午夜的数字之间的差。

    【讨论】:

    • 所以代表前一个午夜的数字是-0.5
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 2014-09-06
    • 1970-01-01
    相关资源
    最近更新 更多