dongtui6347 2015-09-02 22:32
浏览 236

将C ++ std :: chrono :: duration_cast <std :: chrono :: milliseconds>转换为PHP DateTime

I´m writing an application composed of a C++ process that acquires data and stores it in a Sqlite3 database, and a PHP Web application to analyze that data.

Some collected data generates DateTime formats, that are stored on a INTEGER type field on Sqlite3.

At the C++ side, the following piece of code is used to generate the timestamps (in milliseconds precision, as I´m dealing with a real time system):

int64_t convertTimePointToInt64(const std::chrono::time_point<std::chrono::system_clock> &time)
{
    auto count = time.time_since_epoch();
    auto value = std::chrono::duration_cast<std::chrono::milliseconds>(count);

    return value.count();
}

std::chrono::time_point<std::chrono::system_clock> dt = std::chrono::system_clock::now();
auto valueToStore = convertTimePointToInt64(dt);

The valueToStore is then INSERT´ed on Sqlite using SQL (INTEGER type).

My problem is when I try to read it on the Web application. I´m using the following PHP code, where $t is the database INTEGER value:

$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new \DateTime( date('Y-m-d H:i:s.'.$micro, $t) );
$a = $d->format("Y-m-d H:i:s.u");

I´m getting stange values (1031 in year, for example) that indicates the PHP logic is messy.

The C++ code is running very fine (I can store and retrive timestamps and convert it to/back to std::string). I need to fix my PHP code to show the data correctly.

Help appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题