CppCommon  1.0.4.1
C++ Common Library
time.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_TIME_TIME_H
10 #define CPPCOMMON_TIME_TIME_H
11 
12 #include "time/timestamp.h"
13 
14 namespace CppCommon {
15 
17 enum class Weekday
18 {
19  Sunday,
20  Monday,
21  Tuesday,
22  Wednesday,
23  Thursday,
24  Friday,
25  Saturday
26 };
27 
29 
34 template <class TOutputStream>
35 TOutputStream& operator<<(TOutputStream& stream, Weekday weekday);
36 
38 
47 class Time
48 {
49 public:
51  Time() noexcept;
53 
64  explicit Time(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0, int nanosecond = 0);
65  Time(const Time&) noexcept = default;
66  Time(Time&&) noexcept = default;
67  ~Time() noexcept = default;
68 
69  Time& operator=(const Timestamp& timestamp)
70  { return operator=(Time(timestamp)); }
71  Time& operator=(const Time&) noexcept = default;
72  Time& operator=(Time&&) noexcept = default;
73 
74  // Time offset operations
75  Time& operator+=(const Timespan& offset)
76  { return operator=(Time(utcstamp() + offset)); }
77 
78  Time& operator-=(const Timespan& offset)
79  { return operator=(Time(utcstamp() - offset)); }
80 
81  friend Time operator+(const Time& time, const Timespan& offset)
82  { return Time(time.utcstamp() + offset); }
83  friend Time operator+(const Timespan& offset, const Time& time)
84  { return Time(offset + time.utcstamp()); }
85 
86  friend Time operator-(const Time& time, const Timespan& offset)
87  { return Time(time.utcstamp() - offset); }
88  friend Time operator-(const Timespan& offset, const Time& time)
89  { return Time(offset - time.utcstamp()); }
90 
91  friend Timespan operator-(const Time& time1, const Time& time2)
92  { return time1.utcstamp() - time2.utcstamp(); }
93 
94  // Time comparison
95  friend bool operator==(const Time& time, const Timestamp& timestamp)
96  { return time == Time(timestamp); }
97  friend bool operator==(const Timestamp& timestamp, const Time& time)
98  { return Time(timestamp) == time; }
99  friend bool operator==(const Time& time1, const Time& time2) noexcept;
100 
101  friend bool operator!=(const Time& time, const Timestamp& timestamp)
102  { return time != Time(timestamp); }
103  friend bool operator!=(const Timestamp& timestamp, const Time& time)
104  { return Time(timestamp) != time; }
105  friend bool operator!=(const Time& time1, const Time& time2) noexcept;
106 
107  friend bool operator>(const Time& time, const Timestamp& timestamp)
108  { return time > Time(timestamp); }
109  friend bool operator>(const Timestamp& timestamp, const Time& time)
110  { return Time(timestamp) > time; }
111  friend bool operator>(const Time& time1, const Time& time2) noexcept;
112 
113  friend bool operator<(const Time& time, const Timestamp& timestamp)
114  { return time < Time(timestamp); }
115  friend bool operator<(const Timestamp& timestamp, const Time& time)
116  { return Time(timestamp) < time; }
117  friend bool operator<(const Time& time1, const Time& time2) noexcept;
118 
119  friend bool operator>=(const Time& time, const Timestamp& timestamp)
120  { return time >= Time(timestamp); }
121  friend bool operator>=(const Timestamp& timestamp, const Time& time)
122  { return Time(timestamp) >= time; }
123  friend bool operator>=(const Time& time1, const Time& time2) noexcept;
124 
125  friend bool operator<=(const Time& time, const Timestamp& timestamp)
126  { return time <= Time(timestamp); }
127  friend bool operator<=(const Timestamp& timestamp, const Time& time)
128  { return Time(timestamp) <= time; }
129  friend bool operator<=(const Time& time1, const Time& time2) noexcept;
130 
132  std::chrono::time_point<std::chrono::system_clock> chrono() const
133  { return utcstamp().chrono(); }
134 
136  int year() const noexcept { return _year; }
138  int month() const noexcept { return _month; }
140  Weekday weekday() const noexcept { return (Weekday)_weekday; }
142  int day() const noexcept { return _day; }
144  int hour() const noexcept { return _hour; }
146  int minute() const noexcept { return _minute; }
148  int second() const noexcept { return _second; }
150  int millisecond() const noexcept { return _millisecond; }
152  int microsecond() const noexcept { return _microsecond; }
154  int nanosecond() const noexcept { return _nanosecond; }
155 
157  UtcTimestamp utcstamp() const;
159  LocalTimestamp localstamp() const;
160 
162 
167  static Time epoch()
168  { return Time(1970, 1, 1); }
169 
171  void swap(Time& time) noexcept;
172  friend void swap(Time& time1, Time& time2) noexcept;
173 
174 protected:
176  int _year;
178  int _month;
180  int _weekday;
182  int _day;
184  int _hour;
186  int _minute;
188  int _second;
195 
197 
200  explicit Time(const Timestamp& timestamp);
201 };
202 
203 // Forward class declarations
204 class UtcTime;
205 class LocalTime;
206 
208 class UtcTime : public Time
209 {
210 public:
211  using Time::Time;
212  using Time::chrono;
213 
217 
220  explicit UtcTime(const Timestamp& timestamp);
222 
225  template <class Clock, class Duration>
226  explicit UtcTime(const std::chrono::time_point<Clock, Duration>& time_point) : UtcTime(Timestamp(time_point)) {}
228  UtcTime(const Time& time) : Time(time) {}
230  UtcTime(const LocalTime& time);
231 };
232 
234 class LocalTime : public Time
235 {
236 public:
237  using Time::Time;
238  using Time::chrono;
239 
243 
246  explicit LocalTime(const Timestamp& timestamp);
248 
251  template <class Clock, class Duration>
252  explicit LocalTime(const std::chrono::time_point<Clock, Duration>& time_point) : LocalTime(Timestamp(time_point)) {}
254  LocalTime(const Time& time) : Time(time) {}
256  LocalTime(const UtcTime& time);
257 };
258 
261 } // namespace CppCommon
262 
263 #include "time.inl"
264 
265 #endif // CPPCOMMON_TIME_TIME_H
Local time.
Definition: time.h:235
LocalTime(const std::chrono::time_point< Clock, Duration > &time_point)
Initialize local time with a given std::chrono time point.
Definition: time.h:252
LocalTime(const Time &time)
Initialize local time with another time value.
Definition: time.h:254
LocalTime()
Initialize local time with a current value.
Definition: time.h:241
Local timestamp.
Definition: timestamp.h:260
Time.
Definition: time.h:48
friend bool operator>(const Timestamp &timestamp, const Time &time)
Definition: time.h:109
Time & operator-=(const Timespan &offset)
Definition: time.h:78
Time() noexcept
Initialize time with an epoch time.
Definition: time.inl:44
int month() const noexcept
Get month value (1-12)
Definition: time.h:138
friend bool operator<=(const Time &time, const Timestamp &timestamp)
Definition: time.h:125
friend bool operator<(const Time &time, const Timestamp &timestamp)
Definition: time.h:113
friend bool operator<(const Timestamp &timestamp, const Time &time)
Definition: time.h:115
Time & operator=(const Time &) noexcept=default
friend bool operator!=(const Timestamp &timestamp, const Time &time)
Definition: time.h:103
LocalTimestamp localstamp() const
Get local timestamp from the current date & time value.
Definition: time.cpp:122
int year() const noexcept
Get year value (1970-2038 for 32-bit or 1970-3000 for 64-bit)
Definition: time.h:136
Time & operator=(Time &&) noexcept=default
friend bool operator<=(const Timestamp &timestamp, const Time &time)
Definition: time.h:127
int millisecond() const noexcept
Get millisecond value (0-999)
Definition: time.h:150
friend Time operator-(const Time &time, const Timespan &offset)
Definition: time.h:86
void swap(Time &time) noexcept
Swap two instances.
Definition: time.inl:259
friend Time operator-(const Timespan &offset, const Time &time)
Definition: time.h:88
int _month
Month value.
Definition: time.h:178
int _minute
Minute value.
Definition: time.h:186
std::chrono::time_point< std::chrono::system_clock > chrono() const
Convert date & time to the std::chrono time point.
Definition: time.h:132
friend Time operator+(const Timespan &offset, const Time &time)
Definition: time.h:83
static Time epoch()
Get the epoch date & time.
Definition: time.h:167
Time & operator=(const Timestamp &timestamp)
Definition: time.h:69
int _hour
Hour value.
Definition: time.h:184
friend bool operator!=(const Time &time, const Timestamp &timestamp)
Definition: time.h:101
friend Timespan operator-(const Time &time1, const Time &time2)
Definition: time.h:91
int _year
Year value.
Definition: time.h:176
int day() const noexcept
Get day value (1-31)
Definition: time.h:142
int minute() const noexcept
Get minute value (0-59)
Definition: time.h:146
int second() const noexcept
Get second value (0-59)
Definition: time.h:148
friend bool operator==(const Time &time, const Timestamp &timestamp)
Definition: time.h:95
Weekday weekday() const noexcept
Get weekday.
Definition: time.h:140
int hour() const noexcept
Get hour value (0-23)
Definition: time.h:144
int _weekday
Weekday value.
Definition: time.h:180
int _millisecond
Millisecond value.
Definition: time.h:190
UtcTimestamp utcstamp() const
Get UTC timestamp from the current date & time value.
Definition: time.cpp:100
int microsecond() const noexcept
Get microsecond value (0-999)
Definition: time.h:152
Time(const Time &) noexcept=default
friend Time operator+(const Time &time, const Timespan &offset)
Definition: time.h:81
friend bool operator==(const Timestamp &timestamp, const Time &time)
Definition: time.h:97
int _day
Day value.
Definition: time.h:182
friend bool operator>=(const Timestamp &timestamp, const Time &time)
Definition: time.h:121
int _nanosecond
Nanosecond value.
Definition: time.h:194
friend bool operator>=(const Time &time, const Timestamp &timestamp)
Definition: time.h:119
int nanosecond() const noexcept
Get nanosecond value (0-999)
Definition: time.h:154
Time(Time &&) noexcept=default
int _microsecond
Microsecond value.
Definition: time.h:192
int _second
Second value.
Definition: time.h:188
friend bool operator>(const Time &time, const Timestamp &timestamp)
Definition: time.h:107
std::chrono::system_clock::time_point chrono() const noexcept
Convert timestamp to the std::chrono time point.
Definition: timestamp.h:130
UTC time.
Definition: time.h:209
UtcTime(const Time &time)
Initialize UTC time with another time value.
Definition: time.h:228
UtcTime(const std::chrono::time_point< Clock, Duration > &time_point)
Initialize UTC time with a given std::chrono time point.
Definition: time.h:226
UtcTime()
Initialize UTC time with a current value.
Definition: time.h:215
UTC timestamp.
Definition: timestamp.h:248
C++ Common project definitions.
Definition: token_bucket.h:15
std::ostream & operator<<(std::ostream &os, const uint128_t &value)
Definition: uint128.inl:155
Weekday
Weekday.
Definition: time.h:18
Time inline implementation.
Timestamp definition.