Kata Tournament Runner  0.1.00
 All Classes Namespaces Functions Variables Enumerations Groups Pages
date.h
1 #ifndef TOURNAMENT_RUNNER_DATE_H_
2 #define TOURNAMENT_RUNNER_DATE_H_
3 
4 #include <cstdint>
5 #include <ostream>
6 
7 #include <boost/date_time/gregorian/gregorian.hpp>
8 #include <boost/serialization/nvp.hpp>
9 #include <boost/serialization/access.hpp>
10 #include <boost/serialization/split_member.hpp>
11 
12 using boost::serialization::make_nvp;
13 
14 namespace TournamentRunner
15 {
16 
21 class Date
22 {
23 public:
28  Date () = default;
33  explicit Date (const std::string& date_as_string);
34 
39  explicit Date (boost::gregorian::date date);
40 
46  bool is_valid () const;
51  std::string to_iso_extended_string () const;
52 
57  int32_t year () const;
62  int32_t month () const;
67  int32_t day () const;
68 
69  Date& operator= (const Date&) = default;
70  Date (const Date&) = default;
71 
72 private:
73  static boost::gregorian::date date_from_string (const std::string& date_as_string);
74 
75  friend class boost::serialization::access;
76 
77  friend bool operator==(const Date&, const Date&);
78 
82  template<class Archive>
83  void save (Archive& archive,
84  const unsigned int version) const;
85 
89  template<class Archive>
90  void load (Archive& archive,
91  const unsigned int version);
92 
93  BOOST_SERIALIZATION_SPLIT_MEMBER()
94 
95  boost::gregorian::date date_{boost::gregorian::day_clock::local_day()};
96 };
97 
98 bool operator== (const Date& lhs, const Date& rhs);
99 
100 bool operator!= (const Date& lhs, const Date& rhs);
101 
102 std::ostream& operator<<(std::ostream& os,
103  const Date& date);
104 
105 template<class Archive>
106 void Date::save (Archive& archive,
107  const unsigned int /*version*/) const
108 {
109  const auto date_as_string = boost::gregorian::to_iso_extended_string(date_);
110  archive & make_nvp("Date", date_as_string);
111 }
112 
113 template<class Archive>
114 void Date::load (Archive& archive,
115  const unsigned int /*version*/)
116 {
117  auto date_as_string = std::string{};
118  archive & make_nvp("Date", date_as_string);
119  date_ = boost::gregorian::from_simple_string(date_as_string);
120 }
121 
122 } // namespace TournamentRunner
123 
124 #endif //TOURNAMENT_RUNNER_DATE_H_
int32_t day() const
Return the day of month.
Definition: date.cpp:38
Date()=default
Default empty Date constructor.
std::string to_iso_extended_string() const
Return date as extended iso string.
Definition: date.cpp:23
int32_t month() const
Return the month.
Definition: date.cpp:33
bool is_valid() const
Return true if the date is valid.
Definition: date.cpp:18
The Date class provides Date handling based on the boost::gregorian::date.
Definition: date.h:21
int32_t year() const
Return the year.
Definition: date.cpp:28