VCpp时间操作——CTime与CTimeSpan类的使用

⌚Time: 2022-10-02 19:41:10

👨‍💻Author: Jack Ge

CTime


class CTime

{

public:

    static CTime WINAPI GetCurrentTime() throw();

    static BOOL WINAPI IsValidFILETIME(const FILETIME& ft) throw();



    CTime() throw();

    CTime( __time64_t time ) throw();

    CTime( int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,

        int nDST = -1 );

    CTime( WORD wDosDate, WORD wDosTime, int nDST = -1 );

    CTime( const SYSTEMTIME& st, int nDST = -1 );

    CTime( const FILETIME& ft, int nDST = -1 );

#ifdef __oledb_h__

    CTime( const DBTIMESTAMP& dbts, int nDST = -1 ) throw();

#endif



    CTime& operator=( __time64_t time ) throw();



    CTime& operator+=( CTimeSpan span ) throw();

    CTime& operator-=( CTimeSpan span ) throw();



    CTimeSpan operator-( CTime time ) const throw();

    CTime operator-( CTimeSpan span ) const throw();

    CTime operator+( CTimeSpan span ) const throw();



    bool operator==( CTime time ) const throw();

    bool operator!=( CTime time ) const throw();

    bool operator<( CTime time ) const throw();

    bool operator>( CTime time ) const throw();

    bool operator<=( CTime time ) const throw();

    bool operator>=( CTime time ) const throw();



    struct tm* GetGmtTm( struct tm* ptm ) const;

    struct tm* GetLocalTm( struct tm* ptm ) const;

#if !_SECURE_ATL

    _ATL_INSECURE_DEPRECATE("Pass an output time structure to CTime::GetGmtTm")

    struct tm* GetGmtTm() const throw();

    _ATL_INSECURE_DEPRECATE("Pass an output time structure to CTime::GetLocalTm")

    struct tm* GetLocalTm() const throw();

#endif



    bool GetAsSystemTime( SYSTEMTIME& st ) const throw();

    bool GetAsDBTIMESTAMP( DBTIMESTAMP& dbts ) const throw();



    __time64_t GetTime() const throw();



    int GetYear() const throw();

    int GetMonth() const throw();

    int GetDay() const throw();

    int GetHour() const throw();

    int GetMinute() const throw();

    int GetSecond() const throw();

    int GetDayOfWeek() const throw();



#ifndef _ATL_MIN_CRT

    // formatting using "C" strftime

    CString Format( LPCTSTR pszFormat ) const;

    CString FormatGmt( LPCTSTR pszFormat ) const;

    CString Format( UINT nFormatID ) const;

    CString FormatGmt( UINT nFormatID ) const;

#endif



#if defined(_AFX) && defined(_UNICODE)

    // for compatibility with MFC 3.x

    CString Format(LPCSTR pFormat) const;

    CString FormatGmt(LPCSTR pFormat) const;

#endif



#ifdef _AFX

    CArchive& Serialize64(CArchive& ar);

#endif



private:

    __time64_t m_time;

};

CTime获取当前时间


CTime timeNow = CTime::GetCurrentTime();

CTime指定时间初始化 2022-08-15 10:22:03


CTime timeNow(2022,8,15,10,22,3);

CTime比较大小


CTime time1(2022,8,15,10,22,3);

CTime time2(2022,8,15,10,22,4);

if(time1 < time2){//true

    ...

}

CTime转化CString


CTime timeNow = CTime::GetCurrentTime();

CString strTime = timeNow.Format(L"%Y-%m-%d %H:%M:%S 星期%w");

CString转CTime


CString strTime("2222-11-29 11:06:23");

int nYear, nMonth, nDate, nHour, nMin, nSec;

sscanf(strTime.GetBuffer(strTime.GetLength()), "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);

CTime t(nYear, nMonth, nDate, nHour, nMin, nSec);

另外,CTime的


int GetDayOfWeek( ) const throw( );

返回基于本地时间周,周日是1,周一是2,周六是7。这一点很容易出错


CTimeSpan


class CTimeSpan

{

public:

    CTimeSpan() throw();

    CTimeSpan( __time64_t time ) throw();

    CTimeSpan( LONG lDays, int nHours, int nMins, int nSecs ) throw();



    LONGLONG GetDays() const throw();

    LONGLONG GetTotalHours() const throw();

    LONG GetHours() const throw();

    LONGLONG GetTotalMinutes() const throw();

    LONG GetMinutes() const throw();

    LONGLONG GetTotalSeconds() const throw();

    LONG GetSeconds() const throw();



    __time64_t GetTimeSpan() const throw();



    CTimeSpan operator+( CTimeSpan span ) const throw();

    CTimeSpan operator-( CTimeSpan span ) const throw();

    CTimeSpan& operator+=( CTimeSpan span ) throw();

    CTimeSpan& operator-=( CTimeSpan span ) throw();

    bool operator==( CTimeSpan span ) const throw();

    bool operator!=( CTimeSpan span ) const throw();

    bool operator<( CTimeSpan span ) const throw();

    bool operator>( CTimeSpan span ) const throw();

    bool operator<=( CTimeSpan span ) const throw();

    bool operator>=( CTimeSpan span ) const throw();



#ifndef _ATL_MIN_CRT

public:

    CString Format( LPCTSTR pszFormat ) const;

    CString Format( UINT nID ) const;

#endif

#if defined(_AFX) && defined(_UNICODE)

    // for compatibility with MFC 3.x

    CString Format(LPCSTR pFormat) const;

#endif



#ifdef _AFX

    CArchive& Serialize64(CArchive& ar);

#endif



private:

    __time64_t m_timeSpan;

};

CTimeSpan 计算CTime时间差


CTime time1(2022,8,15,10,22,3);

CTime time2 = CTime::GetCurrentTime(); 

CTimeSpan timeSpan = time2-time1; //计算当前系统时间与时间time1的间隔

timeSpan.GetDays(); //获取这段时间间隔共有多少天

timeSpan.GetTotalHours(); //获取这段时间间隔共有多少小时

timeSpan.GetTotalMinutes();//获取这段时间间隔共有多少分钟

timeSpan.GetTotalSeconds();//获取这段时间间隔共有多少秒

CTime加上特定时间


CTime time1(2022,8,15,10,22,3);//2022-08-15 10:22:03

CTimeSpan timeSpan(0,0,1,0);//时间差1分钟

time1 += timeSpan;//2022-08-15 10:23:03