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

The asap::period class is a bound period, with starting and ending dates. Its purpose is to provide datetime iteration. More...

Public Member Functions

 period (const asap::datetime &a=asap::datetime(), const asap::datetime &b=asap::datetime())
 Creates a new peeriod starting at a and ending at b. More...
 
const asap::datetimefrom () const
 Returns an asap::datetime instance marking the beginning of the period. More...
 
void from (const asap::datetime &begin)
 Sets the beginning of the period. More...
 
const asap::datetimeto () const
 Returns an asap::datetime instance marking the end of the period. More...
 
void to (const asap::datetime &end)
 Sets the end of the period. More...
 
template<typename T >
difference () const
 Calculates the difference between the start and the end. More...
 
asap::seconds difference () const
 Calculates the difference between the start and the end as seconds. More...
 
template<uint64_t stepconv>
asap::detail::accessor< stepconv > every (const asap::duration< stepconv > &d) const
 Returns an accessor that can be used to iterate between the beginning and the end dates. More...
 

Detailed Description

The asap::period class is a bound period, with starting and ending dates. Its purpose is to provide datetime iteration.

for (auto d: asap::period(asap::now(), asap::tomorow()).every(1_hour)) {
std::cout << d << std::endl;
}

You can also use the asap::datetime::until method to ease construction of asap::period:

for (auto d: asap::now().until(asap::tomorrow()).every(1_hour)) {
std::cout << d << std::endl;
}

You can specify a period and re-use it as many times as you want:

for (auto d: period.every(1_hour)) std::cout << d << std::endl;
for (auto d: period.every(1_minute)) std::cout << d << std::endl;

period::difference() returns the difference between the end asap::datetime minus the beginning. You can specify the return type as a template parameter (or you can just crate an asap::duration from it):

auto diff1 = period.difference(); // as seconds;
auto diff2 = period.difference<asap::days>(); // as days;
asap::years diff3 = period.difference(); // as years;

You can also obtain the beginning and the end asap::datetime from a period using from() and to(). You can also use from(datetime) and to(datetime) to set the beginning and the end of a period. Existing iterations won't be affected.

Definition at line 31 of file period.h.

Constructor & Destructor Documentation

◆ period()

asap::period::period ( const asap::datetime a = asap::datetime(),
const asap::datetime b = asap::datetime() 
)
explicit

Creates a new peeriod starting at a and ending at b.

Parameters
aAn asap::datetime instance that will be the start of the period
bAn asap::datetime instance that will mark the end

Member Function Documentation

◆ difference() [1/2]

template<typename T >
asap::period::difference ( ) const

Calculates the difference between the start and the end.

Template Parameters
Tthe type to return (e.g, asap::seconds)
Returns
An T instance

◆ difference() [2/2]

asap::seconds asap::period::difference ( ) const

Calculates the difference between the start and the end as seconds.

Returns
An asap::seconds instance

◆ every()

template<uint64_t stepconv>
asap::period::every ( const asap::duration< stepconv > &  d) const

Returns an accessor that can be used to iterate between the beginning and the end dates.

Parameters
dThe stepping duration (e.g, 1_day)
Returns
an period accessor to be used when iterating the period range

This method is useful when iterating between instances of asap::datetime, it returns an accessor that has a begin() and end() method, thus allowing you to use range-based for loops or even use iterators.

◆ from() [1/2]

asap::period::from ( ) const

Returns an asap::datetime instance marking the beginning of the period.

Returns
the beginning of the period

◆ from() [2/2]

asap::period::from ( const asap::datetime begin)

Sets the beginning of the period.

Parameters
beginAn asap::datetime instance to serve as the beginning
Returns
*this

◆ to() [1/2]

asap::period::to ( ) const

Returns an asap::datetime instance marking the end of the period.

Returns
the end of the period

◆ to() [2/2]

asap::period::to ( const asap::datetime end)

Sets the end of the period.

Parameters
endAn asap::datetime instance to serve as the end
Returns
*this