【发布时间】:2021-03-19 14:58:59
【问题描述】:
我编写了一个 c++ 函数来获取 HH:MM:SS 格式的当前时间。我怎样才能添加毫秒或纳秒,所以我可以有像HH:MM:SS:MMM 这样的格式?如果不可能,以毫秒为单位返回当前时间的函数也很好。然后我可以自己计算两个日志点之间的相对时间距离。
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;
}
【问题讨论】:
-
std::put_time有%T用于 HH:MM:SS。 -
'...添加毫秒或纳秒...' 呵呵!这是一个很大的维度范围!选择一个:P ...
-
好吧,因为我有一个 ping 客户端,并且在 bash shell 上我们只能访问 ns! :)