时间戳获取

using namespace std::chrono;
    auto millisec_since_epoch = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
    auto msec = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();

    auto sec_since_epoch = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();

    std::cout << "seconds since epoch: " << sec_since_epoch << std::endl;
    std::cout << "milliseconds since epoch: " << millisec_since_epoch << std::endl;
  • 秒seconds since epoch: 1622534342
  • 微妙 milliseconds since epoch: 1622534342050

标准年月日+时间获取

std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char re_time[256] = {0};
std::strftime(re_time, sizeof(re_time), "%Y-%m-%d %X", std::localtime(&now));
std::cout << “注册时间 ” << re_time << std::endl;

注册时间: 2021-06-01 15:56:42


Re:

https://www.delftstack.com/howto/cpp/how-to-get-time-in-milliseconds-cpp/

http://cn.voidcc.com/question/p-ynucfjxu-sk.html