Linux下可以通过:
struct tm * gmtime (const time_t * timer); //获得gmt(UTC)时间
struct tm * localtime (const time_t * timer); //获得本地时间
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
string getTimeStr(struct tm* t)
{
char tStr[50] = {0};
snprintf(tStr, sizeof(tStr), "%04d-%02d-%02d %02d:%02d:%02d", (1900 + t->tm_year), ( 1 + t->tm_mon), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
return string(tStr);
}
int main(){
time_t ts = time(nullptr);
struct tm* tUTC = gmtime(&ts);
cout<<"UTC time:"<<getTimeStr(tUTC)<<endl;
struct tm* tLocal = localtime(&ts);
cout<<"Local time:"<