diff options
Diffstat (limited to 'src/date.hh')
| -rw-r--r-- | src/date.hh | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/date.hh b/src/date.hh new file mode 100644 index 0000000..8db396e --- /dev/null +++ b/src/date.hh @@ -0,0 +1,70 @@ +#ifndef DATE_HH +#define DATE_HH + +#include <string> +#include <time.h> + +class Date { +public: + Date() + : time_(-1) {} + + Date(time_t time) + : time_(time) {} + + Date(Date const&) = default; + + Date& operator=(Date const&) = default; + + bool empty() const { + return time_ < 0; + } + + time_t value() const { + return time_; + } + + // Return day of date (ie, set time to 00:00:00) + Date day(bool local_time = true) const; + + bool operator==(Date const& date) const { + return empty() ? date.empty() : time_ == date.time_; + } + + bool operator!=(Date const& date) const { + return !(*this == date); + } + + bool operator<(Date const& date) const { + if (empty()) + return !date.empty(); + if (date.empty()) + return false; + return time_ < date.time_; + } + + bool operator<=(Date const& date) const { + return *this < date || *this == date; + } + + bool operator>(Date const& date) const { + return !(*this <= date); + } + + bool operator>=(Date const& date) const { + return !(*this < date); + } + + // If str matches format, returns a non-empty date. + static Date from_format(std::string const& format, + std::string const& str, + bool local_time = true); + + std::string to_format(std::string const& format, + bool local_time = true) const; + +private: + time_t time_; +}; + +#endif // DATE_HH |
