c++ - 如何使用 C++/C++11 打印当前时间(以毫秒为单位)

标签 c++ linux windows time c++11

目前我使用此代码

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/

相关文章:

windows - 如何将尖括号等特殊字符输出到 HTML/XHTML/XML 文件中?

c - SetConsoleCursorPosition : Getting black rows in console upon use

c++ - 如何轻松实现成员函数作为模板参数?

c++ - 痛饮 Perl : wrong ELF class

c++ - Pthread 程序运行速度随着线程的增加而变慢

linux - 无法在 VirtualBox 中为 CentOS 6 guest 安装 Guest Additions

c++ - AES CBC解密过程后解密的文件有奇怪的字符

linux - 如何连接两个 linux 命令的结果?

linux - 如何正确等待 yum lock 被释放?

python - 在Python中执行带空格的Windows路径