How to use ctime () correctly for printing different time stamps

I expected the following code to print different timestamps t1 and t2, however the result shows that t1 and t2 are the same. Where did I make a mistake?

#include<iostream> #include<ctime> using namespace std; int main() { time_t t1 = time(NULL); cout << "time now " << ctime(&t1) << endl; time_t t2 = t1 + 10000.0; cout << "time now " << ctime(&t1) << endl << " time later " << ctime(&t2) <<endl; } 

Result:

 time now Thu Apr 28 20:37:03 2016 time now Thu Apr 28 20:37:03 2016 time later Thu Apr 28 20:37:03 2016 
+5
source share
3 answers

The answer to your question can be found in the manual page for the ctime () function :

The return value indicates a statically allocated string that can be overwritten by subsequent calls to any date and time of the function.

ctime () returns a pointer to the internal buffer that it uses. Each time it is called, it returns a pointer to the same buffer:

  cout << "time now " << ctime(&t1) << endl << " time later " << ctime(&t2) <<endl; 

For this line of code, your compiler generated code that calls ctime() twice, then executes the << statement. But the second time ctime() called, it rewritten the buffer a second time, therefore, when the << operator formats the output, because the result of the first call to ctime() is the same pointer, and the buffer that it indicates was overwritten by the second call ctime() , you print twice twice.

Thank you for posting a minimal, complete, and verifiable example .

+4
source

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.

+3
source

Quote from N1570 7.27.3 Time conversion functions:

With the exception of the strftime function, these functions return a pointer to one of two types of static objects: a broken time structure or a char array. The execution of any of the functions that return a pointer to one of these types of objects can overwrite information in any object of the same type as indicated by the value returned from any previous call for any of them, and functions are not required to avoid data racing with each other friend.

This suggests that the contents indicated by what is returned from ctime() can be overwritten by another call to ctime() , so you have to copy the result to use the result in one expression without a sequence point in that.

Try the following:

 #include<iostream> #include<ctime> #include<string> using namespace std; int main() { time_t t1 = time(NULL); cout << "time now " << ctime(&t1) << endl; time_t t2 = t1 + 10000.0; string t1s = ctime(&t1); string t2s = ctime(&t2); cout << "time now " << t1s << endl << " time later " << t2s <<endl; } 
+2
source

All Articles