ASAP
1.0.0
A C++ header-only library for creating, displaying, iterating and manipulating dates
|
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... | |
datetime & | operator+= (const seconds &d) |
Adds a duration specified in seconds to a datetime. More... | |
datetime & | operator+= (const minutes &d) |
Adds a duration specified in minutes to a datetime. More... | |
datetime & | operator+= (const hours &d) |
Adds a duration specified in hours to a datetime. More... | |
datetime & | operator+= (const days &d) |
Adds a duration specified in days to a datetime. More... | |
datetime & | operator+= (const weeks &d) |
Adds a duration specified in weeks to a datetime. More... | |
datetime & | operator+= (const months &d) |
Adds a duration specified in months to a datetime. More... | |
datetime & | operator+= (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::datetime & | operator-= (const asap::duration< convert > &c) |
Subtracts a duration from this datetime. More... | |
asap::datetime & | operator+= (time_t stamp) |
Adds a duration specified in seconds since epoch to a datetime. More... | |
asap::datetime & | operator-= (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::datetime & | second (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::datetime & | minute (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::datetime & | hour (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::datetime & | wday (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::datetime & | mday (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::datetime & | yday (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::datetime & | month (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::datetime & | year (int value) |
Sets the year specified in this asap::duration. More... | |
The asap::datetime class exposes methods to create, manipulate and print dates.
It can be constructed from:
asap::datetime
asap::datetime(2007, 6, 1, 0, 0, 0))
for July 1 2007asap::datetime
has overloads for printing to an std::iostream
so you can directly print to std::cout
:
You can also format it via the asap::datetime::str
method
Definition at line 29 of file datetime.h.
|
explicitnoexcept |
Constructs a asap::datetime from a time_t
, defaulting to the current time.
time | The time specified as seconds since epoch (time_t ) |
This is also the default asap::datetime constructor
|
explicit |
Constructs a asap::datetime from a string, optionally with a format and a locale.
datetime | The datetime specified in a string |
format | The 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) |
locale | The 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:
One can argue this is the slowest method in the library.
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.
year | The year |
month | The 0-based month (e.g, 0 for January) |
day | The day of the month (not the day of the year!) |
hours | Hour of the day |
minutes | Minutes of the day |
seconds | Seconds of the day |
asap::datetime::afternoon | ( | ) |
Creates a new asap::datetime representing the afternoon of the same day.
asap::datetime::hour | ( | ) | const |
Returns the hours of a day specified in this asap::datetime.
asap::datetime::hour | ( | int | value | ) |
Sets the hour of a day specified in this asap::datetime.
value | The value to set |
*this
You can overflow this value and it will be converted correctly
asap::datetime::mday | ( | ) | const |
Returns the day of a month specified in this asap::datetime.
asap::datetime::mday | ( | int | value | ) |
Sets the day of a month specified in this asap::datetime (1-31)
value | The value to set |
*this
You can overflow this value and it will be converted correctly
asap::datetime::midnight | ( | ) |
Creates a new asap::datetime representing the midnight of the same day.
asap::datetime::minute | ( | ) | const |
Returns the minutes of an hour specified in this asap::datetime.
asap::datetime::minute | ( | int | value | ) |
Sets the minutes in an hour specified in this asap::datetime.
value | The value to set |
*this
You can overflow this value and it will be converted correctly
asap::datetime::month | ( | ) | const |
Returns the month in a year specified in this asap::datetime.
asap::datetime::month | ( | int | value | ) |
Sets the month in a year specified in this asap::datetime (0-11)
value | The value to set |
*this
You can overflow this value and it will be converted correctly
asap::datetime::morning | ( | ) |
Creates a new asap::datetime representing the morning of the same day.
asap::datetime::operator+= | ( | const seconds & | d | ) |
Adds a duration specified in seconds to a datetime.
d | The duration specified in seconds |
*this
asap::datetime::operator+= | ( | const minutes & | d | ) |
Adds a duration specified in minutes to a datetime.
d | The duration specified in minutes |
*this
asap::datetime::operator+= | ( | const hours & | d | ) |
Adds a duration specified in hours to a datetime.
d | The duration specified in hours |
*this
asap::datetime::operator+= | ( | const days & | d | ) |
Adds a duration specified in days to a datetime.
d | The duration specified in days |
*this
asap::datetime::operator+= | ( | const weeks & | d | ) |
Adds a duration specified in weeks to a datetime.
d | The duration specified in weeks |
*this
asap::datetime::operator+= | ( | const months & | d | ) |
Adds a duration specified in months to a datetime.
d | The duration specified in months |
*this
asap::datetime::operator+= | ( | const years & | d | ) |
Adds a duration specified in years to a datetime.
d | The duration specified in years |
*this
asap::datetime::operator+= | ( | time_t | stamp | ) |
Adds a duration specified in seconds since epoch to a datetime.
stamp | The duration specified in seconds since epoch |
*this
asap::datetime::operator- | ( | const datetime & | other | ) | const |
Returns a duration in seconds specifying the difference to another datetime.
other | The datetime to calculate the difference |
other
asap::datetime::operator-= | ( | const asap::duration< convert > & | c | ) |
Subtracts a duration from this datetime.
convert | The converting factor for the duration (automatically deduced) |
c | The duration |
*this
asap::datetime::operator-= | ( | time_t | stamp | ) |
Subtracts a duration specified in seconds since epoch from a datetime.
stamp | The duration specified in seconds since epoch |
*this
asap::datetime::second | ( | ) | const |
Returns the seconds of a minute specified in this asap::datetime.
asap::datetime::second | ( | int | value | ) |
Sets the seconds in a minute specified in this asap::datetime.
value | The value to set |
*this
You can overflow this value and it will be converted correctly
asap::datetime::str | ( | const std::string & | fmt = "%x %X" | ) | const |
Converts to an string using the format specified.
fmt | The format to use when converting |
asap::datetime::timestamp | ( | ) | const |
Converts an asap::datetime to a time_t
time_t
asap::datetime::until | ( | const asap::datetime & | dt | ) | const |
Creates an instance of asap::period starting at this datetime and ending at dt.
dt | Ending datetime |
asap::datetime::wday | ( | ) | const |
Returns the day of a week specified in this asap::datetime.
asap::datetime::wday | ( | int | value | ) |
Sets the day of a week specified in this asap::datetime (0-6)
value | The value to set |
*this
You can overflow this value and it will be converted correctly
asap::datetime::yday | ( | ) | const |
Returns the day in a year specified in this asap::datetime.
asap::datetime::yday | ( | int | value | ) |
Sets the day in a year specified in this asap::datetime (1-365)
value | The value to set |
*this
You can overflow this value and it will be converted correctly
asap::datetime::year | ( | ) | const |
Returns the year specified in this asap::datetime.
asap::datetime::year | ( | int | value | ) |