What returns ctime ? From cppreference :
Pointer to a static character string with a null character containing a textual representation of the date and time. The string can be split between std::asctime and std::ctime and can be overwritten with every call to any of these functions.
This probably works on your compiler, and then ctime() is called first, then the new ctime() , then both operator<<() get an estimate - which emit the same char* . As a result of an unspecified order, your code has undefined behavior. On some compilers, this might work as you hoped! On yours, this is not so.
If you highlight two calls:
cout << "time now " << ctime(&t1) << endl; cout << " time later " << ctime(&t2) <<endl;
You will surely and consistently see different values.
source share