时间转换 unix time stamp(时间戳), windows file time

2020-12-28 03:29

阅读:471

标签:lock   turn   second   poc   str   error   out   getc   cti   

 

 

#include 
#include 

#include 

using namespace std;

#pragma warning(disable:4996)

void unixTime2Str(__int64 n, char strTime[], int bufLen)
{
    struct tm loc_tm;
    errno_t  err = localtime_s(&loc_tm, &n);
    //errno_t  err = _localtime64_s(&tm, &n);
    if (err!= 0)
    {
        perror(strerror(err));
        return;
    }
    //std::cerr
    strftime(strTime, bufLen - 1, "%Y-%m-%d %H:%M:%S", &loc_tm);
    strTime[bufLen - 1] = ‘\0‘;
}


std::time_t getTimeStamp()
{
    std::chrono::time_point<:chrono::system_clock std::chrono::milliseconds> tp 
                = std::chrono::time_point_cast<:chrono::milliseconds>(std::chrono::system_clock::now());

    auto tmp = std::chrono::duration_cast<:chrono::milliseconds>(tp.time_since_epoch());
    std::time_t timestamp = tmp.count();
    //std::time_t timestamp = std::chrono::system_clock::to_time_t(tp);
    return timestamp;
}


std::tm* gettm(std::time_t timestamp)
{
    std::time_t milli = timestamp + (std::time_t)8 * 60 * 60 * 1000;//此处转化为东八区北京时间,如果是其它时区需要按需求修改
    auto mTime = std::chrono::milliseconds(milli);
    auto tp = std::chrono::time_point<:chrono::system_clock std::chrono::milliseconds>(mTime);
    auto tt = std::chrono::system_clock::to_time_t(tp);
    std::tm* now = std::gmtime(&tt);
    
    char strTime[100] = { 0 };
    strftime(strTime, sizeof(strTime) - 1, "%Y-%m-%d %H:%M:%S", now);
    printf(strTime);
    
    printf(" %4d年%02d月%02d日 %02d:%02d:%02d\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
    return now;
}


int main(int argc, char* argv[])
{
    char strTime[100] = { 0 };
    __int64 now = _time64(0);
    //__int64 now = 1444401700;
    //__int64 now = 1594813915910;
    //__int64 now = 1594997085523;
    unixTime2Str(now, strTime, sizeof(strTime));
    cout 

  

时间转换 unix time stamp(时间戳), windows file time

标签:lock   turn   second   poc   str   error   out   getc   cti   

原文地址:https://www.cnblogs.com/hjbf/p/13334027.html


评论


亲,登录后才可以留言!