📄 date.cpp
字号:
#include "Date.h"
#include <iostream>
#include <string>
Date::Date():year(0),month(0),day(0),hour(0),minute(0),second(0) {}
Date::Date(int month, int day, int year, int hour, int minute, int second)
:year(year),month(month),day(day),hour(hour),minute(minute),second(second) {}
void Date::setYear(int& year) {
this->year = year;
}
void Date::setMonth(int& month) {
this->month = month;
}
void Date::setDay(int& day) {
this->day = day;
}
void Date::setHour(int& hour) {
this->hour = hour;
}
void Date::setMinute(int& minute) {
this->minute = minute;
}
void Date::setSecond(int& second) {
this->second = second;
}
int Date::getYear() const {
return year;
}
int Date::getDay() const {
return day;
}
int Date::getMonth() const {
return month;
}
int Date::getHour() const {
return hour;
}
int Date::getMinute() const {
return minute;
}
int Date::getSecond() const {
return second;
}
bool Date::operator ==(const Date &rhs) {
if (this->year == rhs.getYear() && this->month == rhs.getMonth() && this->day == rhs.getDay() &&
this->hour == rhs.getHour() && this->minute == rhs.getMinute() && this->second == rhs.getSecond())
return true;
else
return false;
}
bool Date::operator <(const Date &rhs) {
if (this->year < rhs.getYear())
return true;
else if (this->year > rhs.getYear())
return false;
else {
if (this->month < rhs.getMonth())
return true;
else if (this->month > rhs.getMonth())
return false;
else {
if (this->day < rhs.getDay())
return true;
else if (this->day > rhs.getDay())
return false;
else {
if (this->hour < rhs.getHour())
return true;
else if (this->hour > rhs.getHour())
return false;
else {
if (this->minute < rhs.getMinute())
return true;
else if (this->minute > rhs.getMinute())
return false;
else {
if (this->second < rhs.getSecond())
return true;
else return false;
}
}
}
}
}
}
ostream &operator<<(ostream& os, const Date& date) {
os << date.getMonth() << "/" << date.getDay() << "/" << date.getYear() << " " << date.getHour()
<< ":" << date.getMinute() << ":" << date.getSecond();
return os;
}
istream &operator>>(istream& is, Date& date) {
string str;
is >> str;
string::size_type pos1,pos2,pos3;
pos1 = str.find_first_of("/");
pos2 = str.find_last_of("/");
pos3 = str.find_first_of(" ");
int month = atoi(str.substr(0,pos1).c_str());
int day = atoi(str.substr(pos1 + 1,pos2).c_str());
int year = atoi(str.substr(pos2 + 1,pos3).c_str());
is >> str;
pos1 = str.find_first_of(":");
pos2 = str.find_last_of(":");
pos3 = str.find_last_of(" \n");
int hour = atoi(str.substr(0,pos1).c_str());
int minute = atoi(str.substr(pos1 + 1,pos2).c_str());
int second = atoi(str.substr(pos2 + 1,pos3).c_str());
date.setYear(year);
date.setMonth(month);
date.setDay(day);
date.setHour(hour);
date.setMinute(minute);
date.setSecond(second);
return is;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -