Skip to content

Commit 5345dad

Browse files
authored
Add utc methods for trantor::Date. (#218)
1 parent 582a719 commit 5345dad

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

trantor/utils/Date.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ std::string Date::toDbStringLocal() const
274274
}
275275
return buf;
276276
}
277+
std::string Date::toDbString() const
278+
{
279+
return after(static_cast<double>(-timezoneOffset())).toDbStringLocal();
280+
}
281+
277282
Date Date::fromDbStringLocal(const std::string &datetime)
278283
{
279284
unsigned int year = {0}, month = {0}, day = {0}, hour = {0}, minute = {0},
@@ -312,6 +317,12 @@ Date Date::fromDbStringLocal(const std::string &datetime)
312317
}
313318
return trantor::Date(year, month, day, hour, minute, second, microSecond);
314319
}
320+
Date Date::fromDbString(const std::string &datetime)
321+
{
322+
return fromDbStringLocal(datetime).after(
323+
static_cast<double>(timezoneOffset()));
324+
}
325+
315326
std::string Date::toCustomedFormattedStringLocal(const std::string &fmtStr,
316327
bool showMicroseconds) const
317328
{

trantor/utils/Date.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class TRANTOR_EXPORT Date
7474
return Date::date();
7575
}
7676

77+
static int64_t timezoneOffset()
78+
{
79+
static int64_t offset =
80+
0 - Date::fromDbStringLocal("1970-01-01").secondsSinceEpoch();
81+
return offset;
82+
}
83+
7784
/**
7885
* @brief Return a new Date instance that represents the time after some
7986
* seconds from *this.
@@ -233,13 +240,23 @@ class TRANTOR_EXPORT Date
233240
* is zero 2018-01-01 10:10:25:102414 //If the microsecond is not zero
234241
*/
235242
std::string toDbStringLocal() const;
243+
/**
244+
* @brief Generate a UTC time string for database.
245+
*/
246+
std::string toDbString() const;
236247

237248
/**
238249
* @brief From DB string to trantor local time zone.
239250
*
240251
* Inverse of toDbStringLocal()
241252
*/
242253
static Date fromDbStringLocal(const std::string &datetime);
254+
/**
255+
* @brief From DB string to trantor UTC time.
256+
*
257+
* Inverse of toDbString()
258+
*/
259+
static Date fromDbString(const std::string &datetime);
243260

244261
/**
245262
* @brief Generate a UTC time string.

0 commit comments

Comments
 (0)