C++ C++;以毫秒为单位获取UTC时间

C++ C++;以毫秒为单位获取UTC时间,c++,datetime,boost,C++,Datetime,Boost,我需要以毫秒为单位获取UTC时间,我尝试使用它: std::string getCurrentUTC() { namespace bg = boost::gregorian; static char const* const fmt = "%Y-%b-%d %H:%M:%S"; std::ostringstream ss; ss.imbue(std::locale(std::cout.getloc(), new bg::date_facet(f

我需要以毫秒为单位获取UTC时间,我尝试使用它:

std::string getCurrentUTC()
{
    namespace bg = boost::gregorian;
    static char const* const fmt = "%Y-%b-%d %H:%M:%S";
    std::ostringstream ss;
    ss.imbue(std::locale(std::cout.getloc(), new bg::date_facet(fmt)));
    ss << bg::day_clock::universal_day();
    return ss.str();
}
我需要以下格式:

2020-Jun-29 00:00:00.000

如何解决此问题?

您可以获得UTC时间(毫秒),如下所示:

struct timeb timebStruct;
ftime(&timebStruct);
struct tm               *tmStruct = new tm();
gmtime_r(&timebStruct.time, tmStruct);
std::cout <<  tmStruct->tm_hour
                            << tmStruct->tm_min <<  tmStruct->tm_sec <<  
timebStruct.millitm << std::endl
struct timeb timebStruct;
ftime(时间结构和时间结构);
struct tm*tmStruct=new tm();
gmtime_r(&timebStruct.time,tmStruct);
标准时间:每小时

tm_min tm_sec您可以通过毫秒获得UTC时间,如下所示:

struct timeb timebStruct;
ftime(&timebStruct);
struct tm               *tmStruct = new tm();
gmtime_r(&timebStruct.time, tmStruct);
std::cout <<  tmStruct->tm_hour
                            << tmStruct->tm_min <<  tmStruct->tm_sec <<  
timebStruct.millitm << std::endl
struct timeb timebStruct;
ftime(时间结构和时间结构);
struct tm*tmStruct=new tm();
gmtime_r(&timebStruct.time,tmStruct);
标准时间:每小时
tm_min tm_sec使用此选项,您可以编写:

#include "date/date.h"

#include <chrono>
#include <string>
#include <sstream>

std::string
getCurrentUTC()
{
    namespace ch = std::chrono;
    using date::operator<<;
    static char const* const fmt = "%Y-%b-%d %T";
    std::ostringstream ss;
    auto tp = ch::time_point_cast<ch::milliseconds>(ch::system_clock::now());
    ss << date::format(fmt, tp);
    return ss.str();
}
这是UTC到毫秒精度的当前时间

当C++20
装运时,它将通过以下方式进行移植:

  • 删除
    #包括“date/date.h”
  • 删除
    使用date::operator使用此选项,您可以编写:

    #include "date/date.h"
    
    #include <chrono>
    #include <string>
    #include <sstream>
    
    std::string
    getCurrentUTC()
    {
        namespace ch = std::chrono;
        using date::operator<<;
        static char const* const fmt = "%Y-%b-%d %T";
        std::ostringstream ss;
        auto tp = ch::time_point_cast<ch::milliseconds>(ch::system_clock::now());
        ss << date::format(fmt, tp);
        return ss.str();
    }
    
    这是UTC到毫秒精度的当前时间

    当C++20
    装运时,它将通过以下方式进行移植:

    • 删除
      #包括“date/date.h”

    • 删除
      使用日期::运算符
      静态字符常量*常量fmt=“%Y-%b-%d%H:%M:%S.000”
      @ThomasSablik或
      %Y-%b-%d 00:00:00.000“
      ,结果相同。
      静态字符常量*常量fmt=“%Y-%b-%d%H:%M:%S.000”@ThomasSablik或
      %Y-%b-%d 00:00:00.000“
      ,结果相同。