c++ - QNX 上的 std::mktime 返回 -1

标签 c++ function time std qnx

C++11

在 Ununtu 18 (x64) 和 QNX (x64) 上使用 std::mktime 的跨平台代码

在 Ubuntu 上一切正常。但在 QNX 上,mktime 返回 -1。错误号 = 3 怎么了?

#include <iostream>
#include "ctime"


int main(int argc, char *argv[]){

    const std::string token = "100901";

    std::string dd = token.substr( 0, 2 );
    std::string mm = token.substr( 2, 2 );
    std::string yy = token.substr( 4, 2 );

    struct std::tm date_tm = {};

    date_tm.tm_mday = std::stoi( dd);
    date_tm.tm_mon = std::stoi( mm) - 1;
    date_tm.tm_year = std::stoi( yy) + 100;

    if (std::mktime( &date_tm ) == -1);
    {
        std::cout << "Error";
    }

    return 0;
}

附言尝试将 init struct tm 设置为:

struct std::tm date_tm = {0};

std::memset( &date_tm, 0, sizeof( date_tm ) )

一些注意事项:

1) 如果我两次调用 mktime - 一切都会好起来的(会收到时间)

2) tm结构打印

tm_gmtoff: 0
tm_hour: 0
tm_isdst: 0
tm_mday: 10
tm_min: 0
tm_mon: 8
tm_sec: 0
tm_wday: 0
tm_yday: 0
tm_year: 101

是否有任何其他(简单且快速)的方法可以让我的案件在几秒钟内得到时间?

最佳答案

在 QNX 设备上,通过设置 tm 结构 tm_isdst = 1 和 tm_zone (GMT) 解决了这个问题

不幸的是,它破坏了我的 Ubuntu 构建(由于时区,在我的本地 PC 上它是 EET)。

用 boost::poxis_time 代替接收秒数

    struct tm date_tm;
    std::memset( &date_tm, 0, sizeof( date_tm ) );
    strptime(token.c_str(), "%d%m%y", &date_tm);

    boost::posix_time::time_duration diff
        = boost::posix_time::ptime( boost::gregorian::date_from_tm( date_tm ) )
          - boost::posix_time::ptime( boost::gregorian::date( 1970, 1, 1 ) );

    date = diff.ticks( ) / boost::posix_time::time_duration::rep_type::ticks_per_second;

关于c++ - QNX 上的 std::mktime 返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56871199/

相关文章:

c++ - 使用 std::mutex 时遇到问题

c++ - 模板作为参数类型

javascript - 从外部脚本调用 Js 脚本函数

c++ - 实现功能的麻烦(递归)

c++ - 使用 static_cast 的无效类型转换,我应该使用什么正确的转换?

c++ - 了解 N3690 草案中的 §11.2-4

python - Timeit 模块 - 将对象传递给设置?

计算C中的时间差

c++ - 将午夜后的毫秒数转换为时间对象

c++ - MongoDB c++ 遗留驱动程序 1.1.2 不工作 - 使用 VS2013、Win 10、boost-1_62 编译