std::mktime return QNX -1

std::mktime return -1 on QNX

C++11

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

在 Ubuntu 一切正常。但是在 QNX mktime returns -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;
}

P.S。尝试将 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;