C++: how to get the actual time with time and loca

2019-04-04 09:14发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

localtime has what is best considered a legacy interface. It can't be used in multithreaded code, for example. In a multithreaded environment, you can use localtime_r under Posix or localtime_s under Windows. Otherwise, all you have to do is save the results:

tm then = *localtime( &t1 );
//  ...
tm now = *localtime( &t2 );

It would probably be more idiomatic, however, to only call localtime
immediately before formatting the output, e.g.:

std::string
timestampToString( time_t timeAndDate )
{
    char results[100];
    if ( strftime( results, sizeof( results ), "%Y-%m-%d, %H:%M:%S", 
                localtime( &timeAndDate) ) == 0 ) {
        assert( 0 );
    }
    return results;
}

and then writing:

std::cout << formatTime( t1 ) << std::endl;

(You could also create a more generic formatting function, which took the format as an argument.)



回答4:

You can run continuous clock using following code. It works nicely.

#include<iostream> 
#include <Windows.h> 
#include<ctime> 
using namespace std;

void main() {
  while(true) {
    system("cls"); //to clear screen
    time_t tim;
    time(&tim); 
    cout << ctime(&tim); 
    Sleep(1);
  }
}