ASAP  1.0.0
A C++ header-only library for creating, displaying, iterating and manipulating dates
Public Member Functions | List of all members
asap::datetime Class Reference

The asap::datetime class exposes methods to create, manipulate and print dates. More...

Public Member Functions

 datetime (time_t time=std::time(nullptr)) noexcept
 Constructs a asap::datetime from a time_t, defaulting to the current time. More...
 
 datetime (const std::string &datetime, const std::string &format="%x %X", const std::string &locale="")
 Constructs a asap::datetime from a string, optionally with a format and a locale. More...
 
 datetime (uint32_t year, uint32_t month, uint32_t day, uint32_t hours=0, uint32_t minutes=0, uint32_t seconds=0)
 Constructs a asap::datetime from a broken down time. More...
 
datetimeoperator+= (const seconds &d)
 Adds a duration specified in seconds to a datetime. More...
 
datetimeoperator+= (const minutes &d)
 Adds a duration specified in minutes to a datetime. More...
 
datetimeoperator+= (const hours &d)
 Adds a duration specified in hours to a datetime. More...
 
datetimeoperator+= (const days &d)
 Adds a duration specified in days to a datetime. More...
 
datetimeoperator+= (const weeks &d)
 Adds a duration specified in weeks to a datetime. More...
 
datetimeoperator+= (const months &d)
 Adds a duration specified in months to a datetime. More...
 
datetimeoperator+= (const years &d)
 Adds a duration specified in years to a datetime. More...
 
asap::seconds operator- (const datetime &other) const
 Returns a duration in seconds specifying the difference to another datetime. More...
 
template<uint64_t convert>
asap::datetimeoperator-= (const asap::duration< convert > &c)
 Subtracts a duration from this datetime. More...
 
asap::datetimeoperator+= (time_t stamp)
 Adds a duration specified in seconds since epoch to a datetime. More...
 
asap::datetimeoperator-= (time_t stamp)
 Subtracts a duration specified in seconds since epoch from a datetime. More...
 
asap::datetime morning ()
 Creates a new asap::datetime representing the morning of the same day. More...
 
asap::datetime afternoon ()
 Creates a new asap::datetime representing the afternoon of the same day. More...
 
asap::datetime midnight ()
 Creates a new asap::datetime representing the midnight of the same day. More...
 
time_t timestamp () const
 Converts an asap::datetime to a time_t More...
 
std::string str (const std::string &fmt="%x %X") const
 Converts to an string using the format specified. More...
 
asap::period until (const asap::datetime &dt) const
 Creates an instance of asap::period starting at this datetime and ending at dt. More...
 
int second () const
 Returns the seconds of a minute specified in this asap::datetime. More...
 
asap::datetimesecond (int value)
 Sets the seconds in a minute specified in this asap::datetime. More...
 
int minute () const
 Returns the minutes of an hour specified in this asap::datetime. More...
 
asap::datetimeminute (int value)
 Sets the minutes in an hour specified in this asap::datetime. More...
 
int hour () const
 Returns the hours of a day specified in this asap::datetime. More...
 
asap::datetimehour (int value)
 Sets the hour of a day specified in this asap::datetime. More...
 
int wday () const
 Returns the day of a week specified in this asap::datetime. More...
 
asap::datetimewday (int value)
 Sets the day of a week specified in this asap::datetime (0-6) More...
 
int mday () const
 Returns the day of a month specified in this asap::datetime. More...
 
asap::datetimemday (int value)
 Sets the day of a month specified in this asap::datetime (1-31) More...
 
int yday () const
 Returns the day in a year specified in this asap::datetime. More...
 
asap::datetimeyday (int value)
 Sets the day in a year specified in this asap::datetime (1-365) More...
 
int month () const
 Returns the month in a year specified in this asap::datetime. More...
 
asap::datetimemonth (int value)
 Sets the month in a year specified in this asap::datetime (0-11) More...
 
int year () const
 Returns the year specified in this asap::datetime. More...
 
asap::datetimeyear (int value)
 Sets the year specified in this asap::duration. More...
 

Detailed Description

The asap::datetime class exposes methods to create, manipulate and print dates.

It can be constructed from:

asap::datetime has overloads for printing to an std::iostream so you can directly print to std::cout:

std::cout << asap::now() << std::endl;

You can also format it via the asap::datetime::str method

std::cout << asap::now().str("%c") << std::endl;

Definition at line 29 of file datetime.h.

Constructor & Destructor Documentation

◆ datetime() [1/3]

asap::datetime::datetime ( time_t  time = std::time(nullptr))
explicitnoexcept

Constructs a asap::datetime from a time_t, defaulting to the current time.

Parameters
timeThe time specified as seconds since epoch (time_t)

This is also the default asap::datetime constructor

◆ datetime() [2/3]

asap::datetime::datetime ( const std::string &  datetime,
const std::string &  format = "%x %X",
const std::string &  locale = "" 
)
explicit

Constructs a asap::datetime from a string, optionally with a format and a locale.

Parameters
datetimeThe datetime specified in a string
formatThe datetime format, defaults to "%x %X" which is equivalent to "%d/%m/%Y %H:%M:%S" on some locales and "m/d/Y H:M:S in en_US locale)
localeThe locale to specify when reading the string

This constructor tries its best to read the date and time specified in the string. This is what it tries:

  • Read the string in the specified format and locale
  • Convert the datetime back to a string using the same format and locale
  • If the strings are equal, it stops
  • If not it tries the next known format
  • If nothing works, defaults to 0 seconds since epoch.

One can argue this is the slowest method in the library.

◆ datetime() [3/3]

asap::datetime::datetime ( uint32_t  year,
uint32_t  month,
uint32_t  day,
uint32_t  hours = 0,
uint32_t  minutes = 0,
uint32_t  seconds = 0 
)

Constructs a asap::datetime from a broken down time.

Parameters
yearThe year
monthThe 0-based month (e.g, 0 for January)
dayThe day of the month (not the day of the year!)
hoursHour of the day
minutesMinutes of the day
secondsSeconds of the day

Member Function Documentation

◆ afternoon()

asap::datetime::afternoon ( )

Creates a new asap::datetime representing the afternoon of the same day.

Returns
a new asap::datetime at 12 pm in the same day (which usually is considered afternoon around the world)

◆ hour() [1/2]

asap::datetime::hour ( ) const

Returns the hours of a day specified in this asap::datetime.

Returns
hours of a day of this asap::datetime

◆ hour() [2/2]

asap::datetime::hour ( int  value)

Sets the hour of a day specified in this asap::datetime.

Parameters
valueThe value to set
Returns
*this

You can overflow this value and it will be converted correctly

◆ mday() [1/2]

asap::datetime::mday ( ) const

Returns the day of a month specified in this asap::datetime.

Returns
day of a month of this asap::datetime

◆ mday() [2/2]

asap::datetime::mday ( int  value)

Sets the day of a month specified in this asap::datetime (1-31)

Parameters
valueThe value to set
Returns
*this

You can overflow this value and it will be converted correctly

◆ midnight()

asap::datetime::midnight ( )

Creates a new asap::datetime representing the midnight of the same day.

Returns
a new asap::datetime at 0 am in the same day

◆ minute() [1/2]

asap::datetime::minute ( ) const

Returns the minutes of an hour specified in this asap::datetime.

Returns
minutes of an hour

◆ minute() [2/2]

asap::datetime::minute ( int  value)

Sets the minutes in an hour specified in this asap::datetime.

Parameters
valueThe value to set
Returns
*this

You can overflow this value and it will be converted correctly

◆ month() [1/2]

asap::datetime::month ( ) const

Returns the month in a year specified in this asap::datetime.

Returns
month in a year

◆ month() [2/2]

asap::datetime::month ( int  value)

Sets the month in a year specified in this asap::datetime (0-11)

Parameters
valueThe value to set
Returns
*this

You can overflow this value and it will be converted correctly

◆ morning()

asap::datetime::morning ( )

Creates a new asap::datetime representing the morning of the same day.

Returns
a new asap::datetime at 6 am in the same day (which usually is considered morning around the world)

◆ operator+=() [1/8]

asap::datetime::operator+= ( const seconds d)

Adds a duration specified in seconds to a datetime.

Parameters
dThe duration specified in seconds
Returns
*this

◆ operator+=() [2/8]

asap::datetime::operator+= ( const minutes d)

Adds a duration specified in minutes to a datetime.

Parameters
dThe duration specified in minutes
Returns
*this

◆ operator+=() [3/8]

asap::datetime::operator+= ( const hours d)

Adds a duration specified in hours to a datetime.

Parameters
dThe duration specified in hours
Returns
*this

◆ operator+=() [4/8]

asap::datetime::operator+= ( const days d)

Adds a duration specified in days to a datetime.

Parameters
dThe duration specified in days
Returns
*this

◆ operator+=() [5/8]

asap::datetime::operator+= ( const weeks d)

Adds a duration specified in weeks to a datetime.

Parameters
dThe duration specified in weeks
Returns
*this

◆ operator+=() [6/8]

asap::datetime::operator+= ( const months d)

Adds a duration specified in months to a datetime.

Parameters
dThe duration specified in months
Returns
*this

◆ operator+=() [7/8]

asap::datetime::operator+= ( const years d)

Adds a duration specified in years to a datetime.

Parameters
dThe duration specified in years
Returns
*this

◆ operator+=() [8/8]

asap::datetime::operator+= ( time_t  stamp)

Adds a duration specified in seconds since epoch to a datetime.

Parameters
stampThe duration specified in seconds since epoch
Returns
*this

◆ operator-()

asap::datetime::operator- ( const datetime other) const

Returns a duration in seconds specifying the difference to another datetime.

Parameters
otherThe datetime to calculate the difference
Returns
an instance of asap::seconds representing the difference to other

◆ operator-=() [1/2]

template<uint64_t convert>
asap::datetime::operator-= ( const asap::duration< convert > &  c)

Subtracts a duration from this datetime.

Template Parameters
convertThe converting factor for the duration (automatically deduced)
Parameters
cThe duration
Returns
*this

◆ operator-=() [2/2]

asap::datetime::operator-= ( time_t  stamp)

Subtracts a duration specified in seconds since epoch from a datetime.

Parameters
stampThe duration specified in seconds since epoch
Returns
*this

◆ second() [1/2]

asap::datetime::second ( ) const

Returns the seconds of a minute specified in this asap::datetime.

Returns
seconds of a minute

◆ second() [2/2]

asap::datetime::second ( int  value)

Sets the seconds in a minute specified in this asap::datetime.

Parameters
valueThe value to set
Returns
*this

You can overflow this value and it will be converted correctly

◆ str()

asap::datetime::str ( const std::string &  fmt = "%x %X") const

Converts to an string using the format specified.

Parameters
fmtThe format to use when converting
Returns
A std::string with the resulting conversion

◆ timestamp()

asap::datetime::timestamp ( ) const

Converts an asap::datetime to a time_t

Returns
the timestamp of this asap::datetime in time_t

◆ until()

asap::datetime::until ( const asap::datetime dt) const

Creates an instance of asap::period starting at this datetime and ending at dt.

Parameters
dtEnding datetime
Returns
An instance of asap::period

◆ wday() [1/2]

asap::datetime::wday ( ) const

Returns the day of a week specified in this asap::datetime.

Returns
day of a week of this asap::datetime

◆ wday() [2/2]

asap::datetime::wday ( int  value)

Sets the day of a week specified in this asap::datetime (0-6)

Parameters
valueThe value to set
Returns
*this

You can overflow this value and it will be converted correctly

◆ yday() [1/2]

asap::datetime::yday ( ) const

Returns the day in a year specified in this asap::datetime.

Returns
day in a year

◆ yday() [2/2]

asap::datetime::yday ( int  value)

Sets the day in a year specified in this asap::datetime (1-365)

Parameters
valueThe value to set
Returns
*this

You can overflow this value and it will be converted correctly

◆ year() [1/2]

asap::datetime::year ( ) const

Returns the year specified in this asap::datetime.

Returns
year of this asap::datetime

◆ year() [2/2]

asap::datetime::year ( int  value)

Sets the year specified in this asap::duration.

Parameters
valueThe value to set
Returns
*this