C++常用的時間處理函數(檢驗 struct tm* 是否合法,獲取當前標准時間,獲取當前時間戳,標准時間轉毫秒級時間戳,時間戳轉標准時間,...)


這里只做展示作用,可能更新不及時,獲取源碼請移步gitee個人倉庫:time

Time.hpp

#ifndef  SINICH_EVEEN_TIME
#define SINICH_EVEEN_TIME

/*
*Environment:
*Linux(Ubuntu), C++11,gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
*Description:
*時間計算類
*/

#include <chrono>
#include <ctime>
#include <string>
#include <cstring>

//參數流向
#define IN
#define OUT
#define INOUT

using std::time_t;
using std::string;
using std::mktime;

using namespace std::chrono;

namespace sinicheveen
{
    class Time
    {
    public:
        //檢驗 struct tm* 是否合法
        inline static bool checkTM(IN struct tm* pTime_tm);

        //獲取當前標准時間
        inline static string getCurrStdTime(IN const char* szTimeFormat = "%Y-%m-%d %H:%M:%S");

        //獲取當前時間戳(默認毫秒級別)
        template<class T = milliseconds>
        inline static int64_t getCurrTimeStampByChrono();

        //獲取當前時間戳(毫秒級別)
        inline static int64_t getCurrTimeStampByCTime();

        //根據年齡獲取出生日期時間戳(毫秒級別)
        inline static int64_t getTimeStampByAge(IN const int32_t age);

        //根據出生年月日獲取出生日期時間戳(毫秒級別)
        inline static bool getTimeStampByYMD(IN const string& strBornYMD, OUT int64_t& dlTimeStamp);

        //標准時間轉毫秒級時間戳,標准時間格式:「YYYY-MM-DD HH:MM:SS」
        inline static int64_t stdTimeToTimeStamp(IN const string& strStdTime);

        //毫秒級時間戳轉標准時間,標准時間格式:「YYYY-MM-DD HH:MM:SS」
        inline static string timeStampToStdTime(IN const int64_t timeStamp);
    };    //Time

    /*
    *Name:checkTM
    *Description:檢驗 struct tm* 是否合法
    *Input:IN struct tm* pTime_tm,時間
    *Return:bool,合法返回true,否則返回false
    *Caution:
    */
    inline bool Time::checkTM(IN struct tm* pTime_tm)
    {
        if (nullptr == pTime_tm)
        {
            return false;
        }

        if (pTime_tm->tm_mon < 0 || pTime_tm->tm_mon > 11
            || pTime_tm->tm_mday < 1 || pTime_tm->tm_mday > 31
            || pTime_tm->tm_hour < 0 || pTime_tm->tm_hour > 23
            || pTime_tm->tm_min < 0 || pTime_tm->tm_min > 59
            || pTime_tm->tm_sec < 0 || pTime_tm->tm_sec > 59)
        {
            return false;
        }

        if (4 == (pTime_tm->tm_mon + 1) || 6 == (pTime_tm->tm_mon + 1) || 9 == (pTime_tm->tm_mon + 1) || 11 == (pTime_tm->tm_mon + 1))    //小月有30天
        {
            if (pTime_tm->tm_mday > 30)
            {
                return false;
            }
        }

        if (2 == (pTime_tm->tm_mon + 1))    //2月
        {
            if ((0 == ((pTime_tm->tm_year + 1900) % 400)) || ((0 == ((pTime_tm->tm_year + 1900) % 4)) && (0 != ((pTime_tm->tm_year + 1900) % 100))))
            {

                if (pTime_tm->tm_mday > 29)    //閏年2月份有29天
                {
                    return false;
                }
            }
            else    //平年2月份有28天
            {
                if (pTime_tm->tm_mday > 28)
                {
                    return false;
                }
            }
        }

        return true;
    }    //checkTM()

   /*
    *Name:getCurrStdTime
    *Description:獲取當前標准時間
    *Input:IN const char* szTimeFormat,時間格式
    *Return:string,系統當前時間
    *Caution:
    */
    inline string Time::getCurrStdTime(IN const char* szTimeFormat)
    {
        time_t currTime_t;
        time(&currTime_t);
        struct tm* locTime_tm = localtime(&currTime_t);    //轉換成本地日歷時間
        char szStdTime[50] = { "" };    //自定義格式存儲位置
        strftime(szStdTime, 50, szTimeFormat, locTime_tm);
        return string(szStdTime);
    }    //getCurrStdTime()

    /*
    *Name:getCurrTimeStampByChrono
    *Description:獲取當前時間戳(默認毫秒級別)
    *Input:
    *Return:int64_t,時間戳,默認毫秒級別
    *Caution:
    */
    template<class T>
    inline int64_t Time::getCurrTimeStampByChrono()
    {
        system_clock::time_point currentTime_tp = system_clock::now();    //當前時間
        system_clock::duration timeSinceEpoch_du = currentTime_tp.time_since_epoch();    //從 Epoch 到現的納秒數
        int64_t dlTimeStamp = duration_cast<T>(timeSinceEpoch_du).count();    //時間轉換,默認毫秒級別
        return dlTimeStamp;
    }    //getCurrTimeStampByChrono()

    /*
    *Name:getCurrTimeStampByCTime
    *Description:獲取當前時間戳(毫秒級別)
    *Input:
    *Return:int64_t,時間戳
    *Caution:
    */
    inline int64_t Time::getCurrTimeStampByCTime()
    {
        return time(NULL) * 1000;
    }    //getCurrTimeStampByCTime()

    /*
    *Name:getTimeStampByAge
    *Description:根據年齡獲取出生日期時間戳(毫秒級別)
    *Input:IN const int32_t age,年齡
    *Return:int64_t,出生日期時間戳(毫秒級別)
    *Caution:
    */
    inline int64_t Time::getTimeStampByAge(IN const int32_t age)
    {
        time_t currTime_t;
        time(&currTime_t);
        struct tm* locTime_tm = localtime(&currTime_t);    //轉換成本地日歷時間
        locTime_tm->tm_year = locTime_tm->tm_year - age;
        locTime_tm->tm_hour = 0;
        locTime_tm->tm_min = 0;
        locTime_tm->tm_sec = 0;
        return mktime(locTime_tm) * 1000;
    }    //getTimeStampByAge()

    /*
    *Name:getTimeStampByYMD
    *Description:根據出生年月日獲取出生日期時間戳(毫秒級別)
    *Input:IN const string& strBornYMD,出生年月日
            OUT int64_t& dlTimeStamp,出生日期時間戳(毫秒級別)
    *Return:bool,如果出生年月日不合法返回false,否則時間戳獲取成功返回true
    *Caution:
    */
    inline bool Time::getTimeStampByYMD(IN const string& strBornYMD, OUT int64_t& dlTimeStamp)
    {
        struct tm* pLocTime_tm = (struct tm*)malloc(sizeof(struct tm));
        memset(pLocTime_tm, 0, sizeof(struct tm));
        strptime(strBornYMD.c_str(), "%Y-%m-%d", pLocTime_tm);
        if (true != checkTM(pLocTime_tm))
        {
            free(pLocTime_tm);
            return false;
        }
        dlTimeStamp = mktime(pLocTime_tm) * 1000;
        free(pLocTime_tm);

        return true;
    }

    /*
    *Name:stdTimeToTimeStamp
    *Description:標准時間轉毫秒級時間戳,標准時間格式:「YYYY-MM-DD HH:MM:SS」
    *Input:IN const string& strStdTime,標准時間
    *Return:int64_t,如果標准時間正確,返回對應的時間戳,否則返回-1
    *Caution:
    */
    inline int64_t Time::stdTimeToTimeStamp(IN const string& strStdTime)
    {
        struct tm* pLocTime_tm = (struct tm*)malloc(sizeof(struct tm));
        strptime(strStdTime.c_str(), "%Y-%m-%d %H:%M:%S", pLocTime_tm);
        if (true != checkTM(pLocTime_tm))
        {
            free(pLocTime_tm);
            return -1;
        }
        int64_t dlTimeStamp = mktime(pLocTime_tm) * 1000;
        free(pLocTime_tm);

        return dlTimeStamp;
    }    //stdTimeToTimeStamp()

    /*
    *Name:timeStampToStdTime
    *Description:毫秒級時間戳轉標准時間,標准時間格式:「YYYY-MM-DD HH:MM:SS」
    *Input:IN const int64_t timeStamp,毫秒級時間戳
    *Return:string,標准時間,標准時間格式:「YYYY-MM-DD HH:MM:SS」
    *Caution:
    */
    inline string Time::timeStampToStdTime(IN const int64_t timeStamp)
    {
        time_t timeStamp_t = timeStamp / 1000;
        struct tm* pLocTime_tm;
        pLocTime_tm = localtime(&timeStamp_t);    //轉換成本地日歷時
        char szTimeStr[20] = { 0 };    //儲存標准時間
        strftime(szTimeStr, 50, "%Y-%m-%d %H:%M:%S", pLocTime_tm);    //轉換時間格式

        return szTimeStr;
    }    //timeStampToStdTime()

}    //sinicheveen

#endif    //SINICH_EVEEN_TIME


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM