获取当前时间(以毫秒为单位)或HH:MM:SS:MMM格式 [英] Get current time in milliseconds, or HH:MM:SS:MMM format
本文介绍了获取当前时间(以毫秒为单位)或HH:MM:SS:MMM格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个c ++函数,以 HH:MM:SS
格式获取当前时间。如何添加毫秒或纳秒,因此可以使用 HH:MM:SS:MMM
这样的格式?如果不可能的话,返回以毫秒为单位的当前时间的函数也是不错的选择。然后,我可以自己计算两个日志点之间的相对时间距离。
I've written a c++ function to get the current time in HH:MM:SS
format. How can I add milliseconds or nanoseconds, so I can have a format like HH:MM:SS:MMM
? If not possible, a function that returns current time in ms would also be good. I can then calculate the relative time distances between two log points myself.
string get_time()
{
time_t t = time(0); // get time now
struct tm * now = localtime(&t);
std::stringstream sstm;
sstm << (now->tm_hour) << ':' << (now->tm_min) << ':' << now->tm_sec;
string s = sstm.str();
return s;
}
推荐答案
HowardHinnant的 date 库。
This is a cleaner solution using HowardHinnant's date library.
std::string get_time()
{
using namespace std::chrono;
auto now = time_point_cast<milliseconds>(system_clock::now());
return date::format("%T", now);
}
这篇关于获取当前时间(以毫秒为单位)或HH:MM:SS:MMM格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文