1
2
3
4
5
6 #ifndef SOULNG_UTIL_TIME_INCLUDED
7 #define SOULNG_UTIL_TIME_INCLUDED
8 #include <soulng/util/UtilApi.hpp>
9 #include <chrono>
10 #include <stdint.h>
11 #include <string>
12
13 namespace soulng { namespace util {
14
15 enum class Month : int8_t
16 {
17 january= 1, february, march, april, may, june, july, august, september, october, november, december
18 };
19
20 int GetMonthDays(Month month, int year);
21
22 class Date
23 {
24 public:
25 Date() : year(0), month(Month::january), day(1)
26 {
27 }
28 Date(short year_, Month month_, int8_t day_) : year(year_), month(month_), day(day_)
29 {
30 }
31 int16_t Year() const
32 {
33 return year;
34 }
35 Month GetMonth() const
36 {
37 return month;
38 }
39 int8_t Day() const
40 {
41 return day;
42 }
43 Date AddDays(int n);
44 Date AddMonths(int n);
45 Date AddYears(int n);
46 std::string ToString() const;
47 std::string ToString(bool omitDashes) const;
48 private:
49 int16_t year;
50 Month month;
51 int8_t day;
52 };
53
54 Date GetCurrentDate();
55
56 bool operator==(const Date& left, const Date& right);
57
58 inline bool operator!=(const Date& left, const Date& right)
59 {
60 return !(left == right);
61 }
62
63 bool operator<(const Date& left, const Date& right);
64
65 inline bool operator>(const Date& left, const Date& right)
66 {
67 return right < left;
68 }
69
70 inline bool operator<=(const Date& left, const Date& right)
71 {
72 return !(right > left);
73 }
74
75 inline bool operator>=(const Date& left, const Date& right)
76 {
77 return !(left < right);
78 }
79
80 Date ParseDate(const std::string& dateStr);
81
82 class DateTime
83 {
84 public:
85 DateTime() : date(), secs(0)
86 {
87 }
88 DateTime(Date date_) : date(date_), secs(0)
89 {
90 }
91 DateTime(Date date_, int32_t secs_) : date(date_), secs(secs_)
92 {
93 }
94 Date GetDate() const
95 {
96 return date;
97 }
98 int32_t Hours() const
99 {
100 return secs / 3600;
101 }
102 int32_t Minutes() const
103 {
104 return secs / 60;
105 }
106 int32_t Seconds() const
107 {
108 return secs;
109 }
110 std::string ToString() const;
111 std::string ToString(bool omitDashes, bool omitColons, bool omitMins, bool omitSecs) const;
112 private:
113 Date date;
114 int32_t secs;
115 };
116
117 DateTime GetCurrentDateTime();
118
119 bool operator==(const DateTime& left, const DateTime& right);
120
121 inline bool operator!=(const DateTime& left, const DateTime& right)
122 {
123 return !(left == right);
124 }
125
126 bool operator<(const DateTime& left, const DateTime& right);
127
128 inline bool operator>(const DateTime& left, const DateTime& right)
129 {
130 return right < left;
131 }
132
133 inline bool operator<=(const DateTime& left, const DateTime& right)
134 {
135 return !(right > left);
136 }
137
138 inline bool operator>=(const DateTime& left, const DateTime& right)
139 {
140 return !(left < right);
141 }
142
143 DateTime ParseDateTime(const std::string& dateTimeStr);
144
145 class Timestamp
146 {
147 public:
148 Timestamp() : dateTime(), nanosecs(0) {}
149 Timestamp(Date date_) : dateTime(date_), nanosecs(0) {}
150 Timestamp(Date date_, int32_t secs_) : dateTime(date_, secs_), nanosecs(0) {}
151 Timestamp(const DateTime& dateTime_) : dateTime(dateTime_), nanosecs(0) {}
152 Timestamp(const DateTime& dateTime_, int32_t nanosecs_) : dateTime(dateTime_), nanosecs(nanosecs_) {}
153 const DateTime& GetDateTime() const { return dateTime; }
154 int32_t Nanoseconds() const { return nanosecs; }
155 std::string ToString() const;
156 private:
157 DateTime dateTime;
158 int32_t nanosecs;
159 };
160
161 const int secsInDay = 24 * 3600;
162
163 bool operator==(const Timestamp& left, const Timestamp& right);
164
165 inline bool operator!=(const Timestamp& left, const Timestamp& right)
166 {
167 return !(left == right);
168 }
169
170 bool operator<(const Timestamp& left, const Timestamp& right);
171
172 inline bool operator>(const Timestamp& left, const Timestamp& right)
173 {
174 return right < left;
175 }
176
177 inline bool operator<=(const Timestamp& left, const Timestamp& right)
178 {
179 return !(right > left);
180 }
181
182 inline bool operator>=(const Timestamp& left, const Timestamp& right)
183 {
184 return !(left < right);
185 }
186
187 Timestamp GetCurrentTimestamp();
188
189 Timestamp ParseTimestamp(const std::string& timestampStr);
190
191 std::string FormatTimeMs(int32_t milliseconds);
192
193 std::int64_t CurrentMs();
194
195 int64_t GetCurrentTime();
196
197 std::string DurationStr(const std::chrono::nanoseconds& duration);
198
199 void TimeInit();
200 void TimeDone();
201
202 } }
203
204 #endif // SOULNG_UTIL_TIME_INCLUDED