【发布时间】:2020-12-04 17:59:56
【问题描述】:
我可以得到当前纪元的毫秒数:
#include <chrono>
#include <cstdint>
#include <iostream>
#include <ctime>
uint64_t timeSinceEpochMillisec() {
using namespace std::chrono;
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
int main() {
std::cout << timeSinceEpochMillisec() << std::endl;
return 0;
}
这将输出:1597436329290。
我有一个 sn-p 代码,它使用以秒为单位的纪元时间,效果很好。如何使用 timeSinceEpochMillisec() 使用 strftime 格式化日期和时间?
std::time_t epoch_timestamp = std::time(nullptr);
char buf[80];
std::tm *ts = std::gmtime(&epoch_timestamp);
strftime(buf, sizeof(buf), "%m/%d/%Y %H:%M:%S", ts);
std::cout << buf << std::endl;
输出:
1597437386198
08/14/2020/ 20:36:26
【问题讨论】: