AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
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#pragma once
21
22#include <memory>
23#include <mutex>
24#include <chrono>
25#include <ctime>
26
27
28namespace anch::date {
29
30 class DateFormatter;
31 namespace formatter {
33 }
34
45 class Date {
46
48 friend class DateFormatter;
49
50 // Attributes +
51 protected:
53 static std::mutex _mutex;
54
56 std::int64_t _timestamp;
57
59 int32_t _years = 0;
60
62 uint16_t _months = 0;
63
65 uint16_t _ydays = 0;
66
68 uint16_t _mdays = 0;
69
71 uint16_t _wdays = 0;
72
74 uint16_t _hours = 0;
75
77 uint16_t _minutes = 0;
78
80 uint16_t _seconds = 0;
81
83 uint16_t _milliseconds = 0;
84
86 uint16_t _microseconds = 0;
87
89 uint16_t _nanoseconds = 0;
90 // Attributes -
91
92 // Constructors +
93 public:
99 Date(bool init = true);
100
106 Date(const std::time_t& time);
107
113 Date(const std::tm* const time);
114
120 Date(const std::timespec& time);
121 // Constructors -
122
123 // Destructor +
124 public:
128 virtual ~Date();
129 // Destructor -
130
131 // Methods +
132 private:
138 template<typename R, typename P>
139 void initialize(const std::chrono::duration<R,P>& duration) noexcept {
140 _nanoseconds = static_cast<uint16_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % 1000);
141 _microseconds = static_cast<uint16_t>(std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000);
142 _milliseconds = static_cast<uint16_t>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000);
143 }
144
150 void initialize(const std::tm* const time);
151
155 void computeTimestamp();
156
160 void computeTm(std::tm& time) const;
161
162 public:
170 bool after(const Date& date) const noexcept;
171
179 bool before(const Date& date) const noexcept;
180
188 bool equals(const Date& date) const noexcept;
189 // Methods -
190
191
192 // Operators +
193 public:
200 bool operator > (const Date& date) const noexcept;
201
208 bool operator >= (const Date& date) const noexcept;
209
216 bool operator < (const Date& date) const noexcept;
217
224 bool operator <= (const Date& date) const noexcept;
225
232 bool operator == (const Date& date) const noexcept;
233
240 bool operator != (const Date& date) const noexcept;
241
247 operator std::time_t() const noexcept;
248
254 operator std::tm() const noexcept;
255
261 operator std::timespec() const noexcept;
262 // Operators -
263
264 };
265
266 inline void Date::computeTm(std::tm& time) const {
267 time.tm_sec = _seconds;
268 time.tm_min = _minutes;
269 time.tm_hour = _hours;
270 time.tm_mday = _mdays;
271 time.tm_mon = _months;
272 time.tm_year = _years - 1900;
273 }
274
275 inline bool Date::after(const Date& date) const noexcept {
276 return _timestamp > date._timestamp;
277 }
278
279 inline bool Date::before(const Date& date) const noexcept {
280 return _timestamp < date._timestamp;
281 }
282
283 inline bool Date::equals(const Date& date) const noexcept {
284 return _timestamp == date._timestamp;
285 }
286
287 inline bool Date::operator > (const Date& date) const noexcept {
288 return after(date);
289 }
290
291 inline bool Date::operator >= (const Date& date) const noexcept {
292 return (after(date) || equals(date));
293 }
294
295 inline bool Date::operator < (const Date& date) const noexcept {
296 return before(date);
297 }
298
299 inline bool Date::operator <= (const Date& date) const noexcept {
300 return (before(date) || equals(date));
301 }
302
303 inline bool Date::operator == (const Date& date) const noexcept {
304 return equals(date);
305 }
306
307 inline bool Date::operator != (const Date& date) const noexcept {
308 return !equals(date);
309 }
310
311 inline Date::operator std::time_t() const noexcept {
312 std::tm time;
313 computeTm(time);
314 return std::mktime(&time);
315 }
316
317 inline Date::operator std::tm() const noexcept {
318 std::tm time;
319 computeTm(time);
320 return time;
321 }
322
323
324 inline Date::operator std::timespec() const noexcept {
325 std::timespec time;
326 std::tm cal;
327 computeTm(cal);
328 time.tv_sec = std::mktime(&cal);
329 time.tv_nsec = _nanoseconds + 1000 * _microseconds + 1000000 * _milliseconds;
330 return time;
331 }
332
333}
Definition dateFormatter.hpp:58
Definition date.hpp:45
operator std::tm() const noexcept
Definition date.hpp:317
bool equals(const Date &date) const noexcept
Definition date.hpp:283
bool operator>(const Date &date) const noexcept
Definition date.hpp:287
uint16_t _mdays
Definition date.hpp:68
Date(const std::time_t &time)
Date(const std::tm *const time)
int32_t _years
Definition date.hpp:59
bool operator!=(const Date &date) const noexcept
Definition date.hpp:307
uint16_t _nanoseconds
Definition date.hpp:89
bool before(const Date &date) const noexcept
Definition date.hpp:279
bool operator>=(const Date &date) const noexcept
Definition date.hpp:291
uint16_t _ydays
Definition date.hpp:65
std::int64_t _timestamp
Definition date.hpp:56
Date(bool init=true)
uint16_t _minutes
Definition date.hpp:77
uint16_t _seconds
Definition date.hpp:80
uint16_t _microseconds
Definition date.hpp:86
bool operator==(const Date &date) const noexcept
Definition date.hpp:303
uint16_t _months
Definition date.hpp:62
uint16_t _wdays
Definition date.hpp:71
bool operator<(const Date &date) const noexcept
Definition date.hpp:295
bool after(const Date &date) const noexcept
Definition date.hpp:275
bool operator<=(const Date &date) const noexcept
Definition date.hpp:299
uint16_t _milliseconds
Definition date.hpp:83
uint16_t _hours
Definition date.hpp:74
static std::mutex _mutex
Definition date.hpp:53
Date(const std::timespec &time)
operator std::timespec() const noexcept
Definition date.hpp:324
Definition iDatePartFormatter.hpp:37
Date formatter namespace.
Definition date.hpp:31
Date namespace.