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

The asap::duration class exposes methods to create, manipulate and print durations. More...

Public Member Functions

 duration (double v=0)
 Constructs a duration with the value set to v. More...
 
template<uint64_t convertfrom>
 duration (const asap::duration< convertfrom > &other)
 Converts the value of other to construct an asap::duration More...
 
 operator double () const
 Converts an asap::duration to double in the same 2456convertion factor specified (e.g, 0.5 days, if using asap::days)
 
duration< convert > & operator= (double v)
 Sets the internal value to v. More...
 
double operator* () const
 the same as asap::duration::operator double() More...
 
duration< convert > operator- ()
 Changes the sign of a duration. More...
 
template<uint64_t other>
 operator duration< other > () const
 Converts an duration to another duration factor (e.g, convert an asap::day to an asap::year) More...
 
template<uint64_t convert2>
duration< convert > & operator+= (const duration< convert2 > &other)
 Adds another duration to this duration, converting to this convertion factor (e.g, 1_day + 1_week == 1_day + 7_days) More...
 
duration< convert > & operator+= (const duration< convert > &other)
 Adds another duration to this duration with the same convertion factor. More...
 
template<uint64_t convert2>
duration< convert > & operator-= (const duration< convert2 > &other)
 Subtracts another duration from this duration, converting to this convertion factor (e.g, 1_day -= 1_week == 1_day -= 7_days) More...
 
template<uint64_t convert2>
duration< convert > & operator= (const duration< convert2 > &other)
 Sets this duration as equals to another duration, converting to this convertion factor (e.g, asap::days d = 1_week == asap::days d = 7_days) More...
 
std::string str () const
 Pretty print this duration, breaking down all the units (e.g: std::cout << 10_days would print 1 week, 3 days) More...
 

Detailed Description

template<uint64_t convert = 1>
class asap::duration< convert >

The asap::duration class exposes methods to create, manipulate and print durations.

Template Parameters
convertThe multiplier in seconds for this duration

A duration is an unbounded period of time. It does not have a start datetime nor an end. They can be obtained via the following helper classes:

All of them have singular equivalents (e.g, asap::minute) for convenience. They can also be constructed from literals available in the asap::literals namespace. Consult the asap::literals documentation for more about ASAP literals.

Note that they all work with floating point values, so you can specify fractions of a duration:

// specify half a day (which is equivalent to 12 hours):
auto halfday = asap::days(0.5f);
// note that half an year is not equivalent to 6 months, mainly because
// for duration, a month has 30 days.
bool equiv = asap::years(0.5f) == asap::months(6); //false

You can add and subtract durations from each other. The right-hand operand will be converted to the same "*base*" as the left-hand operator (e.g, if you add asap::seconds to asap::year, the year will be converted to seconds and then added). For this reason is not advisable to add periods with large different convertion factors between the left and the right operands. It is ok to add a year to a second, but seconds added to a year can have rounding issues as it will be converted to a fraction of a year.

std::cout << asap::day(1) + asap::month(1) << std::endl
// "1 month, 1 day"
std::cout << asap::year(1) + asap::second(1) << std::endl
// "1 year", second is lost due to rounding

Subtracting an asap::datetime from another will give you an asap::duration:

std::cout << asap::tomorrow() - asap::now() << std::endl;
// 9 hours, 27 minutes, 11 seconds

You can also add or subtract durations from asap::datetime objects:

std::cout << asap::now() << std::endl; // -> 06-01-2018 14:35:39
std::cout << asap::now() + asap::days(1) << std::endl; // -> 07-01-2018 14:35:39

Literals

The file literals.h has many user-defined literals to provide eye-candy to your code when construction durations:

You have to explicitly include these literals by adding the following in your code that is using ASAP:

using namespace asap::literals

These literals can be used as a shorthand for their class counterpart:

// instead of
std::cout << asap::now() + asap::days(2) - asap::hours(1) << std::endl;
// you can write
std::cout << asap::now() + 2_days - 1_hour << std::endl;

Definition at line 28 of file duration.h.

Constructor & Destructor Documentation

◆ duration() [1/2]

template<uint64_t convert = 1>
asap::duration< convert >::duration ( double  v = 0)
explicit

Constructs a duration with the value set to v.

Parameters
vInitial value

◆ duration() [2/2]

template<uint64_t convert = 1>
template<uint64_t convertfrom>
asap::duration< convert >::duration ( const asap::duration< convertfrom > &  other)

Converts the value of other to construct an asap::duration

Template Parameters
convertfromThe other convertion factor
Parameters
otherthe other duration to read from 3

Member Function Documentation

◆ operator duration< other >()

template<uint64_t convert = 1>
template<uint64_t other>
asap::duration< convert >::operator duration< other > ( ) const

Converts an duration to another duration factor (e.g, convert an asap::day to an asap::year)

Template Parameters
otherThe other convertion factor
Returns
Another duration with the same factor as other

◆ operator*()

template<uint64_t convert = 1>
asap::duration< convert >::operator* ( ) const

the same as asap::duration::operator double()

Returns
the same as asap::duration::operator double() would return

◆ operator+=() [1/2]

template<uint64_t convert = 1>
template<uint64_t convert2>
asap::duration< convert >::operator+= ( const duration< convert2 > &  other)

Adds another duration to this duration, converting to this convertion factor (e.g, 1_day + 1_week == 1_day + 7_days)

Template Parameters
convert2The convertion factor of other
Parameters
otherThe other duration to add
Returns
*this

◆ operator+=() [2/2]

template<uint64_t convert = 1>
asap::duration< convert >::operator+= ( const duration< convert > &  other)

Adds another duration to this duration with the same convertion factor.

Parameters
otherThe other duration to add
Returns
*this

◆ operator-()

template<uint64_t convert = 1>
asap::duration< convert >::operator- ( )

Changes the sign of a duration.

Returns
Another duration with the sign inverted

◆ operator-=()

template<uint64_t convert = 1>
template<uint64_t convert2>
asap::duration< convert >::operator-= ( const duration< convert2 > &  other)

Subtracts another duration from this duration, converting to this convertion factor (e.g, 1_day -= 1_week == 1_day -= 7_days)

Template Parameters
convert2The convertion factor of other
Parameters
otherThe other duration to subtract
Returns
*this

◆ operator=() [1/2]

template<uint64_t convert = 1>
asap::duration< convert >::operator= ( double  v)

Sets the internal value to v.

Returns
*this
Parameters
vThe value to set

◆ operator=() [2/2]

template<uint64_t convert = 1>
template<uint64_t convert2>
asap::duration< convert >::operator= ( const duration< convert2 > &  other)

Sets this duration as equals to another duration, converting to this convertion factor (e.g, asap::days d = 1_week == asap::days d = 7_days)

Template Parameters
convert2The convertion factor of other
Parameters
otherThe other duration to copy
Returns
*this

◆ str()

template<uint64_t convert = 1>
asap::duration< convert >::str ( ) const

Pretty print this duration, breaking down all the units (e.g: std::cout << 10_days would print 1 week, 3 days)

Returns
an instance of std::string with an asap::duration prettified in it.