C++获取当前时间

以下方式获取的时间比北京时间晚了八小时:

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));
发布了40 篇原创文章 · 获赞 3 · 访问量 1417

猜你喜欢

转载自blog.csdn.net/hongge_smile/article/details/103934594
今日推荐