展开全部
// 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表示构造32313133353236313431303231363533e59b9ee7ad9431333262376666的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()