common_util
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
cutl::datetime Class Reference

A simple, feature-rich modern C++ date-time class. More...

#include <datetime.h>

Public Member Functions

 datetime (uint64_t ms)
 Construct a new datetime object.
 
 datetime (const datetime &other)
 Construct a new datetime object.
 
 ~datetime ()
 Destroy the datetime object.
 
uint64_t timestamp () const
 Get the timestamp in milliseconds.
 
std::string format (datetime_format dfmt=datetime_format::datetime_format_a, bool local=true, bool show_milliseconds=true) const
 format the datetime object to a string
 
std::string format (const std::string &fmt, bool local=true, bool show_milliseconds=true) const
 Format the datetime object to a string with a custom format string.
 
std::string utctime () const
 Get the string of UTC time described by datetime object.
 
datetimeoperator= (const datetime &other)
 Define the assignment operator.
 
datetime operator+ (uint64_t ms)
 Define the addition operator.
 
datetime operator- (uint64_t ms)
 Define the subtraction operator.
 
datetimeoperator+= (uint64_t ms)
 Define the addition and assignment operator.
 
datetimeoperator-= (uint64_t ms)
 Define the subtraction and assignment operator.
 
int64_t operator- (const datetime &other) const
 Define the subtraction operator between two datetime objects.
 

Static Public Member Functions

static datetime now ()
 Get a datetime object for the current system time.
 
static datetime get (const std::string &time_text, int isdst=-1)
 Constructs a datetime object from a local time string.
 

Static Public Attributes

static constexpr int second = 1000
 Constants value: second, expressed in milliseconds.
 
static constexpr int min = 60 * second
 Constants value: min, expressed in milliseconds.
 
static constexpr int hour = 60 * min
 Constants value: hour, expressed in milliseconds.
 
static constexpr int day = 24 * hour
 Constants value: day, expressed in milliseconds.
 

Detailed Description

A simple, feature-rich modern C++ date-time class.

Constructor & Destructor Documentation

◆ datetime() [1/2]

cutl::datetime::datetime ( uint64_t ms)

Construct a new datetime object.

Parameters
msa timestamp in milliseconds for initialize the datetime object

◆ datetime() [2/2]

cutl::datetime::datetime ( const datetime & other)

Construct a new datetime object.

Parameters
otherother datetime object to copy

Member Function Documentation

◆ format() [1/2]

std::string cutl::datetime::format ( const std::string & fmt,
bool local = true,
bool show_milliseconds = true ) const

Format the datetime object to a string with a custom format string.

Parameters
fmtdatetime format string, default is "%Y-%m-%d %H:%M:%S.%f". usages like std::put_time, reference to https://en.cppreference.com/w/cpp/io/manip/put_time
localwhether to use local time, default is true. if true means output in local time, otherwise, output in UTC time.
show_millisecondswhether to show milliseconds, default is true. if true means show milliseconds, otherwise, not show milliseconds.
Returns
formatted datetime string described by std::string

◆ format() [2/2]

std::string cutl::datetime::format ( datetime_format dfmt = datetime_format::datetime_format_a,
bool local = true,
bool show_milliseconds = true ) const

format the datetime object to a string

Parameters
dfmtdatetime format, a value of datetime_format enum, default is datetime_format_a
localwhether to use local time, default is true. if true means output in local time, otherwise, output in UTC time.
show_millisecondswhether to show milliseconds, default is true. if true means show milliseconds, otherwise, not show milliseconds.
Returns
formatted datetime string described by std::string

◆ get()

static datetime cutl::datetime::get ( const std::string & time_text,
int isdst = -1 )
static

Constructs a datetime object from a local time string.

Only the following time formats are supported:

  • YYYY-MM-DD HH:MM:SS.sss
  • YYYY-MM-DD HH:MM:SS
  • YYYY.MM.DD HH:MM:SS.sss
  • YYYY.MM.DD HH:MM:SS
  • YYYY/MM/DD HH:MM:SS.sss
  • YYYY/MM/DD HH:MM:SS
  • YYYYMMDD HH:MM:SS.sss
  • YYYYMMDD HH:MM:SS
    Parameters
    time_textlocal time string, use the below formats to construct a datetime object.
    isdstthe setting of daylight saving time, -1 means system automatically determine, 0 means not in daylight saving time, 1 means in daylight saving time
    Returns
    datetime object constructed from the local time string

◆ now()

static datetime cutl::datetime::now ( )
static

Get a datetime object for the current system time.

Returns
datetime object for the current system time

◆ operator+()

datetime cutl::datetime::operator+ ( uint64_t ms)

Define the addition operator.

Parameters
msmilliseconds to add
Returns
datetime object after adding milliseconds

◆ operator+=()

datetime & cutl::datetime::operator+= ( uint64_t ms)

Define the addition and assignment operator.

Parameters
msmilliseconds to add
Returns
datetime& the reference of the current datetime object after adding milliseconds

◆ operator-() [1/2]

int64_t cutl::datetime::operator- ( const datetime & other) const

Define the subtraction operator between two datetime objects.

Parameters
otherdatetime object to subtract
Returns
the duration in milliseconds between current and other datetime objects

◆ operator-() [2/2]

datetime cutl::datetime::operator- ( uint64_t ms)

Define the subtraction operator.

Parameters
msmilliseconds to subtract
Returns
datetime object after subtracting milliseconds

◆ operator-=()

datetime & cutl::datetime::operator-= ( uint64_t ms)

Define the subtraction and assignment operator.

Parameters
msmilliseconds to subtract
Returns
datetime& the reference of the current datetime object after subtracting milliseconds

◆ operator=()

datetime & cutl::datetime::operator= ( const datetime & other)

Define the assignment operator.

Parameters
otherother datetime object to copy
Returns
datetime& the reference of the current datetime object

◆ timestamp()

uint64_t cutl::datetime::timestamp ( ) const

Get the timestamp in milliseconds.

Returns
the timestamp in milliseconds

◆ utctime()

std::string cutl::datetime::utctime ( ) const
inline

Get the string of UTC time described by datetime object.

Returns
the string of UTC time in format "YYYY-MM-DD HH:MM:SS.sss"

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