17#include <trantor/exports.h>
27class TRANTOR_EXPORT Date
30 Date() : microSecondsSinceEpoch_(0){};
37 explicit Date(int64_t microSec) : microSecondsSinceEpoch_(microSec){};
53 unsigned int hour = 0,
54 unsigned int minute = 0,
55 unsigned int second = 0,
56 unsigned int microSecond = 0);
70 static const Date
now()
75 static int64_t timezoneOffset();
84 const Date
after(
double second)
const;
113 return microSecondsSinceEpoch_ ==
date.microSecondsSinceEpoch_;
122 return microSecondsSinceEpoch_ !=
date.microSecondsSinceEpoch_;
131 return microSecondsSinceEpoch_ <
date.microSecondsSinceEpoch_;
140 return microSecondsSinceEpoch_ >
date.microSecondsSinceEpoch_;
149 return microSecondsSinceEpoch_ >=
date.microSecondsSinceEpoch_;
158 return microSecondsSinceEpoch_ <=
date.microSecondsSinceEpoch_;
168 return microSecondsSinceEpoch_;
178 return microSecondsSinceEpoch_ / MICRO_SECONDS_PER_SEC;
209 [[deprecated(
"Replaced by toCustomFormattedString")]]
211 bool showMicroseconds =
false)
const
227 bool showMicroseconds =
false)
const;
246 [[deprecated(
"Replaced by toCustomFormattedStringLocal")]]
248 bool showMicroseconds =
false)
const
261 const std::string &fmtStr,
262 bool showMicroseconds =
false)
const;
312 [[deprecated(
"Replaced by toCustomFormattedString")]]
341 return microSecondsSinceEpoch_ / MICRO_SECONDS_PER_SEC ==
342 date.microSecondsSinceEpoch_ / MICRO_SECONDS_PER_SEC;
352 std::swap(microSecondsSinceEpoch_, that.microSecondsSinceEpoch_);
355 static constexpr long MICRO_SECONDS_PER_SEC = 1000000LL;
358 int64_t microSecondsSinceEpoch_{0};
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