Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: how to get the actual time with time and localtime?

I'm looking for a way to save the time in a HH::MM::SS fashion in C++. I saw here that they are many solutions and after a little research I opted for time and localtime. However, it seems like the localtime function is a little tricky, since it says:

All calls to localtime and gmtime use the same static structure, so each call overwrites the results of the previous call.

The problem that this causes is shown in the next snippet of code:

#include <ctime>
#include <iostream>
using namespace std;

int main() {
time_t t1 = time(0);   // get time now
struct tm * now = localtime( & t1 );

std::cout << t1 << std::endl;
sleep(2);
time_t t2 = time(0);   // get time now
struct tm * now2 = localtime( & t2 );
std::cout << t2 << std::endl;

cout << (now->tm_year + 1900) << '-'
     << (now->tm_mon + 1) << '-'
     <<  now->tm_mday << ", "
     << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec
     << endl;

cout << (now2->tm_year + 1900) << '-'
     << (now2->tm_mon + 1) << '-'
     <<  now2->tm_mday << ", "
     << now2->tm_hour << ":" << now2->tm_min << ":" << now2->tm_sec
     << endl;
}

A typical output for this is:

1320655946
1320655948
2011-11-7, 9:52:28
2011-11-7, 9:52:28

So as you can see, the time_t timestamps are correct, but the localtime messes everything up.

My question is: how do I convert a timestamp ot type time_t into a human-readable time?

like image 957
seb Avatar asked Nov 07 '11 09:11

seb


People also ask

How does time () function work in C?

The time() function is defined in time. h (ctime in C++) header file. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.

How do I print the time in printf?

printf("The time is: %02d:%02d:%02d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); We use the tm_hour , tm_min , and tm_sec members to express a human-readable time format.


2 Answers

If you are worried about reentrancy in localtime and gmtime, there is localtime_r and gmtime_r which can handle multiple calls.

When it comes to formatting the time to your liking, check the function strftime.

like image 69
Some programmer dude Avatar answered Oct 21 '22 14:10

Some programmer dude


the localtime() call stores the results in an internal buffer.

Every time you call it you overwrite the buffer.
An alternative solution would be to make a copy of the buffer.

time_t      t1  = time(0);           // get time now
struct tm* now  = localtime( & t1 ); // convert to local time
struct tm  copy = *now;              // make a local copy.
 //     ^^^ notice no star.

But note: The only time you should be converting to local time is when you display the value. At all other times you should just keep the time as UTC (for storage and manipulation). Since you are only converting the objects for display convert then print immediately and then things will not go wrong.

like image 26
Martin York Avatar answered Oct 21 '22 15:10

Martin York