以下方式获取的时间比北京时间晚了八小时:
time_t t = time(nullptr);
struct tm *p;
p = gmtime(&t);
char s[50];
strftime(s, sizeof(s), "%Y-%m-%d-%H:%M:%S", p);
string current_time = s;
photo_data_ = current_time.substr(0, 10);
photo_time_ = current_time.substr(11, 20);
用以下代码可解决上述问题:
struct timeval tv{};
char buf[64];
gettimeofday(&tv, nullptr);
strftime(buf, sizeof(buf) - 1, "%Y-%m-%d %H:%M:%S", localtime(&tv.tv_sec));