标准C++写的类似C#的时间处理类

头文件

#pragma once
#include <windows.h>
#include <string>
#include <stdio.h>
#include <time.h>
using namespace std;

namespace Object
{
	class Object
	{
	public:
		Object(void);
		virtual bool Equals(const Object *object) = 0;
		virtual int CompareTo(const Object *value) = 0;
		virtual std::string ToString() = 0 ;

	public:
		~Object(void);
	};

	class DateTime :public Object
	{
	public:    
		DateTime(time_t seconds);
		DateTime(int year,int month,int day);
		DateTime(int year,int month,int day,int hour,int minute,int second);
		DateTime(std::string datetimeStr);//日期字符串格式 年/月/日 时:分:秒 例:2008/02/03 9:30:20 出错返回 01/01/1970 00:00:00
		DateTime(std::string datetimeStr,std::string formaterStr);
	private:
		time_t seconds;//自1970起的秒数
		tm date;
	public:
		void AddYears(const time_t years); //将指定的年份数加到此实例的值上。  
		void AddMonths(const time_t Months);//将指定的月份数加到此实例的值上。  
		void AddDays(const time_t days); //将指定的天数加到此实例的值上。 
		void AddHours(const time_t hours);//将指定的小时数加到此实例的值上。 
		void AddMinutes(const time_t minutes);//将指定的分钟数加到此实例的值上。  
		void AddSeconds(const time_t seconds); //将指定的秒数加到此实例的值上。
		void AddWeeks(const time_t weeks);//将指定的周数加到些实上的值上。
		static int Compare(const DateTime *value1,const DateTime *value2);//对两个 DateTime 的实例进行比较,并返回一个指示第一个实例是早于、等于还是晚于第二个实例的整数。  返回值:小于零 value1 小于 value2。 零 value1 等于 value2。 大于零 value1 大于 value2。 
		int CompareTo(const Object *value);//已重载。 将此实例的值与指定的 DateTime 值相比较,并指示此实例是早于、等于还是晚于指定的 DateTime 值。  
		int CompareTo(const DateTime *value);//小于零 此实例小于 value。 零 此实例等于 value。 大于零 此实例大于 value。 


		int DaysInMonth(const int year,const int months);//返回指定年和月中的天数。    
		bool Equals(const Object *object);
		bool Equals(const DateTime *dateTime);
		static bool Equals(const DateTime *value1,const DateTime *value2);
		static DateTime Parse(std::string datetimeStr);//日期字符串格式 月/日/年 时:分:秒 例:02/03/2008 9:30:20 出错返回 01/01/1970 00:00:00
		static DateTime Parse(std::string dateTimeStr,std::string formaterStr);
		std::string ToShortDateString();//将当前 DateTime 对象的值转换为其等效的短日期字符串表示形式。  
		std::string ToString();
		std::string ToString(const std::string formaterStr);//formaterStr = "%Y-%m-%d %H:%M:%S" %Y=年 %m=月 %d=日 %H=时 %M=分 %S=秒
	public:        
		int GetYear();//获取此实例所表示日期的年份部分。
		int GetMonth();//获取此实例所表示日期的年份部分。
		int GetDay();// 获取此实例所表示的日期为该月中的第几天。
		int GetHour();//获取此实例所表示日期的小时部分。 
		int GetMinute();//获取此实例所表示日期的分钟部分
		int GetSecond();//获取此实例所表示日期的秒部分。 
		int DayOfWeek(); //获取此实例所表示的日期是星期几。
		int DayOfYear();//记录今天是一年里面的第几天,从1月1日起,0-365
		static DateTime GetNow(); //返回当前日期是间
	public:
		bool operator == (DateTime &dateTime);
		bool operator > (DateTime &dateTime);
		bool operator < (DateTime &DateTime);
		bool operator >= (DateTime &DateTime);
		bool operator <= (DateTime &DateTime);
		bool operator != (DateTime &DateTime);

	private:
		void InitByStr(std::string dateTimeStr,std::string formaterStr);
	public:
		~DateTime(void);
	};




}

CPP文件

#include "StdAfx.h"
#include "SystemDatetime.h"

namespace Object
{
	DateTime::DateTime(const time_t seconds)
	{
		this->seconds = seconds;
		localtime_s(&this->date, &this->seconds);
	}
	DateTime::DateTime(int year,int month,int day)
	{
		tm t;
		t.tm_year = year-1900;
		t.tm_mon = month-1;
		t.tm_mday = day;
		t.tm_hour = 0;
		t.tm_min = 0;
		t.tm_sec = 0;
		this->seconds = mktime(&t);
		localtime_s(&this->date, &this->seconds);

	}
	DateTime::DateTime(int year,int month,int day,int hour,int minute,int second)
	{
		tm t;
		t.tm_year = year-1900;
		t.tm_mon = month-1;
		t.tm_mday = day;
		t.tm_hour = hour;
		t.tm_min = minute;
		t.tm_sec = second;
		this->seconds = mktime(&t);
		localtime_s(&this->date, &this->seconds);

	}

	DateTime::DateTime(std::string datetimeStr) //日期字符串格式 月/日/年 时:分:秒 例:02/03/2008 9:30:20 出错返回 01/01/1970 00:00:00
	{
		this->InitByStr(datetimeStr,"%d/%d/%d %d:%d:%d");
	}
	DateTime::DateTime(std::string datetimeStr,std::string formaterStr)
	{
		this->InitByStr(datetimeStr,formaterStr);
	}
	DateTime DateTime::Parse(std::string datetimeStr)
	{

		DateTime datetime(datetimeStr);
		return datetime;
	}
	DateTime DateTime::Parse(const std::string datetimeStr,const std::string formaterStr)
	{
		DateTime datetime(datetimeStr,formaterStr);
		return datetime;
	}


	void DateTime::InitByStr(std::string dateTimeStr,std::string formaterStr)
	{
		int year,month,day,hour,minutes,seconds;
		sscanf_s(dateTimeStr.c_str(),formaterStr.c_str(),&year,&month,&day,&hour,&minutes,&seconds);    
		if(year <1900) year = 1970;
		if(month <0) month = 1;
		if(day <0) day = 1;
		if(hour <0) hour = 0;
		if(minutes <0) minutes = 0;
		if(seconds <0) seconds = 0;
		tm t;
		t.tm_year = year-1900;
		t.tm_mon = month-1;
		t.tm_mday = day;
		t.tm_hour = hour;
		t.tm_min = minutes;
		t.tm_sec = seconds;
		this->seconds = mktime(&t);
		localtime_s(&this->date, &this->seconds);
	}
	int DateTime::DayOfWeek()
	{
		return this->date.tm_wday;
	}
	int DateTime::DayOfYear()
	{
		return this->date.tm_yday;
	}
	int DateTime::DaysInMonth(const int year,const int months)
	{
		return 0;
	}

	bool DateTime::Equals(const Object *object)
	{
		DateTime *dateTime = (DateTime *)object;
		if(this->seconds == dateTime->seconds)
			return true;
		return false;
	}

	bool DateTime::Equals(const DateTime *dateTime)
	{
		return this->Equals((Object *)dateTime);
	}

	bool DateTime::Equals(const DateTime *value1,const DateTime *value2)
	{
		if(value1->seconds == value2->seconds)
			return true;
		return false;
	}
	int DateTime::GetDay()
	{    
		return this->date.tm_mday;;
	}

	int DateTime::GetHour()
	{
		return this->date.tm_hour;
	}
	int DateTime::GetMinute()
	{
		return this->date.tm_min;
	}
	int DateTime::GetMonth()
	{
		return this->date.tm_mon;
	}

	DateTime DateTime::GetNow()
	{
		DateTime datetime(time(0));
		return datetime;
	}

	int DateTime::GetSecond()
	{
		return this->date.tm_sec;
	}

	int DateTime::GetYear()
	{
		return this->date.tm_year+1900;
	}

	void DateTime::AddYears(const time_t years)
	{
		this->date.tm_year = this->date.tm_year+years;
		this->seconds = mktime(&this->date);
	}
	void DateTime::AddMonths(const time_t months)
	{
		int a  =(int)((this->date.tm_mon+months)/12);

		this->date.tm_year = this->date.tm_year+a;
		this->date.tm_mon = this->date.tm_mon+(int)((this->date.tm_mon+months)%12)-1;
		this->seconds = mktime(&this->date);
	}

	void DateTime::AddDays(const time_t days)
	{    
		this->AddHours(days*24);
	}
	void DateTime::AddHours(const time_t hours)
	{
		this->AddMinutes(hours*60);
	}
	void DateTime::AddMinutes(const time_t minutes)
	{
		this->AddSeconds(minutes *60);
	}

	void DateTime::AddSeconds(const time_t seconds)
	{
		this->seconds = this->seconds+seconds;
		localtime_s(&this->date, &this->seconds);
	}
	void DateTime::AddWeeks(const time_t weeks)
	{
		this->AddDays(weeks*7);
	}


	int DateTime::Compare(const DateTime *t1,const DateTime *t2)
	{
		if( t1->seconds == t2->seconds)
			return 0;
		if(t1->seconds < t2->seconds)
			return -1;
		return 1;

	}


	int DateTime::CompareTo(const Object *value)
	{
		DateTime * dateTime =  (DateTime *)value;
		if( dateTime->seconds == this->seconds)
			return 0;
		if(this->seconds < dateTime->seconds)
			return -1;
		return 1;
	}

	int DateTime::CompareTo(const DateTime *value)
	{
		if(this->seconds == value->seconds)
			return 0;
		if(this->seconds <value->seconds)
			return -1;
		return 1;
	}


	std::string DateTime::ToShortDateString()
	{
		return this->ToString("%Y-%m-%d");
	}

	std::string DateTime::ToString()
	{
		return this->ToString("%Y-%m-%d %H:%M:%S");
	}
	std::string DateTime::ToString(const std::string formaterStr)
	{
		//strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(&t)); 
		char s[256];
		strftime(s, sizeof(s), formaterStr.c_str(), &this->date);
		std::string str(s);
		return str;

	}

	bool DateTime::operator ==( DateTime &dateTime)
	{
		return this->Equals(&dateTime);

	}
	bool DateTime::operator != (DateTime &datetime)
	{
		if(this->seconds != datetime.seconds)
			return true;
		return false;
	}
	bool DateTime::operator > (DateTime &dateTime)
	{
		if(this->seconds > dateTime.seconds)
			return true;
		return false;
	}
	bool DateTime::operator < (DateTime &datetime)
	{
		if(this->seconds <datetime.seconds)
			return true;
		return false;
	}
	bool DateTime::operator >=(DateTime &datetime)
	{
		if(this->seconds >= datetime.seconds)
			return true;
		return false;
	}

	bool DateTime::operator <=(DateTime &datetime)
	{
		if(this->seconds <= datetime.seconds)
			return true;
		return false;
	}

	DateTime::~DateTime(void)
	{

	}


	Object::Object(void)
	{

	}

	Object::~Object(void)
	{

	}

}