C++当前时间转毫秒时间戳

转载出处https://blog.csdn.net/qq_36351159/article/details/110622744

windows api方式

#include <iostream>
#include <time.h>
#include<windows.h>
#include <stdint.h>

uint64_t GetCurrentTimerMS(char* szTimer=NULL)
{
    uint64_t nTimer = 0;
    SYSTEMTIME currentTime;
    GetLocalTime(&currentTime);

    tm temptm = { currentTime.wSecond,
        currentTime.wMinute,
        currentTime.wHour,
        currentTime.wDay,
        currentTime.wMonth - 1,
        currentTime.wYear - 1900
    };
    nTimer = mktime(&temptm) * 1000 + currentTime.wMilliseconds;
    if(szTimer != NULL)
        sprintf(szTimer, "%llu", nTimer);
    return nTimer;
}
int main(){
    while(1)
    {
        char szTimer[64];
        GetCurrentTimerMS(szTimer);
        printf("millisecond:%s,	%llu

",szTimer,GetCurrentTimerMS());  //毫秒
        Sleep(1);
    }
    return 0;
}

MFC CTime方式

#include <fstream>
//
生成三位随机数 int GetRandomNumber() { int RandomNumber; srand((unsigned)time(NULL));//time()用系统时间初始化种。为rand()生成不同的随机种子。 RandomNumber = rand() % (1000 - 100) + 100; return RandomNumber; }
        //获得当前计算机的毫秒时间(秒+固定三位随机数)
        CTime time = CTime::GetCurrentTime();
        CString time_cs = time.Format(_T("%Y-%m-%d %H:%M:s%S"));
        time_t time1 = ::time(NULL);
        //time1 = time1 * 1000; //CTime只能返回秒,想要毫秒不一样就加三位随机数
        int RandomNumber = GetRandomNumber();
        char haomiao[1000];
        sprintf_s(haomiao, "%lld%d", time1, RandomNumber);

由年月日时分秒毫秒生成随机数

string getCurrentTimeStr()
{
    time_t t = time(NULL);
    char ch[64] = { 0 };
    strftime(ch, sizeof(ch) - 1, "%Y%m%d%H%M%S", localtime(&t));
    return ch;
}
        //生成随机数
        string time_str = getCurrentTimeStr();
        struct timeb tb;
        ftime(&tb);
        char tb_str[10];
        sprintf(tb_str, "%03d", tb.millitm);//获得毫秒
        string fn_str = time_str + tb_str;

相关参考资料

http://www.voidcn.com/article/p-hcmcjcum-bac.html

https://www.cnblogs.com/guolongzheng/p/9399414.html

https://blog.csdn.net/feeltouch/article/details/39701151

C++11获取时间戳和时间戳转日期(毫秒精度)备注(低版本VS不能用)

获取时间戳

std::time_t getTimeStamp()
{
    std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
    auto tmp=std::chrono::duration_cast<std::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;
}

时间戳转日期
其中int64 为自定义跨平台的数据类型

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

程序员阿飞

2021年5月9日

作者: 阿飞

出处: https://www.cnblogs.com/nxopen2018/>

关于作者:专注NX开发、VC++开发、数据库、三维建模领域,请多多赐教!

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可留言(博客文章底部留言)咨询.

原文地址:https://www.cnblogs.com/nxopen2018/p/14748901.html