龙仪的家

导航

<2006年11月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

随笔分类

文章分类

收藏夹

随笔档案

文章档案

统计

我的常用网址

代码库之十七-时间类

有好事者,比如我,看见别人好的代码功能上的缺陷(而我又需要这些功能),忍不住改一下,本着共享的原则贴了出来。这个代码是在知识库中的代码库中的,其中没有提供毫秒的接口,也没有格式化功能。所以再让大家见笑一下。同时感谢这位兄弟,不要告我抄袭:-)。希望大家支持,多提宝贵意见!

////////////////////////////////////////////////////////H////////////////////////////////////////////////////////////////////

 

#ifndef LGLIB_TIME
#define LGLIB_TIME

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include 
<time.h>
#include 
<sys/timeb.h>
namespace lglib
{
    
class __LGLIB__ lg_Time 
    
{
    
public:
        
static lg_Time PASCAL GetCurrentTime();
        lg_Time();
        lg_Time(_timeb time);
        lg_Time(time_t time);
        lg_Time(
int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,int nDST = -1);
        lg_Time(WORD wDosDate, WORD wDosTime, 
int nDST = -1);
        lg_Time(
const lg_Time& timeSrc);
        lg_Time(
const SYSTEMTIME& sysTime, int nDST = -1);
        lg_Time(
const FILETIME& fileTime, int nDST = -1);
        
virtual ~lg_Time();
        
        
const lg_Time& operator=(const lg_Time& timeSrc);
        
const lg_Time& operator=(time_t t);

        
struct tm* GetGmtTm(struct tm* ptm = NULL) const;
        
struct tm* GetLocalTm(struct tm* ptm = NULL) const;
        BOOL GetAsSystemTime(SYSTEMTIME
& timeDest) const;

        time_t GetTime() 
const;
        
int GetYear() const;
        
int GetMonth() const;       // month of year (1 = Jan)
        int GetDay() const;         // day of month
        int GetHour() const;
        
int GetMinute() const;
        
int GetSecond() const;
        unsigned 
short GetMillSecond() const;
        
int GetDayOfWeek() const;   // 1=Sun, 2=Mon, , 7=Sat

        
long operator-(lg_Time time) const;
        BOOL 
operator==(lg_Time time) const;
        BOOL 
operator!=(lg_Time time) const;
        BOOL 
operator<(lg_Time time) const;
        BOOL 
operator>(lg_Time time) const;
        BOOL 
operator<=(lg_Time time) const;
        BOOL 
operator>=(lg_Time time) const;

    
private:
        
struct _timeb m_MillTime;
    }
;
    
class __LGLIB__ lg_TimeString : public lg_Time
    
{
    
public:
        lg_TimeString();
        
~lg_TimeString();
        LPCTSTR Format(
const char* lpFormat);//,
    private:
        
char m_chBuffer[MAX_PATH];
    }
;
}

#endif // LGLIB_TIME


////////////////////////////////////CPP////////////////////////////////<-不知道为何,这段代码总是插不全,只好直接贴出来

#include "stdafx.h"
#include "lg_Time.h"
#include <stdio.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
namespace lglib
{

 lg_Time::lg_Time()
 {
  _ftime( &m_MillTime );
 }

 lg_Time::~lg_Time()
 {
 }
 lg_Time::lg_Time(_timeb time)
 {
  m_MillTime = time;
 }
 lg_Time::lg_Time(time_t time)
 {
  m_MillTime.time = time;
  m_MillTime.millitm = 0;
 }

 lg_Time::lg_Time(const lg_Time& timeSrc)
 {
  m_MillTime = timeSrc.m_MillTime;
 }

 lg_Time::lg_Time(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,
  int nDST)
 {
  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;
  m_MillTime.time = mktime(&atm);
  ASSERT(m_MillTime.time != -1);       // indicates an illegal input time
 }

 lg_Time::lg_Time(WORD wDosDate, WORD wDosTime, int nDST)
 {
  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;
  m_MillTime.time = mktime(&atm);
  ASSERT(m_MillTime.time != -1);       // indicates an illegal input time
 }

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

 lg_Time::lg_Time(const FILETIME& fileTime, int nDST)
 {
  FILETIME localTime;
  if (!FileTimeToLocalFileTime(&fileTime, &localTime))
  {
   m_MillTime.time = 0;
   return;
  }

  SYSTEMTIME sysTime;
  if (!FileTimeToSystemTime(&localTime, &sysTime))
  {
   m_MillTime.time = 0;
   return;
  }

  lg_Time timeT(sysTime, nDST);
  *this = timeT;
 }

 const lg_Time& lg_Time::operator=(const lg_Time& timeSrc)
 {
  m_MillTime = timeSrc.m_MillTime; return *this;
 }

 const lg_Time& lg_Time::operator=(time_t t)
 {
  m_MillTime.time = t; return *this;
 }

 time_t lg_Time::GetTime() const
 {
  return m_MillTime.time;
 }

 int lg_Time::GetYear() const
 {
  return (GetLocalTm(NULL)->tm_year) + 1900;
 }

 int lg_Time::GetMonth() const
 {
  return GetLocalTm(NULL)->tm_mon + 1;
 }

 int lg_Time::GetDay() const
 {
  return GetLocalTm(NULL)->tm_mday;
 }

 int lg_Time::GetHour() const
 {
  return GetLocalTm(NULL)->tm_hour;
 }

 int lg_Time::GetMinute() const
 {
  return GetLocalTm(NULL)->tm_min;
 }

 int lg_Time::GetSecond() const
 {
  return GetLocalTm(NULL)->tm_sec;
 }
 unsigned short lg_Time::GetMillSecond() const
 {
  return m_MillTime.millitm;
 }
 int lg_Time::GetDayOfWeek() const
 {
  return GetLocalTm(NULL)->tm_wday + 1;
 }
 long lg_Time::operator-(lg_Time time) const
 {
  if(m_MillTime.time > time.m_MillTime.time)
   return (m_MillTime.time - time.m_MillTime.time);
  return (time.m_MillTime.time - m_MillTime.time);
 }
 BOOL lg_Time::operator==(lg_Time time) const
 {
  return m_MillTime.time == time.m_MillTime.time;
 }

 BOOL lg_Time::operator!=(lg_Time time) const
 {
  return m_MillTime.time != time.m_MillTime.time;
 }

 BOOL lg_Time::operator<(lg_Time time) const
 {
  return m_MillTime.time < time.m_MillTime.time;
 }

 BOOL lg_Time::operator>(lg_Time time) const
 {
  return m_MillTime.time > time.m_MillTime.time;
 }

 BOOL lg_Time::operator<=(lg_Time time) const
 {
  return m_MillTime.time <= time.m_MillTime.time;
 }
 BOOL lg_Time::operator>=(lg_Time time) const
 {
  return m_MillTime.time >= time.m_MillTime.time;
 }


 lg_Time PASCAL lg_Time::GetCurrentTime()
 {
  _timeb TempTime;
  _ftime( &TempTime );
  return lg_Time(TempTime);
 }

 struct tm* lg_Time::GetGmtTm(struct tm* ptm) const
 {
  if (ptm != NULL)
  {
   *ptm = *gmtime(&m_MillTime.time);
   return ptm;
  }
  else
   return gmtime(&m_MillTime.time);
 }

 struct tm* lg_Time::GetLocalTm(struct tm* ptm) const
 {
  if (ptm != NULL)
  {
   struct tm* ptmTemp = localtime(&m_MillTime.time);
   if (ptmTemp == NULL)
    return NULL;    // indicates the m_MillTime.time was not initialized!

   *ptm = *ptmTemp;
   return ptm;
  }
  else
   return localtime(&m_MillTime.time);
 }

 BOOL lg_Time::GetAsSystemTime(SYSTEMTIME& timeDest) const
 {
  struct tm* ptm = GetLocalTm(NULL);
  if (ptm == NULL)
   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;
 }
 lg_TimeString::lg_TimeString()
 {
  memset(m_chBuffer,0,MAX_PATH);
 }
 lg_TimeString::~lg_TimeString()
 {
 }
 LPCTSTR lg_TimeString::Format(const char* lpFormat)//,...
 {
  int nLen = strlen(lpFormat);
  if(nLen >= MAX_PATH||nLen <= 0)
   return NULL;
  //char chTemp[MAX_PATH];
//  va_list args;
//  va_start(args, lpFormat);
//  _vsnprintf(chTemp, _countof(m_chBuffer), lpFormat, args);
//  va_end(args);
  memset(m_chBuffer,0,MAX_PATH);
  char* p1 = m_chBuffer;
  const char* pTemp = lpFormat;
  const char* pTempNext = NULL;
  while(pTemp[0] != '\0')
  {
   pTempNext =  CharNext(pTemp);
   int n1 = pTempNext - pTemp;
   if(n1 == 1)
   {
    char chTemp[10];
    memset(chTemp,0,10);
    if(pTemp[0] == '%')
    {
     switch(pTemp[1])
     {
     case 'N':
     case 'n':
      sprintf(chTemp,"%.4d",GetYear());
      break;
     case 'Y':
     case 'y':
      sprintf(chTemp,"%.2d",GetMonth());
      break;
     case 'R':
     case 'r':
      sprintf(chTemp,"%.2d",GetDay());
      break;
     case 'S':
     case 's':
      sprintf(chTemp,"%.2d",GetHour());
      break;
     case 'F':
     case 'f':
      sprintf(chTemp,"%.2d",GetMinute());
      break;
     case 'M':
     case 'm':
      sprintf(chTemp,"%.2d",GetSecond());
      break;
     case 'H':
     case 'h':
      sprintf(chTemp,"%.3d",GetMillSecond());
     break;
     default:
      memcpy(p1,pTemp,n1);
      p1+=n1;
      pTemp = pTempNext;
      continue;
     }
     int nLen1 = strlen(chTemp);
     memcpy(p1,chTemp,nLen1);
     p1+=nLen1;
     pTemp = pTempNext + 1;
     continue;
    }
    else
    {
     memcpy(p1,pTemp,n1);
     p1+=n1;
     pTemp = pTempNext;
     continue;
    }
   }
   memcpy(p1,pTemp,n1);
   p1+=n1;
   pTemp = pTempNext;
  }
  return m_chBuffer;
 }
}

posted on 2006-11-20 12:36 龙仪 阅读(1608) 评论(3)  编辑 收藏

评论

# re: 代码库之十七-时间类 2006-11-21 08:41 晓寒

我这里也使用到一个时间。嗬嗬,感觉功能也挺全的。

http://blog.vckbase.com/flyingleaf/archive/2006/08/15/21892.html

# re: 代码库之十七-时间类 2006-11-21 09:53 HateMath的网上田园

"看见别人好的代码功能上的缺陷(而我又需要这些功能),忍不住改一下"

完美型性格的人,做事力求完美,呵呵.

# to:晓寒 2006-11-21 13:56 龙仪

嗯,看过了,不错!就是没有文件时间的操作,和时间的格式化功能。另外你的时间的初始化为什么不放在构造里面呢?
赶明有时间了,我打算根据这个类再写个阴历类

标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]