简体   繁体   中英

Output Discrepancy using ctime and time_t

Found a strange discrepancy outputting time_t through ctime while messing around with chrono and durations. I most definitely am missing something here. There is more information about the question after the code and output. I searched around a bit for a similar question, if there is one please direct me to it.

I'm currently running: OS: Windows 7 64-bit CPU: Intel Core i5 M 520 2.40Ghz RAM: 4GB IDE: Microsoft Visual Studio Express 2013

EDIT: Full Source

#include <iostream>
#include <chrono>
#include <ctime>
#include <thread>

using namespace std::chrono;

int main()
{
    duration<int, std::milli> wait(5500);

    time_point<system_clock>  timePoint_Start;
    time_point<system_clock>  timePoint_End;
    time_t                    timeT_Start;
    time_t                    timeT_End;

    std::cout << "01. timePoint_Start Address, sizeof: " << &timePoint_Start << ", " << sizeof(timePoint_Start) << std::endl; 
    std::cout << "02. timePoint_End   Address, sizeof: " << &timePoint_End   << ", " << sizeof(timePoint_End)   << std::endl;
    std::cout << "03. timeT_Start     Address, sizeof: " << &timeT_Start     << ", " << sizeof(timeT_Start)     << std::endl;
    std::cout << "04. timeT_End       Address, sizeof: " << &timeT_End       << ", " << sizeof(timeT_End)       << std::endl;

    timePoint_Start = system_clock::now();     // < = = = = set the time the 1st time
    timeT_Start     = system_clock::to_time_t(timePoint_Start);
    std::cout << "05. Time Start:   " << system_clock::to_time_t(timePoint_Start) << " " << std::ctime(&timeT_Start);

    std::this_thread::sleep_for(wait);

    timePoint_End   = system_clock::now();     // < = = = = set the time the 2nd time
    timeT_End       = system_clock::to_time_t(timePoint_End);
    std::cout << "06. Time End:     " << system_clock::to_time_t(timePoint_End)   << " " << std::ctime(&timeT_End);

    duration<double> elapsed_seconds = timePoint_End - timePoint_Start;

    std::cout << "07. Time Start:   " << system_clock::to_time_t(timePoint_Start)    << " 0x" << &timeT_Start << " " << std::ctime(&timeT_Start)
              << "08. Time Wait:    duration<int, std::milli> wait(5500);"           << std::endl
              << "09. Time End:     " << system_clock::to_time_t(timePoint_End)      << " 0x" << &timeT_End   << " " << std::ctime(&timeT_End)
              << "10. Time Elapsed: " << elapsed_seconds.count() << "s"              << std::endl;

    std::cout << "11. Time End:     " << system_clock::to_time_t(timePoint_End)      << " 0x" << &timeT_End   << " " << std::ctime(&timeT_End);

And here is where the discrepancy occurs in the output EDIT: Full output

01. timePoint_Start Address, sizeof: 0046FAA4, 8
02. timePoint_End   Address, sizeof: 0046FA94, 8
03. timeT_Start     Address, sizeof: 0046FA84, 8
04. timeT_End       Address, sizeof: 0046FA74, 8
05. Time Start:   1402880407 Sun Jun 15 20:00:07 2014
06. Time End:     1402880413 Sun Jun 15 20:00:13 2014
07. Time Start:   1402880407 0x0046FA84 Sun Jun 15 20:00:07 2014
08. Time Wait:    duration<int, std::milli> wait(1500);
09. Time End:     1402880413 0x0046FA74 Sun Jun 15 20:00:07 2014
10. Time Elapsed: 5.50932s
11. Time End:     1402880413 0x0046FA74 Sun Jun 15 20:00:13 2014

It is my understanding that line 6, line 9, and line 11 of the output should match in regards to its time stamp. Line 9 of the output seems like it is picking up the wrong reference. Is there something syntactically wrong here?

I'm a rookie, but this seems like really strange behavior. If I am in the wrong section please direct me to the right one. Really want to know what is happening here.

The std::ctime() function comes from the C standard, I believe, and in the C standard, it returns a pointer to a static memory location. This can only store one value at a time. Since you have a single humongous std::cout << ... invocation that uses std::ctime() 3 times, you are out of luck; you will get one value displayed in all three places, not 3 different values (in general — though the details depend on how the calls to ctime() are interleaved with the calls to the << operator, which depends on the compiler).

The simplest way to demonstrate this is to revise the code so that the lines labelled 7, 8, 9, 10 are separate statements.

The longer term fix will require a little more thought.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM