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

cl25kdpy  于 2023-07-01  发布在  其他
关注(0)|答案(1)|浏览(253)

我可以得到当前的epoch时间,以毫秒为单位:

#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
wvt8vs2t

wvt8vs2t1#

不幸的是,chrono库只处理时间,而不是日期。看起来像是things will change in C++20,但现在我们必须使用ctime库中的函数和类型。
也就是说,使用SomeClock::now()获取当前时间后,可以使用to_time_t将其从ctime库转换为std::time_t。在此之后,不再涉及chrono库,只有ctime库。
参见thisthis堆栈溢出问题。
一个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;
}

运行下面的代码并查看注解。

相关问题