天天看点

自写的时间相关类

为了测试程序的性能,需要获得程序的执行时间,在做的过程中发现CTime,CTimeSpan精度不够高,SYSTEMTIME精度尚可,所以参考网上一些资料,对SYSTEMTIME做了一层浅浅的封装。

程序主要如下:

// Begin

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

#ifndef INCLUDED_MYTIME_H

#define INCLUDED_MYTIME_H

#include <WTypes.h>

#include <atlstr.h>

#include <atltime.h>

class MyTimeSpan;

class MyTime

{

public:

 MyTime();

 MyTime(const SYSTEMTIME& st);

 void SetTime(const SYSTEMTIME& st);

 SYSTEMTIME GetTime() const;

 MyTimeSpan operator-(const MyTime& time ) const;

private:

 SYSTEMTIME m_time;

};

class MyTimeSpan

 friend class MyTime;

 MyTimeSpan();

 INT64   GetTotalMinutes() const;

 INT64   GetTotalSeconds() const;

 INT64   GetTotalMilliSeconds() const;

 INT64   GetMinute() const;

 INT64   GetSecond() const;

 INT64   GetMilliSecond() const;

 CString ToString() const;

 INT64   m_milliSeconds;

inline MyTime::MyTime()

 GetLocalTime(&m_time);

}

inline MyTime::MyTime(const SYSTEMTIME& st)

 SetTime(st);

inline void MyTime::SetTime(const SYSTEMTIME& st)

 m_time = st;

inline SYSTEMTIME MyTime::GetTime() const

 return m_time;

inline MyTimeSpan MyTime::operator-(const MyTime& st) const

 MyTimeSpan sts; 

 SYSTEMTIME left = this->m_time;

 SYSTEMTIME right = st.m_time;

 CTime  tmLeft(left.wYear, left.wMonth, left.wDay,0,0,0); 

 CTime  tmRight(right.wYear, right.wMonth, right.wDay,0,0,0); 

 CTimeSpan  sp = tmLeft - tmRight;

 long lLMinllis = ((((left.wHour*60 + left.wMinute)*60 +left.wSecond)*1000) + left.wMilliseconds);

 long lRMinllis = ((((right.wHour*60 + right.wMinute)*60 +right.wSecond)*1000) + right.wMilliseconds);

 sts.m_milliSeconds =  (INT64)(sp.GetDays()*24*60*60*1000) + lLMinllis - lRMinllis;

 return sts;

inline MyTimeSpan::MyTimeSpan()

 m_milliSeconds = 0;

inline INT64 MyTimeSpan::GetTotalMilliSeconds() const

 return m_milliSeconds; 

inline INT64 MyTimeSpan::GetTotalSeconds() const

 return m_milliSeconds/1000;

inline INT64 MyTimeSpan::GetTotalMinutes() const

 return (m_milliSeconds/1000)/60;

inline INT64 MyTimeSpan::GetMinute() const

 INT64 tmp = GetTotalSeconds();

 return tmp/60;

inline INT64 MyTimeSpan::GetSecond() const

 INT64 tmp = GetTotalSeconds(); 

 return tmp % 60;

继续阅读