JavaRunner
JavaRunner

Reputation: 2545

C++ time() gives me almost random result

I wrote a following simple code:

time_t makeUnixTimeStamp( int year, int month, int day, int hour, int min, int sec ) {
    tm uts_time;
    uts_time.tm_year = year - 1900;
    uts_time.tm_mon = month - 1;
    uts_time.tm_mday = day;
    uts_time.tm_sec = sec;
    uts_time.tm_min = min;
    uts_time.tm_hour = hour;
    return mktime( &uts_time );
}

std::string getReadableDateTime( unsigned int unixTimeStamp ) {
    char dateTime[ 40 ];
    time_t someTime = unixTimeStamp;
    struct tm *mTime;
    mTime = localtime( &someTime );
    strftime( dateTime, sizeof( dateTime ), "%Y-%m-%d %H:%M:%S", mTime );
    return std::string( dateTime ); 
}


unsigned int startLogTime = makeUnixTimeStamp( 2016, 05, 04, 00, 00, 00 );
time_t nowTime;
time( &nowTime );
std::cout << "readable Time = " << getReadableDateTime( startLogTime ) << '\n';

I get strange output after a few runs. I do php -r 'echo time();' for show current second. Why have I different "readable time" if I don't change anything in my code?

Output:

15:20:58 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450865

15:21:05 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450866

15:21:06 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450867

15:21:07 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-03 23:00:00
1462450868

15:21:08 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-03 23:00:00
1462450869

15:21:09 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450871

15:21:11 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-03 23:00:00
1462450872

15:21:12 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450877

15:21:17 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450882

15:21:22 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-03 23:00:00
1462450883

Seems that if I remove time() function - it works better but I need it after that code.

Upvotes: 2

Views: 92

Answers (2)

ravenspoint
ravenspoint

Reputation: 20457

You should set the DST flag. It is probably being randomly initialized

The Daylight Saving Time flag (tm_isdst) is greater than zero if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and less than zero if the information is not available.

http://www.cplusplus.com/reference/ctime/tm/

A useful recipe is to fist initialize the tm structure with the current local time, so that this gets set the same way as everything else on your machine.

time_t now = time(0);
uts_time = * localtime( &now );
// initialise with the time you really want

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234635

You have some uninitialised parts of your tm structure: the behaviour on reading back any uninitialised portion is undefined.

Use code like tm foo{}; instead which causes all structure elements to be initialised with zero values (and pointers to null pointer values).

Upvotes: 1

Related Questions