Chinaunix首页 | 论坛 | 博客
  • 博客访问: 848169
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:11:50

#pragma once
#define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include

//这里个简单的从MFC抄过来的代码,不过一般情况下也能用了。
//唉,
namespace Cat
{

#define MAKEDWORDLONG(a,b) ((DWORDLONG)(((DWORD)(a))|(((DWORDLONG)((DWORD)(b)))<<32)))

 class TimeSpan
 {
 public:
  TimeSpan() {_timeSpan=0;}

  TimeSpan( __time64_t timevalue ){_timeSpan=timevalue;}
  
  TimeSpan( long lDays, long nHours, long nMins, long nSecs )
  {
   _timeSpan = nSecs + 60* (nMins + 60* (nHours + __int64(24) * lDays));
  }

  LONGLONG GetDays() const{return( _timeSpan/(24*3600) );}
  
  LONGLONG GetTotalHours() const{return( _timeSpan/3600 );}
  
  LONG GetHours() const{return( LONG( GetTotalHours()-(GetDays()*24) ) );}
  
  LONGLONG GetTotalMinutes() const{return( _timeSpan/60 );}
  
  LONG GetMinutes() const {return( LONG( GetTotalMinutes()-(GetTotalHours()*60) ) );}
  
  LONGLONG GetTotalSeconds() const {return( _timeSpan );}
  
  LONG GetSeconds() const throw(){return( LONG( GetTotalSeconds()-(GetTotalMinutes()*60) ) );}

  __time64_t GetTimeSpan() const {return _timeSpan;}

  TimeSpan operator+( TimeSpan span ) const
  {
   return( TimeSpan( _timeSpan+span._timeSpan ) );
  }

  TimeSpan operator-( TimeSpan span ) const
  {
   return( TimeSpan( _timeSpan-span._timeSpan ) );
  }

  TimeSpan& operator+=( TimeSpan span )
  {
   _timeSpan += span._timeSpan;
   return( *this );
  }

  TimeSpan& operator-=( TimeSpan span )
  {
   _timeSpan -= span._timeSpan;
   return( *this );
  }

  bool operator==( TimeSpan span ) const
  {
   return( _timeSpan == span._timeSpan );
  }

  bool operator!=( TimeSpan span ) const
  {
   return( _timeSpan != span._timeSpan );
  }

  bool operator<( TimeSpan span ) const
  {
   return( _timeSpan < span._timeSpan );
  }

  bool operator>( TimeSpan span ) const
  {
   return( _timeSpan > span._timeSpan );
  }

  bool operator<=( TimeSpan span ) const
  {
   return( _timeSpan <= span._timeSpan );
  }

  bool operator>=( TimeSpan span ) const
  {
   return( _timeSpan >= span._timeSpan );
  }

 private:
  __time64_t _timeSpan;
 };

 class Time
 {
 public:
  static Time GetCurrentTime()
  {
   return( Time( ::_time64( NULL ) ) );
  }

  Time() {_time=0;}

  Time( __time64_t timevalue ){_time=timevalue;}

  Time( int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,
   int nDST = -1 )
  {
   struct tm atm;
   atm.tm_sec = nSec;
   atm.tm_min = nMin;
   atm.tm_hour = nHour;
   assert(nDay >= 1 && nDay <= 31);
   atm.tm_mday = nDay;
   assert(nMonth >= 1 && nMonth <= 12);
   atm.tm_mon = nMonth - 1;        // tm_mon is 0 based
   assert(nYear >= 1900);
   atm.tm_year = nYear - 1900;     // tm_year is 1900 based
   atm.tm_isdst = nDST;
   _time = _mktime64(&atm);
   assert(_time != -1);       // indicates an illegal input time

   if(_time == -1)
    _time=0;
  }

  Time( WORD wDosDate, WORD wDosTime, int nDST = -1 )
  {
   struct tm atm;
   atm.tm_sec = (wDosTime & ~0xFFE0) << 1;
   atm.tm_min = (wDosTime & ~0xF800) >> 5;
   atm.tm_hour = wDosTime >> 11;

   atm.tm_mday = wDosDate & ~0xFFE0;
   atm.tm_mon = ((wDosDate & ~0xFE00) >> 5) - 1;
   atm.tm_year = (wDosDate >> 9) + 80;
   atm.tm_isdst = nDST;
   _time = _mktime64(&atm);
   assert(_time != -1);       // indicates an illegal input time

   if(_time == -1)
    _time=0;
  }

  Time( const SYSTEMTIME& sysTime, int nDST = -1 )
  {
   if (sysTime.wYear < 1900)
   {
    __time64_t time0 = 0L;
    Time timeT(time0);
    *this = timeT;
   }
   else
   {
    Time timeT(
     (int)sysTime.wYear, (int)sysTime.wMonth, (int)sysTime.wDay,
     (int)sysTime.wHour, (int)sysTime.wMinute, (int)sysTime.wSecond,
     nDST);
    *this = timeT;
   }
  }

  Time( const FILETIME& fileTime, int nDST = -1 )
  {
   // first convert file time (UTC time) to local time
   FILETIME localTime;
   if (!FileTimeToLocalFileTime(&fileTime, &localTime))
   {
    _time = 0;
    return;
   }

   // then convert that time to system time
   SYSTEMTIME sysTime;
   if (!FileTimeToSystemTime(&localTime, &sysTime))
   {
    _time = 0;
 
    return;
   }

   // then convert the system time to a time_t (C-runtime local time)
   Time timeT(sysTime, nDST);
   *this = timeT;
  }

  Time& operator=( __time64_t time )
  {
   _time=time;
  }

  Time& operator+=( TimeSpan span )
  {
   _time+=span.GetTimeSpan();
   return *this;
  }

  Time& operator-=( TimeSpan span )
  {
   _time-=span.GetTimeSpan();
   return *this;
  }

  TimeSpan operator-( Time time ) const
  {
   return TimeSpan(_time-time._time);
  }

  Time operator-( TimeSpan span ) const
  {
   return Time(_time-span.GetTimeSpan());
  }

  Time operator+( TimeSpan span ) const
  {
   return Time(_time+span.GetTimeSpan());
  }

  bool operator==( Time time ) const
  {
   return _time==time._time;
  }

  bool operator!=( Time time ) const
  {
   return _time!=time._time;
  }

  bool operator<( Time time ) const
  {
   return _time<=time._time;
  }

  bool operator>( Time time ) const
  {
   return _time>time._time;
  }

  bool operator<=( Time time ) const
  {
   return _time<=time._time;
  }

  bool operator>=( Time time ) const
  {
   return _time>=time._time;
  }

  struct tm* GetGmtTm( struct tm* ptm = NULL ) const
  {
   if (ptm != NULL)
   {
    struct tm * ptmTemp;
    ptmTemp = _gmtime64(&_time);

    if(ptmTemp == NULL)
     return NULL;

    *ptm = *ptmTemp;

    return ptm;
   }
   else
    return _gmtime64(&_time);
  }
  
  struct tm* GetLocalTm( struct tm* ptm = NULL ) const
  {
   if (ptm != NULL)
   {
    struct tm* ptmTemp = _localtime64(&_time);
    if (ptmTemp == NULL)
     return NULL;    // indicates the m_time was not initialized!

    *ptm = *ptmTemp;
    return ptm;
   }
   else
    return _localtime64(&_time);
  }
  
  bool GetAsSystemTime( SYSTEMTIME& timeDest ) const
  {
   struct tm* ptm = GetLocalTm(NULL);
   if (!ptm)
    return false;

   timeDest.wYear = (WORD) (1900 + ptm->tm_year);
   timeDest.wMonth = (WORD) (1 + ptm->tm_mon);
   timeDest.wDayOfWeek = (WORD) ptm->tm_wday;
   timeDest.wDay = (WORD) ptm->tm_mday;
   timeDest.wHour = (WORD) ptm->tm_hour;
   timeDest.wMinute = (WORD) ptm->tm_min;
   timeDest.wSecond = (WORD) ptm->tm_sec;
   timeDest.wMilliseconds = 0;

   return true;
  }

  __time64_t GetTime() const
  {
   return _time;
  }

  int GetYear() const
  {
   struct tm * ptm;
   ptm = GetLocalTm();
   return ptm ? (ptm->tm_year) + 1900 : 0 ;
  }

  int GetMonth() const
  {
   struct tm * ptm;
   ptm = GetLocalTm();
   return ptm ? ptm->tm_mon + 1 : 0;
  }

  int GetDay() const
  {
   struct tm * ptm;
   ptm = GetLocalTm();
   return ptm ? ptm->tm_mday : 0 ;
  }

  int GetHour() const
  {
   struct tm * ptm;
   ptm = GetLocalTm();
   return ptm ? ptm->tm_hour : -1 ;
  }

  int GetMinute() const
  {
   struct tm * ptm;
   ptm = GetLocalTm();
   return ptm ? ptm->tm_min : -1 ;
  }

  int GetSecond() const
  {
   struct tm * ptm;
   ptm = GetLocalTm();
   return ptm ? ptm->tm_sec : -1 ;
  }

  int GetDayOfWeek() const
  {
   struct tm * ptm;
   ptm = GetLocalTm();
   return ptm ? ptm->tm_wday + 1 : 0 ;
  }

 private:
  __time64_t _time;
 };
 
}


--------------------next---------------------

阅读(628) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~