常用的时间处理方法

作者在 2006-12-31 06:08:00 发布以下内容

我们在写程序的时候经常要做日期方面的处理,每次都重新写代码实在麻烦,不如把所有的方法做成一个类,并做成DLL,方便以后所有程序的调用.下面给出一些常用的方法的具体实现.希望各位和高手一起研究研究.
对不足之处希望大家批评改正.
//时间处理类CTimeProc
class CTimeProc 
{
public:
 int m_nYear;
 int m_nMonth;
 int m_nDay;
 int m_nHour;
 int m_nMinute;
 int m_nSecond;
 //得到2个日期相隔的天数
 int GetTimeDist(int stYear,int stMonth,int stDay,int edYear,int edMonth,int edDay);
 //判断输入月份的类型
 //该月30天则返回0;该月31天返回1;该月29天返回-1;该月28天返回-2
 int AssertMonthType(int nYear,int nMonth);
 //判断输入月份有多少天
 int DaysInMonth(int nYear,int nMonth);
 //判断输入年份是否闰年
 BOOL AssertIsLeapYear(int nYear);
 //把字符串转换为数字
 int ChangCStringToInt(CString strNumber);
 //从输入时间得到时(时间格式为:''dddd-dd-dd dd:dd:dd")
 CString SplitTimeToHour(CString strTime);
 //从输入时间得到日
 CString SplitTimeToDay(CString strTime);
 //从输入时间得到月
 CString SplitTimeToMonth(CString strTime);
 //从输入时间得到年
 CString SplitTimeToYear(CString strTime);
//得到当前系统的时间
 CString GetCurtTime();
};

CString CTimeProc::GetCurtTime()
{
//利用windowsAPI函数
 SYSTEMTIME sys; 
 GetLocalTime(&sys);
 CString str;
 str.Format("%d-%d-%d %d:%d:%d",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond);
 //把当前时间赋值给成员变量
 m_nYear=sys.wYear;
 m_nMonth=sys.wMonth;
 m_nDay=sys.wDay;
 m_nHour=sys.wHour;
 m_nMinute=sys.wMinute;
 m_nSecond=sys.wSecond;
 return str;
}
//时间格式为"dddd-dd-dd dd:dd:dd"
CString CTimeProc::SplitTimeToYear(CString strTime)
{
 strTime.Delete(4,strTime.GetLength()-4);
 return strTime;
}

CString CTimeProc::SplitTimeToMonth(CString strTime)
{
 strTime.Delete(0,5);
 if(strTime.GetAt(1)=='-')
 {
  strTime.Delete(1,strTime.GetLength()-1);
  return strTime;
 }
 else
 {
  strTime.Delete(2,strTime.GetLength()-2);
  return strTime;
 }
}

CString CTimeProc::SplitTimeToDay(CString strTime)
{
 if(strTime.GetAt(6)=='-')
 {
  strTime.Delete(0,7);
 }
 else
 {
  strTime.Delete(0,8);
 }
 if(strTime.GetAt(1)==' ')
 {
  strTime.Delete(1,strTime.GetLength()-1);
  return strTime;
 }
 else
 {
  strTime.Delete(2,strTime.GetLength()-2);
  return strTime;
 }
}

CString CTimeProc::SplitTimeToHour(CString strTime)
{
 int strlen=strTime.GetLength();
    for(int i=0;i<strlen;i++)
 {
  if(strTime.GetAt(i)==' ')break;
 }
 strTime.Delete(0,i+1);
 if(strTime.GetAt(1)==':')
 {
  strTime.Delete(1,strTime.GetLength()-1);
  return strTime;
 }
 else
 {
  strTime.Delete(2,strTime.GetLength()-2);
  return strTime;
 }
}

int CTimeProc::ChangCStringToInt(CString strNumber)
{
 long len,temp,k,result;
 len=strNumber.GetLength();
 k=0;
 result=0;
 while(len>=1)
 {
  temp=(long)(strNumber.GetAt(k)-48);
  for(int i=1;i<len;i++)
   temp=temp*10;
  result=result+temp;
  k++;
  len--;
 }
 return result;
}

//闰年返回TRUE;否则返回FALSE;
BOOL CTimeProc::AssertIsLeapYear(int nYear)
{
 if((nYear%4==0)&&(nYear%100!=0))
  return TRUE;
 if((nYear%100==0)&&!(nYear%400==0

VC++ | 阅读 1692 次
文章评论,共0条
游客请输入验证码
浏览20259次
文章分类
最新评论