diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2021-11-17 22:34:57 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2021-11-17 22:34:57 +0100 |
| commit | 6232d13f5321b87ddf12a1aa36b4545da45f173d (patch) | |
| tree | 23f3316470a14136debd9d02f9e920ca2b06f4cc /src/date.hh | |
Travel3: Simple image and video display site
Reads the images and videos from filesystem and builds a site in
memroy.
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 |
