C++获取当前时间

六月ゝ 毕业季﹏ 提交于 2020-01-11 16:14:14

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

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));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!