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 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条