CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
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
14namespace CppCommon {
15
17enum class Weekday
18{
19 Sunday,
20 Monday,
21 Tuesday,
24 Friday,
26};
27
29
34template <class TOutputStream>
35TOutputStream& operator<<(TOutputStream& stream, Weekday weekday);
36
38
47class Time
48{
49public:
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;
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
174protected:
176 int _year;
182 int _day;
184 int _hour;
195
197
200 explicit Time(const Timestamp& timestamp);
201};
202
203// Forward class declarations
204class UtcTime;
205class LocalTime;
206
208class UtcTime : public Time
209{
210public:
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
234class LocalTime : public Time
235{
236public:
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() noexcept
Initialize time with an epoch time.
Definition time.inl:44
int month() const noexcept
Get month value (1-12)
Definition time.h:138
std::chrono::time_point< std::chrono::system_clock > chrono() const
Convert date & time to the std::chrono time point.
Definition time.h:132
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
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
friend bool operator<=(const Timestamp &timestamp, const Time &time)
Definition time.h:127
friend void swap(Time &time1, Time &time2) noexcept
Definition time.inl:273
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
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
Time & operator=(Time &&) noexcept=default
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
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
Time & operator=(const Time &) noexcept=default
Time & operator-=(const Timespan &offset)
Definition time.h:78
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
Time & operator=(const Timestamp &timestamp)
Definition time.h:69
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
C++ Common project definitions.
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.