温馨提示:此内容可能已过期/不符合项目要求/编译无法通过。
作者:zhc
#ifndef DATE_H
#define DATE_H
#include <initializer_list>
#include <string>
class Date {
public:
/**
* @brief default constructor
*/
Date() = default ;
/**
* @brief constructor with arguments
*/
Date(int t_year, int t_month, int t_day, int t_hour, int t_minute){
m_year = t_year;
m_month = t_month;
m_day = t_day;
m_hour = t_hour;
m_minute = t_minute;
}
/**
* @brief constructor with a string
*/
Date(const std::string &dateString){
bool ok= true ;
if(dateString.size()!=16) ok = false ;
else if(dateString[4]!='-'||dateString[7]!='-'||dateString[10]!='/'||dateString[13]!=':') ok = false ;
if(ok){
int t = 0;
int num = 0;
for(int i = 0; i < dateString.size();++i ){
int tmp = (int)dateString[i];
if(tmp>=48 && tmp<=57){
t = 10*t+(tmp-48) ;
++num;
}
if(num==4){
m_year = t;
t = 0 ;
}
if(num==6){
m_month = t;
t = 0 ;
}
if(num==8){
m_day = t;
t = 0 ;
}
if(num==10){
m_hour = t;
t = 0 ;
}
if(num==12){
m_minute = t;
t = 0 ;
}
}
}
else{
m_year = 0;
m_month = 0;
m_day = 0;
m_hour = 0;
m_minute = 0;
}
}
/**
* @brief return the year of a Date
* @return a integer indicate the year of a date
*/
int getYear(void) const{
return m_year ;
}
/**
* @brief set the year of a date
* @param a integer indicate the new year of a date
*/
void setYear(const int t_year){
m_year = t_year ;
}
/**
* @brief return the month of a Date
* @return a integer indicate the month of a date
*/
int getMonth(void) const{
return m_month ;
}
/**
* @brief set the month of a date
* @param a integer indicate the new month of a date
*/
void setMonth(const int t_month){
m_month = t_month ;
}
/**
* @brief return the day of a Date
* @return a integer indicate the day of a date
*/
int getDay(void) const{
return m_day ;
}
/**
* @brief set the day of a date
* @param a integer indicate the new day of a date
*/
void setDay(const int t_day){
m_day = t_day ;
}
/**
* @brief return the hour of a Date
* @return a integer indicate the hour of a date
*/
int getHour(void) const{
return m_hour ;
}
/**
* @brief set the hour of a date
* @param a integer indicate the new hour of a date
*/
void setHour(const int t_hour){
m_hour = t_hour ;
}
/**
* @brief return the minute of a Date
* @return a integer indicate the minute of a date
*/
int getMinute(void) const{
return m_minute ;
}
/**
* @brief set the minute of a date
* @param a integer indicate the new minute of a date
*/
void setMinute(const int t_minute){
m_minute = t_minute ;
}
/**
* @brief check whether the date is valid or not
* @return the bool indicate valid or not
*/
static bool isValid(const Date &t_date){
bool ok=true;
int i= t_date.getYear();
int month = t_date.getMonth();
int day = t_date.getDay();
int days;
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
days=31;
else if (i==4||i==6||i==9||i==11)
days=30;
else if ((i%4==0 && i % 100 != 0)|| i % 400 == 0)
days=29;
else
days=28;
if (i<1000|| i>9999&&month<1&&month>12&&day<1&&day>days)
ok = false ;
if(t_date.getHour()<0|| t_date.getHour()>59||t_date.getMinute()<0||t_date.getMinute()>59)
ok = false;
return ok;
}
/**
* @brief convert a string to date, if the format is not correct return
* 0000-00-00/00:00
* @return a date
*/
static Date stringToDate(const std::string &t_dateString){
Date newdate(t_dateString) ;
return newdate ;
}
/**
* @brief convert a date to string, if the date is invalid return
* 0000-00-00/00:00
*/
static std::string dateToString(const Date &t_date){
char a[17];
a[4] = '-' ;
a[7] = '-' ;
a[10] = '/' ;
a[13] = ':' ;
a[16] = '\0' ;
a[0] = t_date.getYear()/1000;
a[1] = (t_date.getYear()/100)%10;
a[2] = (t_date.getYear()/10)%10;
a[3] = t_date.getYear()%10;
a[5] = t_date.getMonth()/10;
a[6] = t_date.getMonth()%10 ;
a[8] = t_date.getDay()/10;
a[9] = t_date.getDay()%10 ;
a[11] = t_date.getHour()/10;
a[12] = t_date.getHour()%10 ;
a[14] = t_date.getMinute()/10;
a[15] = t_date.getMinute()%10 ;
std::string s(a) ;
return s ;
}
/**
* @brief overload the assign operator
*/
Date &operator=(const Date &t_date){
m_year = t_date.getYear() ;
m_month = t_date.getMonth() ;
m_day = t_date.getDay() ;
m_hour = t_date.getHour() ;
m_minute = t_date.getMinute() ;
return *this ;
}
/**
* @brief check whether the CurrentDate is equal to the t_date
*/
bool operator==(const Date &t_date) const{
if(t_date.getYear() != m_year ) return false ;
if(t_date.getMonth() != m_month) return false ;
if(t_date.getDay() != m_year) return false ;
if(t_date.getHour() != m_hour) return false ;
if(t_date.getMinute() != m_minute) return false ;
return true ;
}
/**
* @brief check whether the CurrentDate is greater than the t_date
*/
bool operator>(const Date &t_date) const{
int t[5];
t[0] = m_year-t_date.getYear();
t[1] = m_month - t_date.getMonth();
t[2] = m_day - t_date.getDay();
t[3] = m_hour-t_date.getHour() ;
t[4] = m_minute - t_date.getMinute();
int dt = (((t[0]*12+t[1])*30+t[2])*24+t[3])*60+t[4];
if(dt>0) return true ;
else return false ;
}
/**
* @brief check whether the CurrentDate is less than the t_date
*/
bool operator<(const Date &t_date) const{
int t[5];
t[0] = m_year-t_date.getYear();
t[1] = m_month - t_date.getMonth();
t[2] = m_day - t_date.getDay();
t[3] = m_hour-t_date.getHour() ;
t[4] = m_minute - t_date.getMinute();
int dt = (((t[0]*12+t[1])*30+t[2])*24+t[3])*60+t[4];
if(dt<0) return true ;
else return false ;
}
/**
* @brief check whether the CurrentDate is greater or equal than the t_date
*/
bool operator>=(const Date &t_date) const{
int t[5];
t[0] = m_year-t_date.getYear();
t[1] = m_month - t_date.getMonth();
t[2] = m_day - t_date.getDay();
t[3] = m_hour-t_date.getHour() ;
t[4] = m_minute - t_date.getMinute();
int dt = (((t[0]*12+t[1])*30+t[2])*24+t[3])*60+t[4];
if(dt>=0) return true ;
else return false ;
}
/**
* @brief check whether the CurrentDate is less than or equal to the t_date
*/
bool operator<=(const Date &t_date) const{
int t[5];
t[0] = m_year-t_date.getYear();
t[1] = m_month - t_date.getMonth();
t[2] = m_day - t_date.getDay();
t[3] = m_hour-t_date.getHour() ;
t[4] = m_minute - t_date.getMinute();
int dt = (((t[0]*12+t[1])*30+t[2])*24+t[3])*60+t[4];
if(dt<=0) return true ;
else return false ;
}
private:
int m_year;
int m_month;
int m_day;
int m_hour;
int m_minute;
};
#endif