AnCH Framework  0.1
Another C++ Hack Framework
date.hpp
1 /*
2  ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3  Copyright (C) 2012 Vincent Lachenal
4 
5  This file is part of ANCH Framework.
6 
7  ANCH Framework is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  ANCH Framework is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef _ANCH_DATE_DATE_H_
21 #define _ANCH_DATE_DATE_H_
22 
23 #include <memory>
24 #include <mutex>
25 #include <chrono>
26 #include <ctime>
27 
28 
29 namespace anch {
30  namespace date {
31 
32  class DateFormatter;
33  namespace formatter {
34  class IDatePartFormatter;
35  }
36 
47  class Date {
48 
49  friend class formatter::IDatePartFormatter;
50  friend class DateFormatter;
51 
52  // Attributes +
53  private:
55  static std::mutex _mutex;
56 
58  std::int64_t _timestamp;
59 
61  int32_t _years = 0;
62 
64  uint16_t _months = 0;
65 
67  uint16_t _ydays = 0;
68 
70  uint16_t _mdays = 0;
71 
73  uint16_t _wdays = 0;
74 
76  uint16_t _hours = 0;
77 
79  uint16_t _minutes = 0;
80 
82  uint16_t _seconds = 0;
83 
85  uint16_t _milliseconds = 0;
86 
88  uint16_t _microseconds = 0;
89 
91  uint16_t _nanoseconds = 0;
92  // Attributes -
93 
94  // Constructors +
95  public:
101  Date(bool init = true);
102 
108  Date(const Date& date);
109 
115  Date(const std::time_t& time);
116 
122  Date(const std::tm* const time);
123  // Constructors -
124 
125  // Destructor +
126  public:
130  virtual ~Date();
131  // Destructor -
132 
133  // Methods +
134  private:
140  template<typename R, typename P>
141  void initialize(const std::chrono::duration<R,P>& duration) noexcept {
142  _nanoseconds = static_cast<uint16_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % 1000);
143  _microseconds = static_cast<uint16_t>(std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000);
144  _milliseconds = static_cast<uint16_t>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000);
145  }
146 
152  void initialize(const std::tm* const time);
153 
157  void computeTimestamp();
158 
162  inline void computeTm(std::tm& time) const {
163  time.tm_sec = _seconds;
164  time.tm_min = _minutes;
165  time.tm_hour = _hours;
166  time.tm_mday = _mdays;
167  time.tm_mon = _months;
168  time.tm_year = _years - 1900;
169  }
170 
171  public:
179  inline bool after(const Date& date) const noexcept {
180  return _timestamp > date._timestamp;
181  }
182 
190  inline bool before(const Date& date) const noexcept {
191  return _timestamp < date._timestamp;
192  }
193 
201  inline bool equals(const Date& date) const noexcept {
202  return _timestamp == date._timestamp;
203  }
204  // Methods -
205 
206 
207  // Operators +
208  public:
215  inline bool operator > (const Date& date) const noexcept {
216  return after(date);
217  }
218 
225  inline bool operator >= (const Date& date) const noexcept {
226  return (after(date) || equals(date));
227  }
228 
235  inline bool operator < (const Date& date) const noexcept {
236  return before(date);
237  }
238 
245  inline bool operator <= (const Date& date) const noexcept {
246  return (before(date) || equals(date));
247  }
248 
255  inline bool operator == (const Date& date) const noexcept {
256  return equals(date);
257  }
258 
265  inline bool operator != (const Date& date) const noexcept {
266  return !equals(date);
267  }
268 
274  inline operator std::time_t() const noexcept {
275  std::tm time;
276  computeTm(time);
277  return std::mktime(&time);
278  }
279 
285  inline operator std::tm() const noexcept {
286  std::tm time;
287  computeTm(time);
288  return time;
289  }
290  // Operators -
291 
292  };
293 
294  }
295 }
296 
297 #endif // _ANCH_DATE_DATE_H_
bool equals(const Date &date) const noexcept
Definition: date.hpp:201
bool after(const Date &date) const noexcept
Definition: date.hpp:179
AnCH framework base namespace.
Definition: base64.hpp:28
Definition: dateFormatter.hpp:63
Definition: iDatePartFormatter.hpp:38
bool before(const Date &date) const noexcept
Definition: date.hpp:190
Definition: date.hpp:47