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

Convert Epoch Milliseconds to UTC Date and Time using c++

我可以获得以毫秒为单位的当前纪元时间:

#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;
}

这将输出:1597436329290。

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

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;

输出:

1597437386198
08/14/2020/ 20:36:26

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

也就是说,一旦您使用 SomeClock::now() 获得当前时间,您就可以使用 to_time_t 将其从 ctime 库中转换为 std::time_t。之后就没有涉及到更多的chrono库,只有ctime库。

参见 this and this Whosebug 问题。

A std::time_t 通常只是自世界标准时间 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;
}

运行下面的代码,看评论。