trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
trantor::Date Class Reference

This class represents a time point. More...

#include <trantor/utils/Date.h>

Public Member Functions

 Date (int64_t microSec)
 Construct a new Date instance.
 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.
const Date roundSecond () const
 Return a new Date instance that equals to *this, but with zero microseconds.
const Date roundDay () const
 Return a new Date instance that equals to * this, but with zero hours, minutes, seconds and microseconds.
bool operator== (const Date &date) const
 Return true if the time point is equal to another.
bool operator!= (const Date &date) const
 Return true if the time point is not equal to another.
bool operator< (const Date &date) const
 Return true if the time point is earlier than another.
bool operator> (const Date &date) const
 Return true if the time point is later than another.
bool operator>= (const Date &date) const
 Return true if the time point is not earlier than another.
bool operator<= (const Date &date) const
 Return true if the time point is not later than another.
int64_t microSecondsSinceEpoch () const
 Get the number of milliseconds since 1970-01-01 00:00.
int64_t secondsSinceEpoch () const
 Get the number of seconds since 1970-01-01 00:00.
struct tm tmStruct () const
 Get the tm struct for the time point.
std::string toFormattedString (bool showMicroseconds) const
 Generate a UTC time string.
std::string toCustomedFormattedString (const std::string &fmtStr, bool showMicroseconds=false) const
 Generate a UTC time string formatted by the fmtStr.
std::string toCustomFormattedString (const std::string &fmtStr, bool showMicroseconds=false) const
 Generate a UTC time string formatted by the fmtStr.
std::string toFormattedStringLocal (bool showMicroseconds) const
 Generate a local time zone string, the format of the string is same as the method toFormattedString.
std::string toCustomedFormattedStringLocal (const std::string &fmtStr, bool showMicroseconds=false) const
 Generate a local time zone string formatted by the fmtStr.
std::string toCustomFormattedStringLocal (const std::string &fmtStr, bool showMicroseconds=false) const
 Generate a local time zone string formatted by the fmtStr.
std::string toDbStringLocal () const
 Generate a local time zone string for database.
std::string toDbString () const
 Generate a UTC time string for database.
void toCustomedFormattedString (const std::string &fmtStr, char *str, size_t len) const
 Generate a UTC time string.
void toCustomFormattedString (const std::string &fmtStr, char *str, size_t len) const
 Generate a UTC time string.
bool isSameSecond (const Date &date) const
 Return true if the time point is in a same second as another.
void swap (Date &that)
 Swap the time point with another.

Static Public Member Functions

static const Date date ()
 Create a Date object that represents the current time.
static const Date now ()
 Same as the date() method.
static int64_t timezoneOffset ()
static Date fromDbStringLocal (const std::string &datetime)
 From DB string to trantor local time zone.
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:

Static Public Attributes

static constexpr long MICRO_SECONDS_PER_SEC = 1000000LL

Detailed Description

This class represents a time point.

Constructor & Destructor Documentation

◆ Date() [1/2]

trantor::Date::Date ( int64_t microSec)
inlineexplicit

Construct a new Date instance.

Parameters
microSecThe microseconds from 1970-01-01 00:00:00.

◆ Date() [2/2]

trantor::Date::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.

Parameters
year
month
day
hour
minute
second
microSecond

Member Function Documentation

◆ after()

const Date trantor::Date::after ( double second) const

Return a new Date instance that represents the time after some seconds from *this.

Parameters
second
Returns
const Date

◆ date()

const Date trantor::Date::date ( )
static

Create a Date object that represents the current time.

Returns
const Date

◆ fromDbString()

Date trantor::Date::fromDbString ( const std::string & datetime)
static

From DB string to trantor UTC time.

Inverse of toDbString()

◆ fromDbStringLocal()

Date trantor::Date::fromDbStringLocal ( const std::string & datetime)
static

From DB string to trantor local time zone.

Inverse of toDbStringLocal()

◆ fromISOString()

Date trantor::Date::fromISOString ( const std::string & datetime)
static

Parse a datetime string in ISO-8601 format with some flexibility. Accepts the following formats:

  • yyyy-mm-dd
  • yyyy-mm-dd[ T]HH:MM[:SS[.ffffff]]
  • yyyy-mm-dd[ T]HH:MM[:SS[.ffffff]][timezone] where [timezone] can be one of:
    • Z
    • +HH:MM, -HH:MM, HH:MM (unsigned treated as positive)
    • +HHMM, -HHMM, HHMM (unsigned treated as positive)

◆ isSameSecond()

bool trantor::Date::isSameSecond ( const Date & date) const
inline

Return true if the time point is in a same second as another.

Parameters
date
Returns
true
false

◆ microSecondsSinceEpoch()

int64_t trantor::Date::microSecondsSinceEpoch ( ) const
inline

Get the number of milliseconds since 1970-01-01 00:00.

Returns
int64_t

◆ now()

const Date trantor::Date::now ( )
inlinestatic

Same as the date() method.

Returns
const Date

◆ roundDay()

const Date trantor::Date::roundDay ( ) const

Return a new Date instance that equals to * this, but with zero hours, minutes, seconds and microseconds.

Create a Date object equal to * this, but numbers of hours, minutes, seconds and microseconds are zero.

Returns
const Date

◆ roundSecond()

const Date trantor::Date::roundSecond ( ) const

Return a new Date instance that equals to *this, but with zero microseconds.

Returns
const Date

◆ secondsSinceEpoch()

int64_t trantor::Date::secondsSinceEpoch ( ) const
inline

Get the number of seconds since 1970-01-01 00:00.

Returns
int64_t

◆ swap()

void trantor::Date::swap ( Date & that)
inline

Swap the time point with another.

Parameters
that

◆ tmStruct()

struct tm trantor::Date::tmStruct ( ) const

Get the tm struct for the time point.

Returns
struct tm

◆ toCustomedFormattedString() [1/2]

std::string trantor::Date::toCustomedFormattedString ( const std::string & fmtStr,
bool showMicroseconds = false ) const
inline

Generate a UTC time string formatted by the fmtStr.

Parameters
fmtStris the format string for the function strftime()
showMicrosecondswhether the microseconds are returned.
Note
Examples:
  • "2018-01-01 10:10:25" if the fmtStr is "%Y-%m-%d %H:%M:%S" and the showMicroseconds is false
  • "2018-01-01 10:10:25:102414" if the fmtStr is "%Y-%m-%d %H:%M:%S" and the showMicroseconds is true
Deprecated
Replaced by toCustomFormattedString

◆ toCustomedFormattedString() [2/2]

void trantor::Date::toCustomedFormattedString ( const std::string & fmtStr,
char * str,
size_t len ) const
inline

Generate a UTC time string.

Parameters
fmtStrThe format string.
strThe string buffer for the generated time string.
lenThe length of the string buffer.
Deprecated
Replaced by toCustomFormattedString

◆ toCustomedFormattedStringLocal()

std::string trantor::Date::toCustomedFormattedStringLocal ( const std::string & fmtStr,
bool showMicroseconds = false ) const
inline

Generate a local time zone string formatted by the fmtStr.

Parameters
fmtStr
showMicroseconds
Returns
std::string
Deprecated
Replaced by toCustomFormattedString

◆ toCustomFormattedString() [1/2]

std::string trantor::Date::toCustomFormattedString ( const std::string & fmtStr,
bool showMicroseconds = false ) const

Generate a UTC time string formatted by the fmtStr.

Parameters
fmtStris the format string for the function strftime()
showMicrosecondswhether the microseconds are returned.
Note
Examples:
  • "2018-01-01 10:10:25" if the fmtStr is "%Y-%m-%d %H:%M:%S" and the showMicroseconds is false
  • "2018-01-01 10:10:25:102414" if the fmtStr is "%Y-%m-%d %H:%M:%S" and the showMicroseconds is true

◆ toCustomFormattedString() [2/2]

void trantor::Date::toCustomFormattedString ( const std::string & fmtStr,
char * str,
size_t len ) const

Generate a UTC time string.

Parameters
fmtStrThe format string.
strThe string buffer for the generated time string.
lenThe length of the string buffer.

◆ toCustomFormattedStringLocal()

std::string trantor::Date::toCustomFormattedStringLocal ( const std::string & fmtStr,
bool showMicroseconds = false ) const

Generate a local time zone string formatted by the fmtStr.

Parameters
fmtStr
showMicroseconds
Returns
std::string

◆ toDbStringLocal()

std::string trantor::Date::toDbStringLocal ( ) const

Generate a local time zone string for database.

Note
Examples:
  • "2018-01-01" if hours, minutes, seconds and microseconds are zero
  • "2018-01-01 10:10:25" if the microsecond is zero
  • "2018-01-01 10:10:25:102414" if the microsecond is not zero

◆ toFormattedString()

std::string trantor::Date::toFormattedString ( bool showMicroseconds) const

Generate a UTC time string.

Parameters
showMicrosecondswhether the microseconds are returned.
Note
Examples:
  • "20180101 10:10:25" if the showMicroseconds is false
  • "20180101 10:10:25:102414" if the showMicroseconds is true

◆ toFormattedStringLocal()

std::string trantor::Date::toFormattedStringLocal ( bool showMicroseconds) const

Generate a local time zone string, the format of the string is same as the method toFormattedString.

Parameters
showMicroseconds
Returns
std::string

The documentation for this class was generated from the following file: