sqlite - 格式化儒略日

标签 sqlite julian-date

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

相关文章:

sqlite - Flutter:在我的应用程序中哪里放置 sqflite 代码?

Java Joda-Time DateTime 如何从儒略日期创建日期

java - sqlite 将 Jdn 转换为 unixepoch

python - 如何使用自定义日历进行 timedelta 计算?

c++ - 就像儒略数是用来计算日期有没有具体的数字来计算时间

c# - EF7 使用 SQLite 生成错误的迁移

mysql - Redmine 与 SQL Server 2008 R2

objective-c - iOS SQLite FMDB 事务.. 正确用法?

ruby-on-rails - db :migrate in Rails? 之后的 postgreSQL 文件在哪里