使用C ++将时代毫秒转换为UTC日期和时间

问题描述

我可以得到当前纪元时间(以毫秒为单位):

data = np.full((6,*target_shape),values.reshape(-1,1,1))
principal(*data)

这将输出:1597436329290。

我有一段代码,使用以秒为单位的纪元时间,效果很好。如何使用timeSinceEpochMillisec()通过strftime格式化日期和时间?

#include <chrono>
#include <cstdint>
#include <iostream>
#include <ctime>

uint64_t timeSinceEpochMillisec() {
  using namespace std::chrono;
  return duration_cast<milliseconds>(system_clock::Now().time_since_epoch()).count();
}

int main() {
  std::cout << timeSinceEpochMillisec() << std::endl;
  return 0;
}

输出

std::time_t epoch_timestamp = std::time(nullptr);
char buf[80];

std::tm *ts = std::gmtime(&epoch_timestamp);
strftime(buf,sizeof(buf),"%m/%d/%Y %H:%M:%s",ts);
std::cout << buf << std::endl;

解决方法

不幸的是,chrono库仅处理时间,而不处理日期。似乎是things will change in C++20,但是现在我们必须使用ctime库中的函数和类型。

也就是说,一旦您获得SomeClock::now()的当前时间,就可以使用to_time_tstd::time_t库将其转换为ctime。之后,不再需要任何计时库,只有ctime库。

请参阅thisthis堆栈溢出问题。

std::time_t通常只是从UTC时间1970年1月1日00:00起经过的的数量。您的timeSinceEpochMillisec函数与之类似,但是它以毫秒为单位。 如果将其输出除以1000并得到整数结果,则您的数字可能与 std::time(nullptr)相同,但这可能与实现有关(std::time_t可能有不同的定义)

#include <chrono>
#include <cstdint>
#include <ctime>
#include <iostream>

using Clock = std::chrono::system_clock;

uint64_t timeSinceEpochMillisec() {
    using namespace std::chrono;
    return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}

int main() {
    auto now = Clock::now();

    // time_t comes from the C library and "it is generally implemented as an
    // integral value representing the number of seconds elapsed since 00:00
    // hours,Jan 1,1970 UTC"
    // http://www.cplusplus.com/reference/ctime/time_t/
    auto now_in_time_t = Clock::to_time_t(now);

    // let's compare what we get with the current time converted to time_t and
    // your timeSinceEpochMillisec function
    std::cout << "Your functin: " << timeSinceEpochMillisec() << std::endl;
    std::cout << "time_t: " << now_in_time_t << std::endl;

    // Now let's work with dates. First we convert the current time to a date

    // Use std::localtime to convert the time_t to a "date",whose type is "tm*",where "tm" is a struct
    // Note that I'm using std::localtime instead of std::gmtime to get the date
    // in my local timezone. The std::gmtime gets the date in UTC.

    // https://en.cppreference.com/w/cpp/chrono/c/localtime
    auto now_as_tm_date = std::localtime(&now_in_time_t);

    // Now we can wuery the date struct for individual data
    // tm_year gives the number of years since 1900
    std::cout << "Current year is: " << now_as_tm_date->tm_year + 1900 << std::endl;
    // See http://www.cplusplus.com/reference/ctime/tm/
    // for other fields in the tm struct

    // The strftime function can be used to convert the date to a null
    // terminated char array for easy printing
    char buf[80];
    strftime(buf,sizeof(buf),"%m/%d/%Y %H:%M:%S",now_as_tm_date);
    std::cout << "Current date and time: " << buf << std::endl;
    return 0;
}

运行以下代码并查看注释。