C++ 获取系统当前时间(日历时)

时间:2021-10-18 22:30:51

获取系统当前时间(日历时)

//Linux & C++11

#include <chrono>
#include <ctime>

using namespace std;

string getCurrentSystemTime()
{
    std::time_t secSinceEpoch = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());    //Seconds since the Epoch
    struct tm* calendarTime = localtime(&secSinceEpoch);    //转换成本地日历时间
    char usrdefFormat[50] = { 0 };    //自定义格式存储位置
    strftime(usrdefFormat, 50, "%Y%m%d%H%M%S", calendarTime);    //格式:20191101135525
    return string(usrdefFormat);
}

获取从纪元到现在经历的毫秒数

//Linux & C++11

#include <chrono>

using namespace std::chrono;

uint64_t getMillSecSinchEpoch()
{
    system_clock::time_point currentTime = system_clock::now();    //当前时间
    system_clock::duration timeSinceEpoch = currentTime.time_since_epoch();    //从 Epoch 到现的纳秒数
    uint64_t millSecSinchEpoch = duration_cast<milliseconds>(timeSinceEpoch).count();    //转换为毫秒
    return millSecSinchEpoch;
}

参考资料:


未完 ......

点击访问原文(进入后根据右侧标签,快速定位到本文)