trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
Date.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <trantor/exports.h>
18#include <stdint.h>
19#include <string>
20
21namespace trantor
22{
27class TRANTOR_EXPORT Date
28{
29 public:
30 Date() : microSecondsSinceEpoch_(0){};
31
37 explicit Date(int64_t microSec) : microSecondsSinceEpoch_(microSec){};
38
50 Date(unsigned int year,
51 unsigned int month,
52 unsigned int day,
53 unsigned int hour = 0,
54 unsigned int minute = 0,
55 unsigned int second = 0,
56 unsigned int microSecond = 0);
57
63 static const Date date();
64
70 static const Date now()
71 {
72 return Date::date();
73 }
74
75 static int64_t timezoneOffset();
76
84 const Date after(double second) const;
85
92 const Date roundSecond() const;
93
96
103 const Date roundDay() const;
104
105 ~Date(){};
106
111 bool operator==(const Date &date) const
112 {
113 return microSecondsSinceEpoch_ == date.microSecondsSinceEpoch_;
114 }
115
120 bool operator!=(const Date &date) const
121 {
122 return microSecondsSinceEpoch_ != date.microSecondsSinceEpoch_;
123 }
124
129 bool operator<(const Date &date) const
130 {
131 return microSecondsSinceEpoch_ < date.microSecondsSinceEpoch_;
132 }
133
138 bool operator>(const Date &date) const
139 {
140 return microSecondsSinceEpoch_ > date.microSecondsSinceEpoch_;
141 }
142
147 bool operator>=(const Date &date) const
148 {
149 return microSecondsSinceEpoch_ >= date.microSecondsSinceEpoch_;
150 }
151
156 bool operator<=(const Date &date) const
157 {
158 return microSecondsSinceEpoch_ <= date.microSecondsSinceEpoch_;
159 }
160
167 {
168 return microSecondsSinceEpoch_;
169 }
170
176 int64_t secondsSinceEpoch() const
177 {
178 return microSecondsSinceEpoch_ / MICRO_SECONDS_PER_SEC;
179 }
180
186 struct tm tmStruct() const;
187
195 std::string toFormattedString(bool showMicroseconds) const;
196
197 /* clang-format off */
209 [[deprecated("Replaced by toCustomFormattedString")]]
210 std::string toCustomedFormattedString(const std::string &fmtStr,
211 bool showMicroseconds = false) const
212 {
213 return toCustomFormattedString(fmtStr, showMicroseconds);
214 };
215 /* clang-format on */
226 std::string toCustomFormattedString(const std::string &fmtStr,
227 bool showMicroseconds = false) const;
235 std::string toFormattedStringLocal(bool showMicroseconds) const;
236
237 /* clang-format off */
246 [[deprecated("Replaced by toCustomFormattedStringLocal")]]
247 std::string toCustomedFormattedStringLocal(const std::string &fmtStr,
248 bool showMicroseconds = false) const
249 {
250 return toCustomFormattedStringLocal(fmtStr, showMicroseconds);
251 }
252 /* clang-format on */
261 const std::string &fmtStr,
262 bool showMicroseconds = false) const;
263
271 std::string toDbStringLocal() const;
275 std::string toDbString() const;
276
282 static Date fromDbStringLocal(const std::string &datetime);
288 static Date fromDbString(const std::string &datetime);
289
301 static Date fromISOString(const std::string &datetime);
302
303 /* clang-format off */
312 [[deprecated("Replaced by toCustomFormattedString")]]
313 void toCustomedFormattedString(const std::string &fmtStr,
314 char *str,
315 size_t len) const
316 {
317 toCustomFormattedString(fmtStr, str, len);
318 }
319 /* clang-format on */
320
328 void toCustomFormattedString(const std::string &fmtStr,
329 char *str,
330 size_t len) const; // UTC
331
339 bool isSameSecond(const Date &date) const
340 {
341 return microSecondsSinceEpoch_ / MICRO_SECONDS_PER_SEC ==
342 date.microSecondsSinceEpoch_ / MICRO_SECONDS_PER_SEC;
343 }
344
350 void swap(Date &that)
351 {
352 std::swap(microSecondsSinceEpoch_, that.microSecondsSinceEpoch_);
353 }
354
355 static constexpr long MICRO_SECONDS_PER_SEC = 1000000LL;
356
357 private:
358 int64_t microSecondsSinceEpoch_{0};
359};
360} // namespace trantor
bool operator<=(const Date &date) const
Return true if the time point is not later than another.
Definition Date.h:156
int64_t microSecondsSinceEpoch() const
Get the number of milliseconds since 1970-01-01 00:00.
Definition Date.h:166
void toCustomedFormattedString(const std::string &fmtStr, char *str, size_t len) const
Generate a UTC time string.
Definition Date.h:313
std::string toFormattedStringLocal(bool showMicroseconds) const
Generate a local time zone string, the format of the string is same as the method toFormattedString.
const Date roundSecond() const
Return a new Date instance that equals to *this, but with zero microseconds.
bool operator>=(const Date &date) const
Return true if the time point is not earlier than another.
Definition Date.h:147
std::string toCustomedFormattedString(const std::string &fmtStr, bool showMicroseconds=false) const
Generate a UTC time string formatted by the fmtStr.
Definition Date.h:210
bool operator>(const Date &date) const
Return true if the time point is later than another.
Definition Date.h:138
std::string toCustomedFormattedStringLocal(const std::string &fmtStr, bool showMicroseconds=false) const
Generate a local time zone string formatted by the fmtStr.
Definition Date.h:247
void swap(Date &that)
Swap the time point with another.
Definition Date.h:350
std::string toCustomFormattedStringLocal(const std::string &fmtStr, bool showMicroseconds=false) const
Generate a local time zone string formatted by the fmtStr.
static Date fromDbStringLocal(const std::string &datetime)
From DB string to trantor local time zone.
Date(int64_t microSec)
Construct a new Date instance.
Definition Date.h:37
struct tm tmStruct() const
Get the tm struct for the time point.
Date(unsigned int year, unsigned int month, unsigned int day, unsigned int hour=0, unsigned int minute=0, unsigned int second=0, unsigned int microSecond=0)
Construct a new Date instance.
const Date after(double second) const
Return a new Date instance that represents the time after some seconds from *this.
void toCustomFormattedString(const std::string &fmtStr, char *str, size_t len) const
Generate a UTC time string.
static const Date now()
Same as the date() method.
Definition Date.h:70
bool isSameSecond(const Date &date) const
Return true if the time point is in a same second as another.
Definition Date.h:339
std::string toCustomFormattedString(const std::string &fmtStr, bool showMicroseconds=false) const
Generate a UTC time string formatted by the fmtStr.
std::string toDbString() const
Generate a UTC time string for database.
std::string toDbStringLocal() const
Generate a local time zone string for database.
const Date roundDay() const
Return a new Date instance that equals to * this, but with zero hours, minutes, seconds and microseco...
static Date fromDbString(const std::string &datetime)
From DB string to trantor UTC time.
static Date fromISOString(const std::string &datetime)
Parse a datetime string in ISO-8601 format with some flexibility. Accepts the following formats:
bool operator!=(const Date &date) const
Return true if the time point is not equal to another.
Definition Date.h:120
bool operator<(const Date &date) const
Return true if the time point is earlier than another.
Definition Date.h:129
static const Date date()
Create a Date object that represents the current time.
bool operator==(const Date &date) const
Return true if the time point is equal to another.
Definition Date.h:111
int64_t secondsSinceEpoch() const
Get the number of seconds since 1970-01-01 00:00.
Definition Date.h:176
std::string toFormattedString(bool showMicroseconds) const
Generate a UTC time string.
Definition EventLoop.h:34