⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datetime.cpp

📁 一个时间处理的类
💻 CPP
字号:
// Date.cpp: implementation of the CDateTime class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DateTime.h"
#include <time.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
const char *dayname[] = {"Sunday","Monday","Tuesday","Wednesday",
	   "Thursday","Friday","Saturday"} ;

const char *mname[] = {"January","February","March","April","May",
	   "June","July","August","September","October","November","December"};

static int MonDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
CDateTime::CDateTime(bool bLocal)
{
   m_bLocal = bLocal;
   Today(bLocal);
}

CDateTime::~CDateTime()
{
    
}
CDateTime::CDateTime(int iYear,int iMonth,int iDay,int iHour,int iMinute,int iSecond,bool bLocal)//默认产生UTC时间
{
    m_iYear = iYear;
	m_iMonth = iMonth;
	m_iDay = iDay;
    m_iHour = iHour;
	m_iMinute = iMinute;
	m_iSecond = iSecond;

	m_bLocal = bLocal;

	m_lTime = ToLong();
	ToTime(m_lTime);

}

void CDateTime::Today(bool bLocal)//得到当前日期,默认产生UTC时间
{
	time_t t;
	t= time(NULL);
    struct tm *today;
	m_bLocal = bLocal;
    if(bLocal)
	{
		today = localtime( &t ); /* Convert to local time. */
		
	}
	else //UTC时间
	{
		today = gmtime( &t );

	}
	m_iYear = today->tm_year + 1900;
	m_iMonth = today->tm_mon + 1;
	m_iDay = today->tm_mday;
	m_iWeek = today->tm_wday;
	m_iHour = today->tm_hour;
	m_iMinute = today->tm_min;
	m_iSecond = today->tm_sec;
	m_lTime = ToLong();
	
}
CDateTime& CDateTime::AddYears(int iValue)
{
	if(iValue == 0)
	{
		return *this; 
	}
    m_iYear += iValue;
	m_lTime = ToLong();
	return *this;
}
CDateTime& CDateTime::AddMonths(int iValue)
{
	if(iValue == 0)
	{
		return *this; 
	}
    iValue += m_iMonth;                    // Since month is of type unsigned char
                                        // I choose to use nCount as the main
    while (iValue < 1)                  // counter - TML
    {
        iValue += 12;
        m_iYear--;
    }

    while (iValue > 12)
    {
        iValue -= 12;
        m_iYear++;
    }

    m_iMonth = (unsigned char) iValue;
	m_lTime = ToLong();	
	return *this;
}
CDateTime& CDateTime::AddDays(int iValue)
{
	if(iValue == 0)
	{
		return *this; 
	}	
    m_lTime += iValue;
	ToTime(m_lTime);
	return *this;
}
CDateTime& CDateTime::AddWeeks(int iValue)
{
	if(iValue == 0)
	{
		return *this; 
	}
	m_lTime += 7 * iValue;
	ToTime(m_lTime);
	return *this;
}
CDateTime& CDateTime::AddHours(int iValue)
{
	if(iValue == 0)
	{
		return *this; 
	}
	iValue += m_iHour;
	while(iValue < 0 )
	{
        m_lTime --;
		iValue += 24;
	}
	while (iValue > 23)
	{
		m_lTime ++;
		iValue -= 24;
	}
	m_iHour = iValue;
	ToTime(m_lTime);
	return *this;
}
CDateTime& CDateTime::AddMinutes(int iValue)
{
	if(iValue == 0)
	{
		return *this; 
	}
	int iHour = 0;
	iValue += m_iMinute;
	while(iValue < 0 )
	{
        iHour --;
		iValue += 60;
	}
	while (iValue > 59)
	{
		iHour ++;
		iValue -= 60;
	}
	m_iMinute = iValue;
	AddHours(iHour);
	return *this;

}
CDateTime& CDateTime::AddSeconds(int iValue)
{
	if(iValue == 0)
	{
		return *this; 
	}
	int iMinute = 0;
	iValue += m_iSecond;
	while(iValue < 0 )
	{
        iMinute --;
		iValue += 60;
	}
	while (iValue > 59)
	{
		iMinute ++;
		iValue -= 60;
	}
	m_iSecond = iValue;
	AddMinutes(iMinute);
	return *this;
}

int CDateTime::Compare(CDateTime& OtherDate)
{
	if(m_lTime > OtherDate.m_lTime  )
	{
         return 1; //大于
	}
	if(m_lTime < OtherDate.m_lTime)
	{
		return -1; //小于
	}
    return 0;
}
std::string CDateTime::ToString(std::string sFormat)
{
	if(m_lTime <= 0)
	{
        return "";
	}
    _FORMAT pos[6];
    int i,j;
	int iCurPos = 0;
	char sData[5];
	std::string sTime;

    pos[0].local = sFormat.find("YYYY");
    pos[0].Key = "YYYY"; 
	pos[1].local = sFormat.find("MM");

	pos[1].Key = "MM"; 

	
    pos[2].local = sFormat.find("DD");

	pos[2].Key = "DD"; 

	
    pos[3].local = sFormat.find("HH");

	pos[3].Key = "HH"; 

	
    pos[4].local = sFormat.find("MI");
	pos[4].Key = "MI"; 

	
    pos[5].local = sFormat.find("SS");
	pos[5].Key = "SS"; 



	for(i = 0; i < 6 ; i++)
	{
		if(pos[i].local == -1)
		{
			return "";
		}
		for(j = i + 1; j < 6 ; j ++)
		{

			if(pos[i].local > pos[j].local && pos[j].local!= -1)
			{
                _FORMAT temp;
				temp  = pos[i];
				pos[i] = pos[j];
				pos[j] = temp;
			}
		}
        
	}
	
	for(i = 0; i < 6; i++)
	{
		
        sTime += sFormat.substr(iCurPos,pos[i].local - iCurPos);
	    if(pos[i].Key == "YYYY")
		{
			memset(sData,0,5);
			sprintf(sData,"%04d",m_iYear);
            sTime += sData;
		}
	    if(pos[i].Key == "MM")
		{
			memset(sData,0,5);
			sprintf(sData,"%02d",m_iMonth);
            sTime += sData;
		}
	    if(pos[i].Key == "DD")
		{
			memset(sData,0,5);
			sprintf(sData,"%02d",m_iDay);
            sTime += sData;
		}
	    if(pos[i].Key == "HH")
		{
			memset(sData,0,5);
			sprintf(sData,"%02d",m_iHour);
            sTime += sData;
		}
	    if(pos[i].Key == "MI")
		{
			memset(sData,0,5);
			sprintf(sData,"%02d",m_iMinute);
            sTime += sData;
		}
	    if(pos[i].Key == "SS")
		{
			memset(sData,0,5);
			sprintf(sData,"%02d",m_iSecond);
            sTime += sData;
		}
		iCurPos = pos[i].local + pos[i].Key.length() ;
	}
	
	return sTime;
	
}

CDateTime CDateTime::StringToTime(std::string sDate,std::string sFormat)
{
    _FORMAT pos[6];
    int i,j;
	int iCurPos = 0;
	char sData[5];
	std::string sTime;
    m_iYear = 0;
    m_iMonth = 0;
	m_iDay = 0;
	m_iHour = 0;
	m_iMinute = 0;
	m_iSecond = 0;
	m_iWeek = 0;
	m_lTime = 0;

    pos[0].local = sFormat.find("YYYY");
	pos[0].Key = "YYYY"; 

	pos[1].local = sFormat.find("MM");
	pos[1].Key = "MM"; 

    pos[2].local = sFormat.find("DD");
	pos[2].Key = "DD"; 

    pos[3].local = sFormat.find("HH");
	pos[3].Key = "HH"; 

    pos[4].local = sFormat.find("MI");
	pos[4].Key = "MI"; 

    pos[5].local = sFormat.find("SS");
	pos[5].Key = "SS"; 


	for(i = 0; i < 6 ; i++)
	{
		if(pos[i].local == -1)
		{
			return *this;
		}
		for(j = i + 1; j < 6 ; j ++)
		{
			if(pos[i].local > pos[j].local)
			{
                _FORMAT temp;
				temp  = pos[i];
				pos[i] = pos[j];
				pos[j] = temp;
			}
		}
        
	}
	
	for(i = 0; i < 6; i++)
	{
		
       	memset(sData,0,5);
		if(pos[i].local != -1)
		{
			
			if(sDate.length() < pos[i].local + pos[i].Key.length())
			{
				return *this;
			}
			strcpy(sData,sDate.substr(pos[i].local,pos[i].Key.length()).c_str()); 
		}
		else
		{
			continue;
		}
		
	    if(pos[i].Key == "YYYY")
		{
           
			m_iYear = atoi(sData);

		}
	    if(pos[i].Key == "MM")
		{
			m_iMonth = atoi(sData);
		}
	    if(pos[i].Key == "DD")
		{
			m_iDay = atoi(sData);
		}
	    if(pos[i].Key == "HH")
		{
			m_iHour = atoi(sData);
			
		}
	    if(pos[i].Key == "MI")
		{
			m_iMinute = atoi(sData);

		}
	    if(pos[i].Key == "SS")
		{
			m_iMinute = atoi(sData);
		}
		
	}
    m_lTime = ToLong();
	return *this;
}
CDateTime& CDateTime::UTCToLocalTime()
{
    if(!m_bLocal && m_lTime > 0)
	{
		 long sSeconds;
		 CDateTime local(true);
		 CDateTime UTC(false);
         sSeconds = local- UTC;
		 AddSeconds(sSeconds);
         m_bLocal = true;
	}
	return *this;
}
CDateTime& CDateTime::LocalToUTCTime()
{
    if(m_bLocal && m_lTime > 0)
	{
		 long sSeconds;
		 CDateTime local(true);
		 CDateTime UTC(false);
         sSeconds = UTC- local;
		 AddSeconds(sSeconds);
         m_bLocal = false;
	}
	return *this;
}

long   CDateTime::operator-(CDateTime& DT)
{
	long lSeconds = 0;
	lSeconds = (m_lTime - DT.m_lTime) * 24 * 60 * 60;
    lSeconds += m_iHour * 60 * 60 + m_iMinute * 60 + m_iSecond - 
		(DT.m_iHour * 60 * 60 + DT.m_iMinute * 60 + DT.m_iSecond);
	return lSeconds;
    
}
long CDateTime::ToLong()
{
	int a,b=0;

	int work_month=m_iMonth, work_day=m_iDay, work_year=m_iYear;

	// correct for negative year

	if (work_year < 0)
		work_year++;

	if (work_month <= 2)
	{
		work_year--;
		work_month +=12;
	}

	// deal with Gregorian calendar

	if (work_year*10000. + work_month*100. + work_day >= 15821015.)
	{
		a = (int)(work_year/100.);
		b = 2 - a + a/4;
	}

	m_lTime  = (long) (365.25*work_year) +
			 (long) (30.6001 * (work_month+1))  +  work_day + 1720994L + b ;
	GetWeekFromLongTime();
	return m_lTime;
}

void CDateTime::ToTime(long lDate)
{
	long a,b,c,d,e,z,alpha;
	z = lDate + 1;

	// dealing with Gregorian calendar reform

	if (z < 2299161L)
		a = z;
	else
	{
		alpha = (long) ((z-1867216.25) / 36524.25);
		a = z + 1 + alpha - alpha/4;
	}

	b = ( a > 1721423 ? a + 1524 : a + 1158 );
	c = (long) ((b - 122.1) / 365.25);
	d = (long) (365.25 * c);
	e = (long) ((b - d) / 30.6001);

	m_iDay = (unsigned char)(b - d - (long)(30.6001 * e));
	m_iMonth = (unsigned char)((e < 13.5) ? e - 1 : e - 13);
    m_iYear = (int)((m_iMonth > 2.5 ) ? (c - 4716) : c - 4715);
	GetWeekFromLongTime();
}

int CDateTime::GetWeekFromLongTime()
{
   m_iWeek = (unsigned char) ((m_lTime + 1) % 7 + 1);
   return m_iWeek;
}

bool CDateTime::isLeapYear()
{
	return  ( (m_iYear >= 1582) ?
		  (m_iYear % 4 == 0  &&  m_iYear % 100 != 0  ||  m_iYear % 400 == 0 ):
		  (m_iYear % 4 == 0) );
}

int CDateTime::DaysInMonth()
{
    return MonDays[m_iMonth-1] + (m_iMonth==2 && isLeapYear());
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -