目前我使用此代码
string now() {
time_t t = time(0);
char buffer[9] = {0};
strftime(buffer, 9, "%H:%M:%S", localtime(&t));
return string(buffer);
}
格式化时间。我需要添加毫秒,所以输出格式为:16:56:12.321
最佳答案
不要在 Boost 上浪费时间(我知道很多人会被这种说法冒犯并认为它是异端邪说)。
此讨论包含两个非常可行的解决方案,它们不需要您将自己束缚于非标准的 3rd 方库。
C++ obtaining milliseconds time on Linux -- clock() doesn't seem to work properly
http://linux.die.net/man/3/clock_gettime
可以在 opengroup.org 上找到对 gettimeofday 的引用
关于c++ - 如何使用 C++/C++11 打印当前时间(以毫秒为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16077299/