3

C++11

Have a cross platform code with usage of std::mktime on Ununtu 18 (x64) and QNX (x64)

On Ubuntu everything is ok. But on QNX mktime returns -1. errno = 3 What's wrong?

#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. Tried init struct tm as:

struct std::tm date_tm = {0};

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

Some notes:

1) if I call mktime twice - everything will be ok (will receive time)

2) tm structure print

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

Are there any other (easy and fast) ways to get time in seconds for my case?

Holiday
  • 31
  • 4

1 Answers1

0

On QNX device this problem was fixed by setting to tm structure tm_isdst = 1 and tm_zone (GMT)

Unfortunately it broke my Ubuntu build (due to timezone, on my local PC it was EET).

Replaced receiving seconds by 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;

Holiday
  • 31
  • 4