获取时间函数

时间:2021-03-24 16:54:27
/*
 @file TimeUtil.h
 @brief 时间函数
*/
#ifndef __PUBLIC_TIMEUTIL_H__
#define __PUBLIC_TIMEUTIL_H__
 
#include <time.h>
#include <sys/timeb.h>
 
#ifdef WIN32
#include <Windows.h>
#else
#include <stdint.h>
#include <sys/time.h>
#endif // WIN32
 
#include "boost/chrono.hpp"
#include "boost/date_time.hpp"


#include "DataType.h"
 
#define NSEC_PER_MSEC   (1000000)//每毫秒包含的纳秒数
#define MSEC_PER_SEC    (1000)  //每秒包含的毫秒数
#define SEC_PER_MIN     (60)    //每分钟包含的秒数
#define MIN_PER_HOUR    (60)    //每小时包含的分钟数
#define HOUR_PER_DAY    (24)    //每天包含的小时数
 
 
namespace kbd_public
{
    /* @brief 计算机启动至今的毫秒数 */
    __inline int64 elapseSinceBoot()
    {
        boost::chrono::time_point<boost::chrono::steady_clock, boost::chrono::milliseconds> stTime =
            boost::chrono::time_point_cast<boost::chrono::milliseconds>(boost::chrono::steady_clock::now());
        return stTime.time_since_epoch().count();
    }
 
    /* @brief 获取1970-1-1至今的秒数 */
    __inline int64 getUTCTimeSec()
    {
        boost::chrono::time_point<boost::chrono::system_clock, boost::chrono::seconds> curTime =
            boost::chrono::time_point_cast<boost::chrono::seconds>(boost::chrono::system_clock::now());
 
        return curTime.time_since_epoch().count();
    }
 
    /* @brief 获取1970-1-1至今的毫秒数 */
    __inline uint64 getUTCTimeMsec()
    {
        boost::chrono::time_point<boost::chrono::system_clock, boost::chrono::milliseconds> curTime =
            boost::chrono::time_point_cast<boost::chrono::milliseconds>(boost::chrono::system_clock::now());
 
        return curTime.time_since_epoch().count();
    }
 
    /*
    @brief 获取当前时间
    @param int64 & lSec    返回的秒数(1970-1-1至今的秒数)
    @param uint16 & wMsec  返回的毫秒数
    */
    __inline void getUTCTime(int64 &lSec, uint16 &wMsec)
    {
        uint64 ulTimeMsec = getUTCTimeMsec();
 
        lSec = ulTimeMsec / MSEC_PER_SEC;
        wMsec = ulTimeMsec % MSEC_PER_SEC;
    }
 
#ifdef WIN32
    typedef struct _SYSTEMTIME  LOCALTIME;
#else
    typedef struct _LOCALTIME
    {
        unsigned short wYear;
        unsigned short wMonth;
        unsigned short wDayOfWeek;  //< 周日为0,周一到周六为1到6
        unsigned short wDay;
        unsigned short wHour;
        unsigned short wMinute;
        unsigned short wSecond;
        unsigned short wMilliseconds;
    } LOCALTIME, *PLOCALTIME;
#endif
 
    __inline void convertPTimeToSystemTime(const boost::posix_time::ptime &stTime, LOCALTIME &stLocalTime)
    {
        stLocalTime.wYear = stTime.date().year();
        stLocalTime.wMonth = stTime.date().month();
        stLocalTime.wDayOfWeek = stTime.date().day_of_week();
        stLocalTime.wDay = stTime.date().day();
        stLocalTime.wHour = static_cast<uint16>(stTime.time_of_day().hours());
        stLocalTime.wMinute = static_cast<uint16>(stTime.time_of_day().minutes());
        stLocalTime.wSecond = static_cast<uint16>(stTime.time_of_day().seconds());
        stLocalTime.wMilliseconds = stTime.time_of_day().total_milliseconds() % MSEC_PER_SEC;
    }
 
    /*
     @brief 获取本地时间
     @param LOCALTIME & stTime 返回的本地时间
    */
    __inline void getLocalSysTime(LOCALTIME &stTime)
    {
        boost::posix_time::ptime stLocalTime = boost::posix_time::microsec_clock::local_time();
 
        convertPTimeToSystemTime(stLocalTime, stTime);
    }
 
    /* @brief 获取本地时间 */
    __inline LOCALTIME getLocalSystemTime()
    {
        LOCALTIME stSysLocalTime;
        boost::posix_time::ptime stLocalTime = boost::posix_time::microsec_clock::local_time();
        
        convertPTimeToSystemTime(stLocalTime, stSysLocalTime);
 
        return stSysLocalTime;
    }


    /* @brief 将UTC时间至今的毫秒数转换为本地时间 */
    __inline LOCALTIME convertUTCMsecToLocalTime(uint64 ulMsec)
    {
        /* @brief 先获取本地时间和UTC时间相差几个小时 */
        static int64 lHours = (boost::posix_time::second_clock::local_time() -
                               boost::posix_time::second_clock::universal_time()).hours();
        static boost::posix_time::ptime stEpoch(boost::gregorian::date(1970, 1, 1), boost::posix_time::hours(lHours));
        
        /* @brief 转换为本地时间 */
        boost::posix_time::ptime stLocalTime = stEpoch + boost::posix_time::millisec(ulMsec);
        LOCALTIME stSysLocalTime;
        convertPTimeToSystemTime(stLocalTime, stSysLocalTime);
        return stSysLocalTime;
    }
 
    /* @brief 将UTC时间至今的秒数转换为本地时间 */
    __inline LOCALTIME convertUTCSecToLocalTime(uint64 ulSec)
    {
        /* @brief 先获取本地时间和UTC时间相差几个小时 */
        static int64 lHours = (boost::posix_time::second_clock::local_time() -
                               boost::posix_time::second_clock::universal_time()).hours();
        static boost::posix_time::ptime stEpoch(boost::gregorian::date(1970, 1, 1), boost::posix_time::hours(lHours));
 
        /* @brief 转换为本地时间 */
        boost::posix_time::ptime stLocalTime = stEpoch + boost::posix_time::seconds(ulSec);
 
        LOCALTIME stSysLocalTime;
        convertPTimeToSystemTime(stLocalTime, stSysLocalTime);
        return stSysLocalTime;
    }
 
    /* @brief 本地时间转换为UTC时间 */
    __inline boost::posix_time::ptime localTimeToUTC(boost::posix_time::ptime &stLocalTime)
    {
        /* @brief 先获取本地时间和UTC时间相差几个小时 */
        static int64 lHours = (boost::posix_time::second_clock::local_time() -
                               boost::posix_time::second_clock::universal_time()).hours();
        static boost::posix_time::ptime stUtcTime = stLocalTime - boost::posix_time::hours(lHours);
 
        return stUtcTime;
    }
 
    /* @brief 转换本地时间为UTC时间,并转换为UTC时间至今的毫秒数 */
    __inline uint64 convertLocalTimeToUTC(boost::posix_time::ptime &stLocalTime)
    {
        /* @brief 先获取本地时间和UTC时间相差几个小时 */
        static int64 lHours = (boost::posix_time::second_clock::local_time() -
                               boost::posix_time::second_clock::universal_time()).hours();
        static boost::posix_time::ptime stDelta(boost::gregorian::date(1970, 1, 1), boost::posix_time::hours(lHours));
 
        return (stLocalTime - stDelta).total_milliseconds();
    }
 
    /* @brief 将本地时间转换为UTC时间至今的毫秒数,如果输入值非法,返回0 */
    __inline uint64 getUTCMsecFromLocalTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nMsec)
    {
        try
        {
            boost::posix_time::ptime localTime(boost::gregorian::date(nYear, nMonth, nDay),
                                               boost::posix_time::time_duration(nHour, nMin, nSec) + boost::posix_time::millisec(nMsec));
            return convertLocalTimeToUTC(localTime);
        }
        catch (const std::exception&)
        {
            return 0;
        }
    }
 
}   //namespace kbd_public
 
#endif // __PUBLIC_TIMEUTIL_H__