// Date.h: interface for the CFCWorldDate class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_DATE_H__FF59967B_C0A3_4CF9_9054_8578E83FF3BD__INCLUDED_)
#define AFX_DATE_H__FF59967B_C0A3_4CF9_9054_8578E83FF3BD__INCLUDED_
#include
#include
#ifdef HAVE_UNISTD_H
#include
#endif
#include
#include
class CDateTime
{
struct _FORMAT
{
int local;
std::string Key;
};
public:
long m_lTime;
int m_iYear;
int m_iMonth;
int m_iDay;
int m_iHour;
int m_iMinute;
int m_iSecond;
int m_iWeek;
int m_iDiff;//UTC与LOCAL时间之间所差的秒数,用本地时间减去UTC时间
bool m_bLocal;
public:
//得到一个月的天数
int DaysInMonth();
//判断是否为润年
bool isLeapYear();
CDateTime(bool bLocal= false);//false表示构造的UTC时间,它构造的当前时间
CDateTime(int iYear,int iMonth,int iDay,int iHour,int iMinute,int iSecond,bool bLocal= false);//默认产生UTC时间
virtual ~CDateTime();
void Today(bool bLocal= false);//得到当前日期,默认产生UTC时间
int GetYear() {return m_iYear;}
int GetMonth(){ return m_iMonth ;}
int GetDay() { return m_iDay;}
int GetWeek() {return m_iMonth;}
CDateTime& AddYears(int iValue);
CDateTime& AddMonths(int iValue);
CDateTime& AddDays(int iValue);
CDateTime& AddWeeks(int iValue);
CDateTime& AddHours(int iValue);
CDateTime& AddMinutes(int iValue);
CDateTime& AddSeconds(int iValue);
//比较时间大小,如果比OtherDate大,返回1,如果相等,返回0,如果小于,返回-1
int Compare(CDateTime& OtherDate);
//格式化时间
std::string ToString(std::string sFormat="YYYY-MM-DD HH:MI:SS");
CDateTime StringToTime(std::string sDate,std::string sFormat="YYYY-MM-DD HH:MI:SS");
//把时间从UTC到本地时间转换
CDateTime& UTCToLocalTime();
//把时间从本地到UTC时间转换
CDateTime& LocalToUTCTime();
//得到两个时间相差的秒数
long operator-(CDateTime& DT);
private:
int GetWeekFromLongTime();
void ToTime(long lDate);
long ToLong();
};
#endif // !defined(AFX_DATE_H__FF59967B_C0A3_4CF9_9054_8578E83FF3BD__INCLUDED_)
// Date.cpp: implementation of the CDateTime class.
//
//////////////////////////////////////////////////////////////////////
#include "DateTime.h"
#include
//////////////////////////////////////////////////////////////////////
// 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; //小于
}
if( m_iHour > OtherDate.m_iHour )
return 1;
if( m_iHour < OtherDate.m_iHour )
return -1;
if( m_iMinute> OtherDate.m_iMinute )
return 1;
if( m_iMinute < OtherDate.m_iMinute )
return -1;
if( m_iSecond > OtherDate.m_iSecond )
return 1;
if( m_iSecond < OtherDate.m_iSecond )
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_iSecond = 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());
}
#include
#include
class data
{
public:
data(int,int,int);
runnian();
void addoneday();
void showdata();
private:
int year;
int month;
int day;
};
data::data(int d,int m,int y)
{
day=d;
month=m;
year=y;
}
data::runnian()
{
if(year%400==0)
return true;
else if (year%100!=0&&year%4==0)
return true;
else
return false;
}
void data::showdata()
{
cout<<"日期为:"<
void data::addoneday()
{
if(month==1||month==3||month==5||month==7||month==8||month==10)
{
if(day==31)
{
day=1;
month++;
}
else
day++;
}
else if(month==12)
{
if(day==31)
{
month =1;
day=1;
year++;
}
}
else if(month==2)
{
if(runnian()&&day==29)
{
month++;
day=1;
}
else if(!runnian()&&day==28) //runnian=leapyear阳历或阴历中有闰日的年,或阴阳历中有闰月的年。
{
month++;
day=1;
}
else
day++;
}
else
{
if(day==30)
{
month++;
day=1;
}
else
day++;
}
}
int main()
{
data d(17,11,2010);
d.showdata();
d.addoneday();
d.showdata();
return 0;
}
运行结果:
日期为:17/11/2010
日期为:18/11/2010
Press any key to continue
能实现日期加+1
VFP程序中运用命令窗口
例如?{^2010/11/17}+12
?{^2010/11/17}-15
?{^2010/11/17}-{^2009/11/17}
运行就可以得到结果了
EXCEL