Files
BasicsOfComputerSoftwareEng…/FinalProject/Date.cpp
2023-06-24 19:15:06 +08:00

73 lines
1.7 KiB
C++

#include "Date.hpp"
#include <string>
Date::Date() : time(0), _isAny(true){};
Date::Date(const time_t &_time) : time(_time), _isAny(false){};
Date::Date(const tm &_time) : _isAny(false) {
tm temp = _time;
this->time = mktime(&temp);
}
void Date::setNewDate(const time_t &newTime) {
this->_isAny = false;
this->time = newTime;
}
void Date::setNewDate(const tm &newTime) {
this->_isAny = false;
tm temp = newTime;
this->time = mktime(&temp);
}
void Date::setAny() {
this->time = 0;
this->_isAny = true;
}
bool Date::isAny() const {
return this->_isAny;
}
time_t Date::getTime() const {
return this->time;
}
std::string Date::toString() const {
if (this->_isAny) {
return "[Any]";
}
char buffer[20];
tm curTime;
#if defined __APPLE__ || defined __linux__
curTime = *(localtime(&this->time));
#endif
#if defined _WIN64 || defined _WIN32
localtime_s(&curTime, &this->time);
#endif
strftime(buffer, 20, "%F", &curTime);
return std::string(buffer);
}
bool Date::operator<(const Date &otherDate) const {
return (this->_isAny || otherDate._isAny) || this->time < otherDate.time;
}
bool Date::operator>(const Date &otherDate) const {
return (this->_isAny || otherDate._isAny) || this->time > otherDate.time;
}
bool Date::operator<=(const Date &otherDate) const {
return (this->_isAny || otherDate._isAny) || this->time <= otherDate.time;
}
bool Date::operator>=(const Date &otherDate) const {
return (this->_isAny || otherDate._isAny) || this->time >= otherDate.time;
}
bool Date::operator==(const Date &otherDate) const {
return (this->_isAny || otherDate._isAny) || this->time == otherDate.time;
}