summaryrefslogtreecommitdiff
path: root/src/date.hh
blob: 8db396ec6b62ebcc7c6ef5de152cd86d7dba341f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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