【问题标题】:Get current time in milliseconds, or HH:MM:SS:MMM format以毫秒为单位获取当前时间,或 HH:MM:SS:MMM 格式
【发布时间】: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! :)

标签: c++ date


【解决方案1】:

这是使用C++11 chrono 库的可移植方法:

#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <string>

// ...

std::string time_in_HH_MM_SS_MMM()
{
    using namespace std::chrono;

    // get current time
    auto now = system_clock::now();

    // get number of milliseconds for the current second
    // (remainder after division into seconds)
    auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;

    // convert to std::time_t in order to convert to std::tm (broken time)
    auto timer = system_clock::to_time_t(now);

    // convert to broken time
    std::tm bt = *std::localtime(&timer);

    std::ostringstream oss;

    oss << std::put_time(&bt, "%H:%M:%S"); // HH:MM:SS
    oss << '.' << std::setfill('0') << std::setw(3) << ms.count();

    return oss.str();
}

【讨论】:

  • 真的吗?你得到 3 位数的毫秒数?
  • 这个解决方案几乎是可移植的。但是,并非每个操作系统都支持每个转换说明符。在我的 Windows 7 机器上,当尝试使用 %F%T 之类的说明符时,我收到错误 "Invalid parameter passed to C runtime function."。我不得不分别用%Y-%m-%d%H:%M:%S 替换它们。
  • @Sebastian 它应该可以在C++11 兼容的编译器之间移植。你的编译器相当老吗?也许它有一个不完整的C++11 实现?我对代码进行了更改,以便更多人使用,谢谢指出。
  • 编译器是新的:$ g++ --version g++.exe (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0 这是导致问题的运行时。如果我有时间,我会更多地研究它,但现在它对我有用,没有像 %F%T 这样的聚合格式代码。
  • 如果您像我一样被困在 GCC 4 上,只是一个警告:std::put_time 在 GCC 5 之前没有实现。
【解决方案2】:

这是使用 HowardHinnant 的 date 库的更简洁的解决方案。

std::string get_time()
{
    using namespace std::chrono;
    auto now = time_point_cast<milliseconds>(system_clock::now());
    return date::format("%T", now);
}

【讨论】:

    【解决方案3】:

    对于windows可能:

    #include <iostream>
    #include <Windows.h>
    #include <strsafe.h>
    
    int main()
    {
        CHAR sysTimeStr[13] = {};
        SYSTEMTIME systemTime;
        GetLocalTime(&systemTime);
        sprintf_s(sysTimeStr,
            "%u:%02u:%02u:%03u",
            systemTime.wHour,
            systemTime.wMinute,
            systemTime.wSecond,
            systemTime.wMilliseconds);
    
        std::cout << sysTimeStr;
    }
    

    【讨论】:

      【解决方案4】:

      尝试使用gettimeofday(),而不是使用time()(自纪元以来的秒数)。为您提供一个包含微秒字段的结构。

      【讨论】:

      • 或者在 C++11 中使用&lt;chrono&gt;
      • 请注意,gettimeofday 是一个 POSIX 函数,而不是标准 C++。该解决方案不能移植到非 POSIX 系统;一般来说,应该首选其他答案中的 C++11 方法。
      猜你喜欢
      • 1970-01-01
      • 2011-10-25
      • 2017-02-20
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      相关资源
      最近更新 更多