Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Reference

Header <boost/chrono/include.hpp>
Included on the C++11 Recommendation
Chrono I/O V1
Chrono I/O V2
Chrono Rounding Utilities
Other Clocks

As constexpr will not be supported by some compilers, it is replaced in the code by BOOST_CONSTEXPR for constexpr functions and BOOST_STATIC_CONSTEXPR for struct/class static fields. The same applies to noexecpt which is replaced by noexcept in the code.

The documentation doesn't use these macros.

Include all the chrono header files.

#include <boost/chrono/chrono.hpp>
#include <boost/chrono/chrono_io.hpp>
#include <boost/chrono/process_cpu_clocks.hpp>
#include <boost/chrono/thread_clocks.hpp>
#include <boost/chrono/ceil.hpp>
#include <boost/chrono/floor.hpp>
#include <boost/chrono/round.hpp>

Include only the standard files.

#include <boost/chrono/chrono.hpp>

Include only the standard files.

#include <boost/chrono/duration.hpp>
#include <boost/chrono/time_point.hpp>
#include <boost/chrono/system_clocks.hpp>
#include <boost/chrono/typeof/boost/chrono/chrono.hpp>

At present, tthere is no know limitation respect to the C++11 standard.

The current implementation provides in addition:

When BOOST_NO_STATIC_ASSERT is defined, the user can select the way static assertions are reported. Define

  • BOOST_CHRONO_USES_STATIC_ASSERT: define it if you want to use Boost.StaticAssert.
  • BOOST_CHRONO_USES_MPL_ASSERT: define it if you want to use Boost.MPL static asertions.
  • BOOST_CHRONO_USES_ARRAY_ASSERT: define it if you want to use internal static asertions.

The default behavior is as BOOST_CHRONO_USES_ARRAY_ASSERT was defined.

When BOOST_CHRONO_USES_MPL_ASSERT is not defined the following symbols are defined as

#define BOOST_CHRONO_A_DURATION_REPRESENTATION_CAN_NOT_BE_A_DURATION \
    "A duration representation can not be a duration"
#define BOOST_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_DURATION_MUST_BE_A_STD_RATIO \
    "Second template parameter of duration must be a boost::ratio"
#define BOOST_CHRONO_DURATION_PERIOD_MUST_BE_POSITIVE \
    "duration period must be positive"
#define BOOST_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_TIME_POINT_MUST_BE_A_BOOST_CHRONO_DURATION \
    "Second template parameter of time_point must be a boost::chrono::duration"

Depending on the static assertion used system you will have an hint of the failing assertion either through the symbol or through the text.

When BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING is defined the lib don't provides the hybrid error handling prototypes:

Clock::time_point Clock::now(system::error_code&ec=boost::throws());

This allow to be closer to the standard and to avoid the Boost.System dependency, making possible to have Boost.Chrono as a header-only library.

By default BOOST_CHRONO_PROVIDE_HYBRID_ERROR_HANDLING is defined.

When BOOST_CHRONO_HEADER_ONLY is defined the lib is header-only.

If in addition BOOST_USE_WINDOWS_H is defined <windows.h> is included, otherwise files in boost/detail/win are used to reduce the impact of including <windows.h>.

However, you will either need to define BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING or link with Boost.System.

Version 2.0.0 deprecates the preceding IO features.

These deprecated features will be provided by default up to boost 1.55. If you don't want to include the deprecated features you could define BOOST_CHRONO_DONT_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0. Since 1.55 these features will not be included any more by default. Since this version, if you want to include the deprecated features yet you could define BOOST_CHRONO_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0. These deprecated features will be only available until boost 1.58, that is you have 1 year and a half to move to the new features.

BOOST_CHRONO_VERSION defines the Boost.Chrono version. The default version is 1. In this case the following breaking or extending macros are defined if the opposite is not requested:

  • BOOST_CHRONO_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0

The user can request the version 2 by defining BOOST_CHRONO_VERSION to 2. In this case the following breaking or extending macros are defined if the opposite is not requested:

  • Breaking change BOOST_CHRONO_DONT_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0

The default value for BOOST_CHRONO_VERSION will be changed to 2 since Boost 1.55.

This file contains duration specific classes and non-member functions.

namespace boost {
  namespace chrono {

    template <class Rep, class Period = ratio<1> >  class duration;

  }
  template <class Rep1, class Period1, class Rep2, class Period2>
  struct common_type<duration<Rep1, Period1>,
                     duration<Rep2, Period2> >;

  namespace chrono {

    // customization traits
    template <class Rep> struct treat_as_floating_point;
    template <class Rep> struct duration_values;

    // duration arithmetic
    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr
    typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
    operator+(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr
    typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
    operator-(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period, class Rep2>
    constexpr
    duration<typename common_type<Rep1, Rep2>::type, Period>
    operator*(
        const duration<Rep1, Period>& d,
        const Rep2& s);

    template <class Rep1, class Period, class Rep2>
    constexpr
    duration<typename common_type<Rep1, Rep2>::type, Period>
    operator*(
        const Rep1& s,
        const duration<Rep2, Period>& d);

    template <class Rep1, class Period, class Rep2>
    constexpr
    duration<typename common_type<Rep1, Rep2>::type, Period>
    operator/(
        const duration<Rep1, Period>& d,
        const Rep2& s);

    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr
    typename common_type<Rep1, Rep2>::type
    operator/(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    #ifdef BOOST_CHRONO_EXTENSIONS
    // Used to get frecuency of events
    template <class Rep1, class Rep2, class Period>
    constexpr
    double operator/(
        const Rep1& s,
        const duration<Rep2, Period>& d);
    #endif

    // duration comparisons
    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool operator==(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool operator!=(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool __duration__op_le_1(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool operator<=(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool operator>(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool operator>=(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    // duration_cast

    template <class ToDuration, class Rep, class Period>
    constexpr
    ToDuration duration_cast(const duration<Rep, Period>& d);

    // convenience typedefs
    typedef duration<boost::int_least64_t, nano> nanoseconds;    // at least 64 bits needed
    typedef duration<boost::int_least64_t, micro> microseconds;  // at least 55 bits needed
    typedef duration<boost::int_least64_t, milli> milliseconds;  // at least 45 bits needed
    typedef duration<boost::int_least64_t> seconds;              // at least 35 bits needed
    typedef duration<boost::int_least32_t, ratio< 60> > minutes; // at least 29 bits needed
    typedef duration<boost::int_least32_t, ratio<3600> > hours;  // at least 23 bits needed

  }
}
template <class Rep> struct treat_as_floating_point
    : boost::is_floating_point<Rep> {};

The duration template uses the treat_as_floating_point trait to help determine if a duration with one tick period can be converted to another duration with a different tick period. If treat_as_floating_point<Rep>::value is true, then Rep is a floating-point type and implicit conversions are allowed among durations. Otherwise, the implicit convertibility depends on the tick periods of the durations. If Rep is a class type which emulates a floating-point type, the author of Rep can specialize treat_as_floating_point so that duration will treat this Rep as if it were a floating-point type. Otherwise Rep is assumed to be an integral type, or a class emulating an integral type.

template <class Rep>
struct duration_values
{
public:
    static constexpr Rep zero();
    static constexpr Rep max();
    static constexpr Rep min();
};

The duration template uses the duration_values trait to construct special values of the duration's representation (Rep). This is done because the representation might be a class type with behavior which requires some other implementation to return these special values. In that case, the author of that class type should specialize duration_values to return the indicated values.

static constexpr Rep zero();

Returns: Rep(0). Note: Rep(0) is specified instead of Rep() since Rep() may have some other meaning, such as an uninitialized value.

Remarks: The value returned corresponds to the additive identity.

static constexpr Rep max();

Returns: numeric_limits<Rep>::max().

Remarks: The value returned compares greater than zero().

static constexpr Rep min();

Returns: numeric_limits<Rep>::lowest().

Remarks: The value returned compares less than or equal to zero().

template <class Rep1, class Period1, class Rep2, class Period2>
struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2> >
{
    typedef chrono::duration<typename common_type<Rep1, Rep2>::type, see bellow> type;
};

The period of the duration indicated by this specialization of common_type is the greatest common divisor of Period1 and Period2. This can be computed by forming a ratio of the greatest common divisor of Period1::num and Period2::num, and the least common multiple of Period1::den and Period2::den.

Note: The typedef type is the duration with the largest tick period possible where both duration arguments will convert to it without requiring a division operation. The representation of this type is intended to be able to hold any value resulting from this conversion, with the possible exception of round-off error when floating-point durations are involved (but not truncation error).

A duration measures time between two points in time (time_point). A duration has a representation which holds a count of ticks, and a tick period. The tick period is the amount of time which occurs from one tick to another in units of a second. It is expressed as a rational constant using ratio.

namespace boost { namespace chrono {

    template <class Rep, class Period>
    class duration {
    public:
        typedef Rep rep;
        typedef Period period;
    private:
        rep rep_; // exposition only
    public:
        constexpr duration();
        template <class Rep2>
        constexpr explicit duration(const Rep2& r);

        template <class Rep2, class Period2>
        constexpr duration(const duration<Rep2, Period2>& d);

        duration& operator=(const duration&) = default;

        constexpr rep count() const;

        constexpr duration  __duration__op_plus();
        constexpr duration  __duration__op_minus();
        duration& operator++();
        duration  operator++(int);
        duration& operator--();
        duration  operator--(int);

        duration& operator+=(const duration& d);
        duration& operator-=(const duration& d);

        duration& operator*==(const rep& rhs);
        duration& operator/=(const rep& rhs);
        duration& operator%=(const rep& rhs);
        duration& operator%=(const duration& rhs);

        static constexpr duration zero();
        static constexpr duration min();
        static constexpr duration max();
    };

}}

Rep must be an arithmetic type, or a class emulating an arithmetic type, compile diagnostic otherwise. If duration is instantiated with the type of Rep being a duration, compile diagnostic is issued.

Period must be an instantiation of ratio, compile diagnostic otherwise.

Period::num must be positive, compile diagnostic otherwise.

Examples:

  • duration<long, ratio<60> > holds a count of minutes using a long.
  • duration<long long, milli> holds a count of milliseconds using a long long.
  • duration<double, ratio<1, 30> > holds a count using a double with a tick period of 1/30 second (a tick frequency of 30 Hz).

The following members of duration do not throw an exception unless the indicated operations on the representations throw an exception.

constexpr duration();

Effects: Constructs an object of type duration from duration_values<rep>::zero().

template <class Rep2>
constexpr explicit duration(const Rep2& r);

Remarks: Rep2 is implicitly convertible to rep, and

  • treat_as_floating_point<rep>::value is true, or
  • !treat_as_floating_point<rep>::value && !treat_as_floating_point<Rep2>::value is true.

If these constraints are not met, this constructor will not participate in overload resolution. Note: This requirement prevents construction of an integral-based duration with a floating-point representation. Such a construction could easily lead to confusion about the value of the duration.

Example:

duration<int, milli> d(3.5);  // do not compile
duration<int, milli> d(3);    // ok

Effects: Constructs an object of type duration.

Post Conditions: count() == static_cast<rep>(r).

template <class Rep2, class Period2>
constexpr duration(const duration<Rep2, Period2>& d);

Remarks: treat_as_floating_point<rep>::value, or ratio_divide<Period2, period>::type::den == 1, else this constructor will not participate in overload resolution. note This requirement prevents implicit truncation error when converting between integral-based durations. Such a construction could easily lead to confusion about the value of the duration.

Example:

duration<int, milli> ms(3);
duration<int, micro> us = ms;  // ok
duration<int, milli> ms2 = us; // do not compile

Effects: Constructs an object of type duration, constructing rep_ from duration_cast<duration>(d).count().

constexpr rep count() const;

Returns: rep_.

constexpr duration operator+() const;

Returns: *this.

constexpr duration operator-() const;

Returns: duration(-rep_).

duration& operator++();

Effects: ++rep_.

Returns: *this.

duration operator++(int);

Returns: duration(rep_++).

duration& operator--();

Effects: --rep_.

Returns: *this.

duration operator--(int);

Returns: duration(rep_--).

duration& operator+=(const duration& d);

Effects: rep_ += d.count().

Returns: *this.

duration& operator-=(const duration& d);

Effects: rep_ -= d.count().

Returns: *this.

duration& operator%=(const duration& d);

Effects: rep_ %= d.count().

Returns: *this.

duration& operator*=(const rep& rhs);

Effects: rep_ *= rhs.

Returns: *this.

duration& operator/=(const rep& rhs);

Effects: rep_ /= rhs.

Returns: *this.

duration& operator%=(const rep& rhs);

Effects: rep_ %= rhs.

Returns: *this.

static constexpr duration zero();

Returns: duration(duration_values<rep>::zero()).

static constexpr duration min();

Returns: duration(duration_values<rep>::min()).

static constexpr duration max();

Returns: duration(duration_values<rep>::max()).

template <class Rep1, class Period1, class Rep2, class Period2>
constexpr
typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);

Returns: CD(CD(lhs).count() + CD(rhs).count()) where CD is the type of the return value.

template <class Rep1, class Period1, class Rep2, class Period2>
constexpr
typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);

Returns: CD(CD(lhs).count() - CD(rhs).count()) where CD is the type of the return value.

template <class Rep1, class Period, class Rep2>
constexpr
duration<typename common_type<Rep1, Rep2>::type, Period>
operator*(const duration<Rep1, Period>& d, const Rep2& s);

Requires: Let CR represent the common_type of Rep1 and Rep2. This function will not participate in overload resolution unless both Rep1 and Rep2 are implicitly convertible to CR.

Returns: CD(CD(d).count() * s) where CD is the type of the return value.

template <class Rep1, class Period, class Rep2>
constexpr
duration<typename common_type<Rep1, Rep2>::type, Period>
operator*(const Rep1& s, const duration<Rep2, Period>& d);

Requires: Let CR represent the common_type of Rep1 and Rep2. This function will not participate in overload resolution unless both Rep1 and Rep2 are implicitly convertible to CR.

Returns: d * s.

template <class Rep1, class Period, class Rep2>
constexpr
duration<typename common_type<Rep1, Rep2>::type, Period>
operator/(const duration<Rep1, Period>& d, const Rep2& s);

Requires: Let CR represent the common_type of Rep1 and Rep2. This function will not participate in overload resolution unless both Rep1 and Rep2 are implicitly convertible to CR, and Rep2 is not an instantiation of duration.

Returns: CD(CD(d).count() / s) where CD is the type of the return value.

template <class Rep1, class Period1, class Rep2, class Period2>
constexpr
typename common_type<Rep1, Rep2>::type
operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);

Remarks: Let CD represent the common_type of the two duration arguments. Returns: Returns CD(lhs).count() / CD(rhs).count().

Included only if BOOST_CHRONO_EXTENSIONS is defined.

This overloading could be used to get the frequency of an event counted by Rep1.

template <class Rep1, class Rep2, class Period>
constexpr
double operator/(const Rep1& s, const duration<Rep2, Period>& d);

Remarks: Let CR represent the common_type of Rep1 and Rep2. This function will not participate in overload resolution unless both Rep1 and Rep2 are implicitly convertible to CR, and Rep1 is not an instantiation of duration. Let CD represent duration<CR,Period>.

Returns: CR(s)/CD(d).count() where CD is the type of the return value.

template <class Rep1, class Period, class Rep2>
constexpr
duration<typename common_type<Rep1, Rep2>::type, Period>
operator%(const duration<Rep1, Period>& d, const Rep2& s);

Remarks: Let CR represent the common_type of Rep1 and Rep2. This function will not participate in overload resolution unless Rep2 must be implicitly convertible to CR and Rep2 must not be an instantiation of duration.

Returns: CD(CD(d).count() % s) where CD is the type of the return value.

template <class Rep1, class Period1, class Rep2, class Period2>
constexpr
typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
operator%(const duration<Rep1, Period1>& lhs,
          const duration<Rep2, Period2>& rhs);

Remarks: This function will not participate in overload resolution unless

Returns: CD(CD(lhs).count() % CD(rhs).count()) where CD is the type of the return value.

template <class Rep1, class Period1, class Rep2, class Period2>
bool operator==(const duration<Rep1, Period1>& lhs,
                const duration<Rep2, Period2>& rhs);

Returns: Let CD represent the common_type of the two duration arguments.

Returns: Returns CD(lhs).count() == CD(rhs).count()

template <class Rep1, class Period1, class Rep2, class Period2>
bool operator!=(const duration<Rep1, Period1>& lhs,
                const duration<Rep2, Period2>& rhs);

Returns: !(lhs == rhs).

template <class Rep1, class Period1, class Rep2, class Period2>
bool operator< (const duration<Rep1, Period1>& lhs,
                const duration<Rep2, Period2>& rhs);

Returns: Let CD represent the common_type of the two duration arguments. Returns CD(lhs).count() < CD(rhs).count()

template <class Rep1, class Period1, class Rep2, class Period2>
bool operator<=(const duration<Rep1, Period1>& lhs,
                const duration<Rep2, Period2>& rhs);

Returns: !(rhs < lhs).

template <class Rep1, class Period1, class Rep2, class Period2>
bool operator> (const duration<Rep1, Period1>& lhs,
                const duration<Rep2, Period2>& rhs);

Returns: rhs < lhs.

template <class Rep1, class Period1, class Rep2, class Period2>
bool operator>=(const duration<Rep1, Period1>& lhs,
                const duration<Rep2, Period2>& rhs);

Returns: !(lhs < rhs).

template <class ToDuration, class Rep, class Period>
ToDuration duration_cast(const duration<Rep, Period>& d);

Requires: This function will not participate in overload resolution unless ToDuration is an instantiation of duration.

Returns: Forms CF which is a ratio resulting from ratio_divide<Period, typename ToDuration::period>::type. Let CR be the common_type of ToDuration::rep, Rep, and intmax_t.

  • If CF::num == 1 and CF::den == 1, then returns ToDuration(static_cast<typename ToDuration::rep>(d.count()))
  • else if CF::num != 1 and CF::den == 1, then returns ToDuration(static_cast<typename ToDuration::rep>(static_cast<CR>(d.count()) * static_cast<CR>(CF::num)))
  • else if CF::num == 1 and CF::den != 1, then returns ToDuration(static_cast<typename ToDuration::rep>(static_cast<CR>(d.count()) / static_cast<CR>(CF::den)))
  • else returns ToDuration(static_cast<typename ToDuration::rep>(static_cast<CR>(d.count()) * static_cast<CR>(CF::num) / static_cast<CR>(CF::den)))

Remarks: This function does not rely on any implicit conversions. All conversions must be accomplished through static_cast. The implementation avoids all multiplications or divisions when it is known at compile-time that it can be avoided because one or more arguments are 1. All intermediate computations are carried out in the widest possible representation and only converted to the destination representation at the final step.

// convenience typedefs
typedef duration<boost::int_least64_t, nano> nanoseconds;    // at least 64 bits needed
typedef duration<boost::int_least64_t, micro> microseconds;  // at least 55 bits needed
typedef duration<boost::int_least64_t, milli> milliseconds;  // at least 45 bits needed
typedef duration<boost::int_least64_t> seconds;              // at least 35 bits needed
typedef duration<boost::int_least32_t, ratio< 60> > minutes; // at least 29 bits needed
typedef duration<boost::int_least32_t, ratio<3600> > hours;  // at least 23 bits needed

A clock represents a bundle consisting of a duration, a time_point, and a function now() to get the current time_point. A clock must meet the requirements in the following Table.

In this table C1 and C2 denote Clock types. t1 and t2 are values returned from C1::now() where the call returning t1 happens before the call returning t2 and both of these calls occur before C1::time_point::max(). (note This means C1 did not wrap around between t1 and t2.).

Table 5.1. Clock Requirements

expression

return type

operational semantics

C1::rep

An arithmetic type or class emulating an arithmetic type.

The representation type of the duration and time_point.

C1::period

ratio

The tick period of the clock in seconds.

C1::duration

chrono::duration<C1::rep, C1::period>

The duration type of the clock.

C1::time_point

chrono::time_point<C1> or chrono::time_point<C2, C1::duration>

The time_point type of the clock. Different clocks are permitted to share a time_point definition if it is valid to compare their time_points by comparing their respective durations. C1 and C2 must refer to the same epoch.

C1::is_steady

constexpr bool

true if t1 <= t2 is always true, else false. Note: A clock that can be adjusted backwards is not steady

C1::now()

C1::time_point

Returns a time_point representing the current point in time.


Models of Clock:

A type TC meets the TrivialClock requirements if:

  • TC satisfies the Clock requirements,
  • the types TC::rep, TC::duration, and TC::time_point satisfy the requirements of EqualityComparable, LessThanComparable, DefaultConstructible, CopyConstructible, CopyAssignable, Destructible, and the requirements of numeric types.
[Note] Note

This means, in particular, that operations on these types will not throw exceptions.

  • lvalues of the types TC::rep, TC::duration, and TC::time_point are swappable,
  • the function TC::now() does not throw exceptions, and
  • the type TC::time_point::clock meets the TrivialClock requirements, recursively.

Models of TrivialClock:

A type EcC meets the EcClock requirements if

  • TC satisfies the TrivialClock requirements, and
  • it add now() interfaces allowing to recover internal error codes as described in the following table.

In this table C1 denotes a EcClock type and ec is an instance of a boost::system::error_code.

Table 5.2. Clock Requirements

expression

return type

operational semantics

C1::now(ec)

C1::time_point

Returns a time_point representing the current point in time. ec will stores the error-code in case something was wrong internally.

C1::now(boost::throws())

C1::time_point

Returns a time_point representing the current point in time. Throws a boost::system::system_error exception in case something was wrong internally.


Models of Clock:

This file contains time_point specific classes and non-member functions.

namespace boost {
  namespace chrono {

    template <class Clock, class Duration = typename Clock::duration>
    class time_point;

  }
  template <class Clock, class Duration1, class Duration2>
  struct common_type<time_point<Clock, Duration1>,
                     time_point<Clock, Duration2> >;

  namespace chrono {

    // time_point arithmetic
    template <class Clock, class Duration1, class Rep2, class Period2>
    constexpr time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2> >::type>
    operator+(const time_point<Clock, Duration1>& lhs,
              const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period1, class Clock, class Duration2>
    constexpr time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type>
    operator+(const duration<Rep1, Period1>& lhs,
              const time_point<Clock, Duration2>& rhs);

    template <class Clock, class Duration1, class Rep2, class Period2>
    constexpr time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2> >::type>
    operator-(const time_point<Clock, Duration1>& lhs,
              const duration<Rep2, Period2>& rhs);

    template <class Clock, class Duration1, class Duration2>
    constexpr typename common_type<Duration1, Duration2>::type
    operator-(const time_point<Clock, Duration1>& lhs,
              const time_point<Clock, Duration2>& rhs);

    // time_point comparisons
    template <class Clock, class Duration1, class Duration2>
    constexpr bool
    operator==(const time_point<Clock, Duration1>& lhs,
               const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr bool
    operator!=(const time_point<Clock, Duration1>& lhs,
               const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr bool
    operator<(const time_point<Clock, Duration1>& lhs,
              const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr bool
    operator<=(const time_point<Clock, Duration1>& lhs,
               const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr bool
    operator>(const time_point<Clock, Duration1>& lhs,
              const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr bool
    operator>=(const time_point<Clock, Duration1>& lhs,
               const time_point<Clock, Duration2>& rhs);

    // time_point_cast
    template <class ToDuration, class Clock, class Duration>
    constexpr time_point<Clock, ToDuration>
    time_point_cast(const time_point<Clock, Duration>& t);

  }
}
template <class Clock, class Duration1, class Duration2>
struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2> >
{
    typedef chrono::time_point<Clock, typename common_type<Duration1, Duration2>::type> type;
};

The common_type of two time_points is a time_point with the same Clock (both have the same Clock), and the common_type of the two durations.

A time_point represents a point in time with respect to a specific clock.

template <class Clock, class Duration>
class time_point {
public:
    typedef Clock                     clock;
    typedef Duration                  duration;
    typedef typename duration::rep    rep;
    typedef typename duration::period period;
private:
    duration d_; // exposition only
public:
    constexpr time_point();

    constexpr explicit time_point(const duration& d);

    // conversions
    template <class Duration2>
    constexpr
    time_point(const time_point<clock, Duration2>& t);

    // observer
    constexpr duration time_since_epoch() const;

    // arithmetic

    #ifdef BOOST_CHRONO_EXTENSIONS
    constexpr time_point  operator+();
    constexpr time_point  operator-();
    time_point& operator++();
    time_point  operator++(int);
    time_point& operator--();
    time_point  operator--(int);

    time_point& __time_point__op_plus_eq_1(const rep& d);
    time_point& operator-=(const rep& d);
    #endif

    time_point& __time_point__op_plus_eq_2(const duration& d);
    time_point& operator-=(const duration& d);

    // special values

    static constexpr time_point min();
    static constexpr time_point max();
};

Clock must meet the Clock requirements.

Duration must be an instantiation of duration, compile diagnostic otherwise.

constexpr time_point();

Effects: Constructs an object of time_point, initializing d_ with duration::zero(). This time_point represents the epoch.

constexpr time_point(const duration& d);

Effects: Constructs an object of time_point, initializing d_ with d. This time_point represents the epoch + d.

template <class Duration2>
constexpr
time_point(const time_point<clock, Duration2>& t);

Requires: This function will not participate in overload resolution unless Duration2 is implicitly convertible to duration.

Effects: Constructs an object of time_point, initializing d_ with t.time_since_epoch().

constexpr duration time_since_epoch() const;

Returns: d_.

constexpr time_point operator+() const;

Returns: *this.

constexpr time_point operator-() const;

Returns: time_point(-d_).

time_point& operator++();

Effects: ++d_.

Returns: *this.

time_point operator++(int);

Returns: time_point(d_++).

time_point& operator--();

Effects: --d_.

Returns: *this.

time_point operator--(int);

Returns: time_point(d_--).

time_point& operator+=(const rep& r);

Effects: d_ += duration(r).

Returns: *this.

time_point& operator-=(const rep& r);

Effects: d_ -= duration(r)

Returns: *this.

time_point& operator+=(const duration& d);

Effects: d_ += d.

Returns: *this.

time_point& operator-=(const duration& d);

Effects: d_ -= d

Returns: *this.

static constexpr time_point min();

Returns: time_point(duration::min()).

static constexpr time_point max();

Returns: time_point(duration::max()).

template <class Clock, class Duration1, class Rep2, class Period2>
constexpr
time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2> >::type>
operator+(const time_point<Clock, Duration1>& lhs,
          const duration<Rep2, Period2>& rhs);

Returns: CT(lhs.time_since_epoch() + rhs) where CT is the type of the return value.

template <class Rep1, class Period1, class Clock, class Duration2>
constexpr
time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type>
operator+(const duration<Rep1, Period1>& lhs,
          const time_point<Clock, Duration2>& rhs);

Returns: rhs + lhs.

template <class Clock, class Duration1, class Rep2, class Period2>
constexpr
time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2> >::type>
operator-(const time_point<Clock, Duration1>& lhs,
          const duration<Rep2, Period2>& rhs);

Returns: lhs + (-rhs).

template <class Clock, class Duration1, class Duration2>
constexpr
typename common_type<Duration1, Duration2>::type
operator-(const time_point<Clock, Duration1>& lhs,
          const time_point<Clock, Duration2>& rhs);

Returns: lhs.time_since_epoch() - rhs.time_since_epoch().

template <class Clock, class Duration1, class Duration2>
constexpr
bool operator==(const time_point<Clock, Duration1>& lhs,
                const time_point<Clock, Duration2>& rhs);

Returns: lhs.time_since_epoch() == rhs.time_since_epoch().

template <class Clock, class Duration1, class Duration2>
constexpr
bool operator!=(const time_point<Clock, Duration1>& lhs,
                const time_point<Clock, Duration2>& rhs);

Returns: !(lhs == rhs).

template <class Clock, class Duration1, class Duration2>
constexpr
bool operator< (const time_point<Clock, Duration1>& lhs,
                const time_point<Clock, Duration2>& rhs);

Returns: lhs.time_since_epoch() < rhs.time_since_epoch().

template <class Clock, class Duration1, class Duration2>
constexpr
bool operator<=(const time_point<Clock, Duration1>& lhs,
                const time_point<Clock, Duration2>& rhs);

Returns: !(rhs < lhs).

template <class Clock, class Duration1, class Duration2>
constexpr
bool operator>(const time_point<Clock, Duration1>& lhs,
               const time_point<Clock, Duration2>& rhs);

Returns: rhs < lhs.

template <class Clock, class Duration1, class Duration2>
constexpr
bool operator>=(const time_point<Clock, Duration1>& lhs,
                const time_point<Clock, Duration2>& rhs);

Returns: !(lhs < rhs).

template <class ToDuration, class Clock, class Duration>
constexpr
time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);

Requires: This function will not participate in overload resolution unless ToDuration is an instantiation of duration.

Returns: time_point<Clock, ToDuration>(duration_cast<ToDuration>(t.time_since_epoch())).

This file contains the standard clock classes. The types defined in this section satisfy the TrivialClock requirements

namespace boost {
  namespace chrono {

    // Clocks
    class system_clock;
    class steady_clock;
    class high_resolution_clock;

    template <class CharT>
    struct clock_string<system_clock, CharT>;
    template <class CharT>
    struct clock_string<steady_clock, CharT>;

  }
}

The system_clock class provides a means of obtaining the current wall-clock time from the system-wide real-time clock. The current time can be obtained by calling system_clock::now(). Instances of system_clock::time_point can be converted to and from time_t with the system_clock::to_time_t() and system_clock::to_time_point() functions. If system clock is not steady, a subsequent call to system_clock::now() may return an earlier time than a previous call (e.g. if the operating system clock is manually adjusted, or synchronized with an external clock).

The current implementation of system_clock is related an epoch (midnight UTC of January 1, 1970), but this is not in the contract. You need to use the static function static

std::time_t to_time_t(const time_point& t);

which returns a time_t type that is based on midnight UTC of January 1, 1970.

class system_clock {
public:
    typedef see bellow          duration;
    typedef duration::rep                        rep;
    typedef duration::period                     period;
    typedef chrono::time_point<system_clock>     time_point;
    static constexpr bool is_steady =            false;


    static time_point  now() noexcept;
    static time_point  now(system::error_code & ec);

    // Map to C API
    static std::time_t to_time_t(const time_point& t) noexcept;
    static time_point  from_time_t(std::time_t t) noexcept;
};

system_clock satisfy the Clock requirements:

  • system_clock::duration::min() < system_clock::duration::zero() is true.
  • The nested duration typedef has a resolution that depends on the one provided by the platform.
time_t to_time_t(const time_point& t) noexcept;

Returns: A time_t such that the time_t and t represent the same point in time, truncated to the coarser of the precisions among time_t and time_point.

time_point from_time_t(time_t t) noexcept;

Returns: A time_point such that the time_point and t represent the same point in time, truncated to the coarser of the precisions among time_point and time_t.

Defined if the platform support steady clocks.

steady_clock satisfy the Clock requirements.

steady_clock class provides access to the system-wide steady clock. The current time can be obtained by calling steady_clock::now(). There is no fixed relationship between values returned by steady_clock::now() and wall-clock time.

#ifdef BOOST_HAS_CLOCK_STEADY
    class steady_clock {
    public:
        typedef nanoseconds                          duration;
        typedef duration::rep                        rep;
        typedef duration::period                     period;
        typedef chrono::time_point<steady_clock>     time_point;
        static constexpr bool is_steady =            true;

        static time_point  now() noexcept;
        static time_point  now(system::error_code & ec);
    };
#endif

high_resolution_clock satisfy the Clock requirements.

#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  typedef steady_clock high_resolution_clock;  // as permitted by [time.clock.hires]
#else
  typedef system_clock high_resolution_clock;  // as permitted by [time.clock.hires]
#endif
template <class CharT>
struct clock_string<system_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};

clock_string<>::name() returns "system_clock".

clock_string<>::since() returns " since Jan 1, 1970"

#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY

template <class CharT>
struct clock_string<steady_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};
#endif

clock_string<>::name() returns "steady_clock".

clock_string<>::since() returns " since boot"

namespace boost {
  namespace chrono {

    template <class Clock, class CharT>
    struct clock_string;

  }
}
template <class Clock, class CharT>
struct clock_string;

This template must be specialized for specific clocks. The specialization must define the following functions

static std::basic_string<CharT> name();
static std::basic_string<CharT> since();

clock_string<>::name() return the clock name, which usually corresponds to the class name.

clock_string<>::since() return the textual format of the clock epoch.

Register duration<> and time_point<> class templates to Boost.Typeof.

namespace boost {
namespace chrono {

    template <class CharT>
    class duration_punct;

    template <class CharT, class Traits>
        std::basic_ostream<CharT, Traits>&
        duration_short(std::basic_ostream<CharT, Traits>& os);

    template <class CharT, class Traits>
        std::basic_ostream<CharT, Traits>&
        duration_long(std::basic_ostream<CharT, Traits>& os);

    template <class CharT, class Traits, class Rep, class Period>
        std::basic_ostream<CharT, Traits>&
        operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d);

    template <class CharT, class Traits, class Rep, class Period>
        std::basic_istream<CharT, Traits>&
        operator>>(std::basic_istream<CharT, Traits>& is, duration<Rep, Period>& d)

    template <class CharT, class Traits, class Clock, class Duration>
        std::basic_ostream<CharT, Traits>&
        operator<<(std::basic_ostream<CharT, Traits>& os,
               const time_point<Clock, Duration>& tp);

    template <class CharT, class Traits, class Clock, class Duration>
        std::basic_istream<CharT, Traits>&
        operator>>(std::basic_istream<CharT, Traits>& is,
               time_point<Clock, Duration>& tp);

}
}

The duration unit names can be customized through the facet: duration_punct. duration unit names come in two varieties: long and short. The default constructed duration_punct provides names in the long format. These names are English descriptions. Other languages are supported by constructing a duration_punct with the proper spellings for "hours", "minutes" and "seconds", and their abbreviations (for the short format).

template <class CharT>
class duration_punct
    : public std::locale::facet
{
public:
    typedef std::basic_string<CharT> string_type;
    enum {use_long, use_short};

    static std::locale::id id;

    explicit duration_punct(int use = use_long);

    duration_punct(int use,
        const string_type& long_seconds, const string_type& long_minutes,
        const string_type& long_hours, const string_type& short_seconds,
        const string_type& short_minutes, const string_type& short_hours);

    duration_punct(int use, const duration_punct& d);

    template <class Period> string_type short_name() const;
    template <class Period> string_type long_name() const;
    template <class Period> string_type name() const;

    bool is_short_name() const;
    bool is_long_name() const;
};

The short or long format can be easily chosen by streaming a duration_short or duration_long manipulator respectively.

template <class CharT, class Traits>
    std::basic_ostream<CharT, Traits>&
    duration_short(std::basic_ostream<CharT, Traits>& os);

Effects: Set the duration_punct facet to stream durations and time_points as abbreviations.

Returns: the output stream

template <class CharT, class Traits>
    std::basic_ostream<CharT, Traits>&
    duration_long(std::basic_ostream<CharT, Traits>& os);

Effects: Set the duration_punct facet to stream durations and time_points as long text.

Returns: the output stream

Any duration can be streamed out to a basic_ostream. The run-time value of the duration is formatted according to the rules and current format settings for duration::rep. This is followed by a single space and then the compile-time unit name of the duration. This unit name is built on the string returned from ratio_string<> and the data used to construct the duration_punct which was inserted into the stream's locale. If a duration_punct has not been inserted into the stream's locale, a default constructed duration_punct will be added to the stream's locale.

A time_point is formatted by outputting its internal duration followed by a string that describes the time_point::clock epoch. This string will vary for each distinct clock, and for each implementation of the supplied clocks.

template <class CharT, class Traits, class Rep, class Period>
    std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d);

Effects: outputs the duration as an abrevieated or long text format depending on the state of the duration_punct facet.

Returns: the output stream

template <class CharT, class Traits, class Rep, class Period>
    std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& is, duration<Rep, Period>& d)

Effects: reads a duration from the input stream. If a format error is found, the input stream state will be set to failbit.

Returns: the input stream

template <class CharT, class Traits, class Clock, class Duration>
    std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
           const time_point<Clock, Duration>& tp);

Effects: outputs the time_point as an abrevieated or long text format depending on the state of the duration_punct facet.

Returns: the output stream

template <class CharT, class Traits, class Clock, class Duration>
    std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& is,
           time_point<Clock, Duration>& tp);

Effects: reads a time_point from the input stream. If a format error is found, the input stream state will be set to failbit.

Returns: the input stream

This file includes the i/o of the two major components, duration and time_point.

#include <boost/chrono/io/duration_style.hpp>
#include <boost/chrono/io/timezone.hpp>
#include <boost/chrono/io/ios_state.hpp>
#include <boost/chrono/io/duration_get.hpp>
#include <boost/chrono/io/duration_put.hpp>
#include <boost/chrono/io/duration_units.hpp>
#include <boost/chrono/io/duration_io.hpp>
#include <boost/chrono/io/time_point_get.hpp>
#include <boost/chrono/io/time_point_put.hpp>
#include <boost/chrono/io/time_point_units.hpp>
#include <boost/chrono/io/time_point_io.hpp>
namespace boost {
  namespace chrono {

    // typedefs
    enum class duration_style {
      prefix, symbol
    };
  }
}
enum class duration_style {
  prefix, symbol
};

duration unit names come in two varieties: prefix and symbol.

namespace boost {
  namespace chrono {

    struct timezone
    {
      enum type {
        utc, local
      };
    };
  }
}
enum class timezone {
  utc, local
};
namespace boost {
  namespace chrono {

    // setters and getters
    duration_style get_duration_style(std::ios_base & ios);
    void set_duration_style(std::ios_base& ios, duration_style style);

    timezone get_timezone(std::ios_base & ios);
    void set_timezone(std::ios_base& ios, timezone tz);

    template<typename CharT>
    std::basic_string<CharT> get_time_fmt(std::ios_base & ios);
    template<typename CharT>
    void set_time_fmt(std::ios_base& ios, std::basic_string<CharT> const& fmt);

    // i/o state savers
    struct duration_style_io_saver;
    template<typename CharT = char, typename Traits = std::char_traits<CharT> >
    struct timezone_io_saver;
    template<typename CharT = char, typename Traits = std::char_traits<CharT> >
    struct time_fmt_io_saver;
  }
}
duration_style get_duration_style(std::ios_base & ios);

Returns: The stream's duration_style attribute associated to ios.

void set_duration_style(std::ios_base& ios, duration_style style);

Effects: Set the stream's duration_style attribute associated to ios with the style parameter.

timezone get_timezone(std::ios_base & ios);

Returns: The stream's timezone attribute associated to ios.

void set_timezone(std::ios_base& ios, timezone tz);

Effects: Set the stream's timezone attribute associated to ios with the tz parameter.

template<typename CharT>
std::basic_string<CharT> get_time_fmt(std::ios_base & ios);

Returns: The stream's time format attribute associated to ios.

template<typename CharT>
void set_time_fmt(std::ios_base& ios, std::basic_string<CharT> const& fmt);

Effects: Set the stream's time format attribute associated to ios with the fmt parameter.

// i/o state savers

struct duration_style_io_saver
{
  typedef std::ios_base state_type; // the state type is ios_base
  typedef duration_style aspect_type; // the aspect type is the __duration_style

  explicit duration_style_io_saver(state_type &s);
  duration_style_io_saver(state_type &s, aspect_type new_value);
  ~duration_style_io_saver();
  void restore();
};

The state_type is a version of the IOStreams base class std::ios_base.

The first constructor takes a stream object and saves a reference to the stream and the current value of a particular stream attribute.

explicit duration_style_io_saver(state_type &s);

Effects: Constructs a duration_style_io_saver by storing s.

Post Conditions: static_cast<duration_style>(*this) == f.

The second constructor works like the first, and uses its second argument to change the stream's attribute to the new aspect_type value given.

explicit duration_style_io_saver(state_type &s, aspect_type new_value);

Effects: Constructs a duration_style_io_saver by storing s.

Post Conditions: static_cast<duration_style>(*this) == f.

The destructor restores the stream's attribute to the saved value.

~duration_style_io_saver();

Effects: As if restore().

The restoration can be activated early (and often) with the restore member function.

void restore();

Effects: Restores the stream's duration_style attribute to the saved value.

template<typename CharT = char, typename Traits = std::char_traits<CharT> >
struct timezone_io_saver
{
  typedef std::basic_ios<CharT, Traits> state_type;
  typedef timezone aspect_type;

  explicit timezone_io_saver(state_type &s);
  timezone_io_saver(state_type &s, aspect_type new_value);
  ~timezone_io_saver();
  void timezone_io_saver__restore();
};

The state_type is a version of the IOStreams base class template std::basic_ios<CharT, Traits>, where CharT is a character type and Traits is a character traits class. The user would usually place an actual input, output, or combined stream object for the state-type parameter, and not a base class object.

The first constructor takes a stream object and saves a reference to the stream and the current value of a particular stream attribute.

explicit timezone_io_saver(state_type &s);

Effects: Constructs a timezone_io_saver by storing s.

The second constructor works like the first, and uses its second argument to change the stream's attribute to the new aspect_type value given.

explicit timezone_io_saver(state_type &s, aspect_type new_value);

Effects: Constructs a timezone_io_saver by storing s.

The destructor restores the stream's attribute to the saved value.

~timezone_io_saver();

Effects: As if restore().

The restoration can be activated early (and often) with the restore member function.

void restore();

Effects: Restores the stream's timezone attribute to the saved value.

template<typename CharT = char, typename Traits = std::char_traits<CharT> >
struct time_fmt_io_saver
{
  typedef std::basic_ios<CharT, Traits> state_type;

  explicit time_fmt_io_saver(state_type &s);
  time_fmt_io_saver(state_type &s, basic_string<CharT> const& new_value);
  ~time_fmt_io_saver();
  void restore();
};

The state_type is a version of the IOStreams base class template std::basic_ios<CharT, Traits>, where CharT is a character type and Traits is a character traits class. The user would usually place an actual input, output, or combined stream object for the state-type parameter, and not a base class object.

The first constructor takes a stream object and saves a reference to the stream and the current value of a particular stream attribute.

explicit time_fmt_io_saver(state_type &s);

Effects: Constructs a time_fmt_io_saver by storing s.

The second constructor works like the first, and uses its second argument to change the stream's attribute to the new aspect_type value given.

explicit time_fmt_io_saver(state_type &s, aspect_type new_value);

Effects: Constructs a time_fmt_io_saver by storing s.

The destructor restores the stream's attribute to the saved value.

~time_fmt_io_saver();

Effects: As if restore().

The restoration can be activated early (and often) with the restore member function.

void restore();

Effects: Restores the stream's time format attribute to the saved value.

namespace boost {
  namespace chrono {

    template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
    class duration_get;

  }
}
template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
class duration_get: public std::locale::facet
{
public:
  typedef CharT char_type; // Type of character the facet is instantiated on
  typedef std::basic_string<CharT> string_type; // Type of character string passed to member functions.
  typedef InputIterator iter_type; // Type of iterator used to scan the character buffer.

  explicit duration_get(size_t refs = 0);

  template <typename Rep, typename Period>
  iter_type get(
    iter_type s,
    iter_type end,
    std::ios_base& ios,
    std::ios_base::iostate& err,
    duration<Rep, Period>& d,
    const char_type* pattern,
    const char_type* pat_end
  ) const;

  template <typename Rep, typename Period>
  iter_type get(
    iter_type s,
    iter_type end,
    std::ios_base& ios,
    std::ios_base::iostate& err,
    duration<Rep, Period>& d
  ) const;

  template <typename Rep>
  iter_type get_value(
    iter_type s,
    iter_type end,
    std::ios_base& ios,
    std::ios_base::iostate& err,
    Rep& r
  ) const;

  iter_type get_unit(
    iter_type i,
    iter_type e,
    std::ios_base& is,
    std::ios_base::iostate& err,
    detail::rt_ratio &rt
  ) const

  static std::locale::id id; // Unique identifier for this type of facet.

  ~duration_get()
  {
  }

};

The duration_get facet extracts duration from a character string and stores the resulting value in a class duration d argument. The facet parses the string using a specific format as a guide. If the string does not fit the format, then the facet will indicate an error by setting the err argument to iosbase::failbit. In other words, user confirmation is required for reliable parsing of user-entered durations, but machine-generated formats can be parsed reliably. This allows parsers to be aggressive about interpreting user variations on standard formats.

explicit duration_get(size_t refs);

Constructs a duration_get facet.

Parameters:

  • refs: references

Effects: Constructs a duration_get facet. If the refs argument is 0 then destruction of the object is delegated to the locale, or locales, containing it. This allows the user to ignore lifetime management issues. On the other had, if refs is 1 then the object must be explicitly deleted; locale will not do so. In this case, the object can be maintained across the lifetime of multiple locales.

~duration_get();

Destructs a duration_get facet.

template <typename Rep, typename Period>
iter_type get(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err,
    duration<Rep, Period> &d, const char_type *pattern, const char_type *pat_end) const;

Extracts a duration from the range [s,end) following the pattern [pattern,pat_end).

Parameters:

  • s: start input stream iterator
  • end: end input stream iterator
  • ios: a reference to a ios_base
  • err: the ios_base state
  • d: the duration
  • pattern: begin of the formatting pattern
  • pat_end: end of the formatting pattern

Requires: [s, end) and [pattern, pat_end) shall be valid ranges.

Effects: The function starts by evaluating err = std::ios_base::goodbit.

Then it computes an intermediate representation based on Rep according to the following rules:

  • If Rep is a floating point type, the intermediate representation is long double.
  • Else if Rep is a signed integral type, the intermediate representation is long long.
  • Else if Rep is an unsigned integral type, the intermediate representation is unsigned long long.
  • Else intermediate representation is Rep.

Next the following local variable r of type intermediate representation and rt of type rt_ratio are default constructed.

It then enters a loop, reading zero or more characters from s at each iteration. Unless otherwise specified below, the loop terminates when the first of the following conditions holds:

  • The expression pattern == pat_end evaluates to true.
  • The expression err == std::ios_base::goodbit evaluates to false.
  • The expression s == end evaluates to true, in which case the function evaluates err = std::ios_base::eofbit | std::ios_base::failbit.
  • The next element of pattern is equal to '%', followed by a conversion specifier character, format. If the number of elements in the range [pattern,pat_end) is not sufficient to unambiguously determine whether the conversion specification is complete and valid, the function evaluates err = std::ios_base::failbit. Otherwise, the function evaluates s = get_value(s, end, ios, err, r) when the conversion specification is 'v' and s = get_value(s, end, ios, err, rt) when the conversion specification is 'u'. If err == std::ios_base::goodbit holds after the evaluation of the expression, the function increments pattern to point just past the end of the conversion specification and continues looping.
  • The expression isspace(*pattern, ios.getloc()) evaluates to true, in which case the function first increments pattern until pattern == pat_end || !isspace(*pattern, ios.getloc()) evaluates to true, then advances s until s == end || !isspace(*s, ios.getloc()) is true, and finally resumes looping.
  • The next character read from s matches the element pointed to by pattern in a case-insensitive comparison, in which case the function evaluates ++pattern, ++s and continues looping. Otherwise, the function evaluates err = std::ios_base::failbit.

If a duration representation value and a unit specifier have successfully been parsed, compute (rt.num/rt.den)/(Period::num/Period::den) reduced to lowest terms. If this ratio can not be stored without overflow, evaluates err = std::ios_base::failbit. Otherwise store the result of this division in num and den.

If the division did not result in overflow, then compute r * num / den in such a way as to avoid intermediate overflow. If r has integral type and this computation would not be exact, evaluates err = std::ios_base::failbit. If the result of the computation would overflow Rep, evaluates err = std::ios_base::failbit. Otherwise the result of r * num / den is used to construct a duration<Rep, Period> which is assigned to d.

Returns: s, an iterator pointing just beyond the last character that can be determined to be part of a valid duration.

template <typename Rep, typename Period>
iter_type get(
  iter_type s,
  iter_type end,
  std::ios_base& ios,
  std::ios_base::iostate& err,
  duration<Rep, Period>& d
) const;

Extracts a duration from the range [s,end) following the default pattern.

Parameters:

  • s: start input stream iterator
  • end: end input stream iterator
  • ios: a reference to a ios_base
  • err: the ios_base state
  • d: the duration

Effects: Stores the duration pattern from the __duration_unit facet associated to 'ios in let say str`. Last as if

return get(s, end, ios, err, ios, d, str.data(), str.data() + str.size());

Returns: s, an iterator pointing just beyond the last character that can be determined to be part of a valid duration.

template <typename Rep>
iter_type get_value(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err, Rep& r) const;

Extracts a duration representation from the range [s,end).

Parameters:

  • s: start input stream iterator
  • end: end input stream iterator
  • ios: a reference to a ios_base
  • err: the ios_base state
  • r: a reference to the duration representation

Effects: As if

return std::use_facet<std::num_get<char_type, iter_type>>(ios.getloc()).get(s, end, ios, err, r);

Returns: s, an iterator pointing just beyond the last character that can be determined to be part of a valid Rep value.

iter_type get_unit(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err, detail::rt_ratio &rt) const;

Extracts a duration unit from the range [s,end).

Parameters:

  • s: start input stream iterator
  • end: end input stream iterator
  • ios: a reference to a ios_base
  • err: the ios_base state
  • rt: a reference to the duration run-time ratio.

Effects:

  • If the first character is '[', an attempt is made to consume a pattern of the form "[N/D]" where N and D have type unsigned long long.
    • If successful, rt records the values of N and D, otherwise evaluates err = std::ios_base::failbit and return i.
    • Return the parse the longest string possible matching one of the durations units that can follow the pattern "[N/D]", as if
return do_get_n_d_prefix_unit(facet, i, e, is, err);
  • Otherwise the next character is not '['. Return the parse the longest string possible matching one of the durations units, as if
return do_get_prefix_unit(facet, i, e, is, err, rt);

Returns: i, an iterator pointing just beyond the last character that can be determined to be part of a valid duration unit.

virtual iter_type do_get_n_d_prefix_unit(
  duration_units<CharT> const &facet,
  iter_type i,
  iter_type e,
  std::ios_base&,
  std::ios_base::iostate& err
) const;

Extracts the run-time ratio associated to the duration when it is given in [N/D] form.

This is an extension point of this facet so that we can take in account other periods that can have a useful translation in other contexts, as e.g. days and weeks.

Parameters:

  • s: start input stream iterator
  • end: end input stream iterator
  • ios: a reference to a ios_base
  • err: the ios_base state
  • rt: a reference to the duration run-time ratio.

Effects: Scan s for the longest of all the plural forms associated with the duration units. If sucessfull, sets the matched ratio in rt. Otherwise evaluates err = std::ios_base::failbit.

Returns: s, an iterator pointing just beyond the last character that can be determined to be part of a valid name.

virtual iter_type do_get_prefix_unit(
  duration_units<CharT> const &facet,
  iter_type i,
  iter_type e,
  std::ios_base&,
  std::ios_base::iostate& err,
  detail::rt_ratio &rt
) const;

Extracts the run-time ratio associated to the duration when it is given in prefix form.

This is an extension point of this facet so that we can take in account other periods that can have a useful translation in other contexts, as e.g. days and weeks.

Parameters:

  • s: start input stream iterator
  • end: end input stream iterator
  • ios: a reference to a ios_base
  • err: the ios_base state
  • rt: a reference to the duration run-time ratio.

Effects: Scan s for the longest of all the plural forms associated with the duration units. If sucessfull, sets the matched ratio in rt. Otherwise evaluates err = std::ios_base::failbit.

Returns: s, an iterator pointing just beyond the last character that can be determined to be part of a valid name.

namespace boost {
  namespace chrono {

    template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
    class duration_put;

  }
}
template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
class duration_put: public std::locale::facet
{
public:
  typedef CharT char_type; // Type of character the facet is instantiated on.
  typedef std::basic_string<CharT> string_type; // Type of character string passed to member functions.
  typedef OutputIterator iter_type; // Type of iterator used to write in the character buffer.

  explicit duration_put(size_t refs = 0);

  template <typename Rep, typename Period>
  iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const CharT* pattern,
      const CharT* pat_end) const;

  template <typename Rep, typename Period>
  iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const;

  template <typename Rep, typename Period>
  iter_type put_value(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const;

  template <typename Rep, typename Period>
  iter_type put_unit(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const;


  static std::locale::id id; // Unique identifier for this type of facet.

  ~duration_put();
};
explicit duration_put(size_t refs);

Constructs a duration_put facet.

Parameters:

  • refs: references

Effects: Constructs a duration_put facet. If the refs argument is 0 then destruction of the object is delegated to the locale, or locales, containing it. This allows the user to ignore lifetime management issues. On the other had, if refs is 1 then the object must be explicitly deleted; locale will not do so. In this case, the object can be maintained across the lifetime of multiple locales.

~duration_put();

Destructs a duration_put facet.

template <typename Rep, typename Period>
iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d, const CharT* pattern,
    const CharT* pat_end) const;

Parameters:

  • s: an output stream iterator
  • ios: a reference to a ios_base
  • d: the duration
  • pattern: begin of the formatting pattern
  • pat_end: end of the formatting pattern

Effects: Steps through the sequence from pattern to pat_end, identifying characters that are part of a pattern sequence. Each character that is not part of a pattern sequence is written to s immediately, and each pattern sequence, as it is identified, results in a call to put_value or put_unit; thus, pattern elements and other characters are interleaved in the output in the order in which they appear in the pattern. Pattern sequences are identified by converting each character c to a char value as if by ct.narrow(c,0), where ct is a reference to ctype<charT> obtained from ios.getloc(). The first character of each sequence is equal to '%', followed by a pattern specifier character specifier, which can be 'v' for the duration value or 'u' for the duration unit. . For each valid pattern sequence identified, calls put_value(s, ios, d) or put_unit(s, ios, d).

Returns: An iterator pointing immediately after the last character produced.

template <typename Rep, typename Period>
iter_type put(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const;

Parameters:

  • s: an output stream iterator
  • ios: a reference to a ios_base
  • d: the duration

Retrieves Stores the duration pattern from the __duration_unit facet in let say str. Last as if

return put(s, ios, d, str.data(), str.data() + str.size());

Returns: An iterator pointing immediately after the last character produced.

template <typename Rep, typename Period>
iter_type put_value(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const;

Parameters:

  • s: an output stream iterator
  • ios: a reference to a ios_base
  • d: the duration

Effects: As if std::use_facet<std::num_put<CharT, iter_type> >(ios.getloc()).put(s, ios, ' ', static_cast<long int> (d.count())).

Returns: An iterator pointing immediately after the last character produced.

template <typename Rep, typename Period>
iter_type put_unit(iter_type s, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const;

Parameters:

  • s: an output stream iterator
  • ios: a reference to a ios_base
  • fill: the fill character
  • d: the duration

Effects: Let facet be the duration_units<CharT> facet associated to ios. If the associated unit is named, as if

string_type str = facet.get_unit(get_duration_style(ios), d);
s=std::copy(str.begin(), str.end(), s);

Otherwise, format the unit as "[Period::num/Period::den]" followed by the unit associated to [N/D] obtained using facet.get_n_d_unit(get_duration_style(ios), d).

Returns: s, iterator pointing immediately after the last character produced.

namespace boost {
  namespace chrono {
    class rt_ratio;
    template <typename CharT = char>
    class duration_units;

  }
}
class rt_ratio
{
public:
  template <typename Period>
  rt_ratio(Period const&) :
    num(Period::type::num), den(Period::type::den)
  {
  }

  rt_ratio(intmax_t n = 0, intmax_t d = 1) :
    num(n), den(d)
  {
  }

  intmax_t num;
  intmax_t den;
};
template <typename CharT = char>
class duration_units: public std::locale::facet
{
public:
  typedef CharT char_type; // Type of character the facet is instantiated on.
  typedef std::basic_string<CharT> string_type; // Type of character string passed to member functions.
  static std::locale::id id; // Unique identifier for this type of facet.

  explicit duration_units(size_t refs = 0);
  virtual const string_type* get_n_d_valid_units_start() const =0;
  virtual const string_type* get_n_d_valid_units_end() const=0;
  virtual const string_type* get_valid_units_start() const=0;
  virtual const string_type* get_valid_units_end() const=0;
  virtual bool match_n_d_valid_unit(const string_type* k) const = 0;
  virtual bool match_valid_unit(const string_type* k, rt_ratio& rt) const = 0;
  virtual string_type get_pattern() const=0;
  template <typename Rep, typename Period>
  string_type get_unit(duration_style style, duration<Rep, Period> const& d) const;
  template <typename Rep, typename Period>
  string_type get_n_d_unit(duration_style style, duration<Rep, Period> const& d) const;
  template <typename Period>
  bool is_named_unit() const;

protected:
  virtual ~duration_units();
  virtual string_type do_get_n_d_unit(duration_style style, rt_ratio rt, intmax_t v) const = 0;
  virtual string_type do_get_unit(duration_style style,rt_ratio rt, intmax_t v) const = 0;
  virtual bool do_is_named_unit(rt_ratio rt) const =0;
};

duration_units facet gives useful information about the duration units, as the number of plural forms, the plural form associated to a duration, the text associated to a plural form and a duration's period,

explicit duration_units(size_t refs = 0);

Construct a duration_units facet.

Parameters:

  • refs: references

Effects: Construct a duration_units facet. If the refs argument is 0 then destruction of the object is delegated to the locale, or locales, containing it. This allows the user to ignore lifetime management issues. On the other had, if refs is 1 then the object must be explicitly deleted; the locale will not do so. In this case, the object can be maintained across the lifetime of multiple locales.

virtual ~duration_units();

Effects: Destroys the facet.

virtual const string_type* get_n_d_valid_units_start() const =0;

Returns: pointer to the start of valid [N/D] units.

virtual const string_type* get_n_d_valid_units_end() const=0;

Returns: pointer to the end of valid [N/D] units.

virtual const string_type* get_valid_units_start() const=0;

Returns: pointer to the start of valid units, symbol or prefix with its different plural forms.

virtual const string_type* get_valid_units_end() const=0;

Returns: pointer to the end of valid units.

virtual bool match_n_d_valid_unit(const string_type* k) const = 0;

Parameters:

  • k: the found pointer to the [N/D] unit.

Returns: true if k matches a valid unit.

virtual bool match_valid_unit(const string_type* k, rt_ratio& rt) const = 0;

Parameters:

  • k: the found pointer to the unit.

Effects: rt is set to the valid Period when the k matches a valid unit. Returns: true if k matches a valid unit.

virtual string_type get_pattern() const=0;

Returns: the pattern to be used by default.

template <typename Rep, typename Period>
string_type get_unit(duration_style style, duration<Rep, Period> const& d) const;

Returns: get_unit(style, d.count(), rt_ratio(Period())), the unit associated to this duration.

template <typename Rep, typename Period>
string_type get_n_d_unit(duration_style style, duration<Rep, Period> const& d) const;

Returns: get_n_d_unit(style, d.count(), rt_ratio(Period())), i.e. the [N/D] suffix unit associated to this duration.

template <typename Period>
bool is_named_unit() const;

Returns: do_is_named_unit(rt_ratio(Period())), true if the unit associated to the given Period is named, false otherwise.

virtual string_type do_get_n_d_unit(duration_style style, rt_ratio rt, intmax_t v) const = 0;

Returns: the [N/D] suffix unit associated to this duration.

virtual string_type do_get_unit(duration_style style,rt_ratio rt, intmax_t v) const = 0;

Returns: the unit associated to this duration.

virtual bool do_is_named_unit(rt_ratio rt) const =0;

Returns: true if the unit associated to the given Period is named, false otherwise.

template <typename CharT = char>
class duration_units_default: public duration_units<CharT>
{
protected:
  static const std::size_t pfs_ = 2; // The defaul English facet has two plural forms.

public:
  typedef CharT char_type;
  typedef std::basic_string<CharT> string_type;

  explicit duration_units_default(size_t refs = 0);
  ~duration_units_default();

  bool match_n_d_valid_unit(const string_type* k) const;
  bool match_valid_unit(const string_type* k, rt_ratio& rt) const;
  const string_type* get_n_d_valid_units_start()const;
  const string_type* get_n_d_valid_units_end()const;
  string_type* get_valid_units_start() const;
  string_type* get_valid_units_end() const;
  string_type get_pattern() const;
protected:
  bool do_is_named_unit(rt_ratio rt) const;
  string_type do_get_n_d_unit(duration_style style, rt_ratio, intmax_t v) const;
  string_type do_get_unit(duration_style style, rt_ratio rt, intmax_t v) const;

  virtual std::size_t do_get_plural_forms() const;
  virtual std::size_t do_get_plural_form(int_least64_t value) const;

  virtual string_type do_get_unit(duration_style style, ratio<1> u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, ratio<60> u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, ratio<3600> u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, atto u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, femto u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, pico u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, nano u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, micro u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, milli u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, centi u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, deci u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, deca u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, hecto u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, kilo u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, mega u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, giga u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, tera u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, peta u, std::size_t pf) const;
  virtual string_type do_get_unit(duration_style style, exa u, std::size_t pf) const;

  virtual string_type do_get_ratio_prefix(duration_style style, atto u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, femto u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, pico u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, nano u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, micro u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, milli u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, centi u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, deci u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, deca u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, hecto u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, kilo u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, mega u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, giga u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, tera u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, peta u) const;
  virtual string_type do_get_ratio_prefix(duration_style style, exa u) const;
};

This class is used to define the strings for the default English. This facet names the units associated to the following periods:

  • atto,
  • femto,
  • pico,
  • nano,
  • micro,
  • milli,
  • centi,
  • deci,
  • ratio<1>,
  • deca,
  • hecto,
  • kilo,
  • mega,
  • giga,
  • tera,
  • peta,
  • exa,
  • ratio<60> and
  • ratio<3600>.
explicit duration_units_default(size_t refs = 0);

Construct a duration_units_default facet.

Parameters:

  • refs: references

Effects: Construct a duration_units_default facet. If the refs argument is 0 then destruction of the object is delegated to the locale, or locales, containing it. This allows the user to ignore lifetime management issues. On the other had, if refs is 1 then the object must be explicitly deleted; the locale will not do so. In this case, the object can be maintained across the lifetime of multiple locales.

virtual ~duration_units_default();

Effects: Destroys the facet.

virtual const string_type* get_n_d_valid_units_start() const;

Returns: pointer to the start of valid [N/D] units.

virtual const string_type* get_n_d_valid_units_end() const;

Returns: pointer to the end of valid [N/D] units.

virtual const string_type* get_valid_units_start() const;

Returns: pointer to the start of valid units, symbol or prefix with its different plural forms.

virtual const string_type* get_valid_units_end() const;

Returns: pointer to the end of valid units.

virtual bool match_n_d_valid_unit(const string_type* k) const;

Parameters:

  • k: the found pointer to the [N/D] unit.

Returns: true if k matches a valid unit.

virtual bool match_valid_unit(const string_type* k, rt_ratio& rt) const;

Parameters:

  • k: the found pointer to the unit.

Effects: rt is set to the valid Period when the k matches a valid unit. Returns: true if k matches a valid unit.

virtual string_type get_pattern() const;

Returns: the pattern to be used by default.

virtual string_type do_get_n_d_unit(duration_style style, rt_ratio rt, intmax_t v) const;

Returns: the [N/D] suffix unit associated to this duration.

virtual string_type do_get_unit(duration_style style,rt_ratio rt, intmax_t v) const;

Returns: the unit associated to this duration.

virtual bool do_is_named_unit(rt_ratio rt) const;

Returns: true if the unit associated to the given Period is named, false otherwise.

virtual std::size_t do_get_plural_forms() const;

Returns: the number of associated plural forms this facet manages.

virtual std::size_t do_get_plural_form(int_least64_t value) const;

Gets the associated plural form.

Parameters:

value: the duration representation

Returns: the plural form associated to the value parameter. In English there are 2 plural forms

  • 0 singular (-1 or 1)
  • 1 plural for all others
virtual string_type do_get_unit(duration_style style, ratio<1> u, std::size_t pf) const;

Parameters:

  • style: the duration style.
  • period: the period associated to the duration hours.
  • pf: the requested plural form.

Returns: if style is symbol returns "s", otherwise if pf is 0 return "second", if pf is 1 "seconds"

virtual string_type do_get_unit(duration_style style, ratio<60> u, std::size_t pf) const;

Parameters:

  • style: the duration style.
  • period: the period associated to the duration hours.
  • pf: the requested plural form.

Returns: if style is symbol returns "min", otherwise if pf is 0 return "minute", if pf is 1 "minutes"

virtual string_type do_get_unit(duration_style style, ratio<3600> u, std::size_t pf) const;

Parameters:

  • style: the duration style.
  • period: the period associated to the duration hours.
  • pf: the requested plural form.

Returns: if style is symbol returns "h", otherwise if pf is 0 return "hour", if pf is 1 "hours"

virtual string_type do_get_unit(duration_style style, atto u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, femto u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, pico u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, nano u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, micro u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, milli u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, centi u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, deci u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, deca u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, hecto u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, kilo u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, mega u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, giga u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, tera u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, peta u, std::size_t pf) const;
virtual string_type do_get_unit(duration_style style, exa u, std::size_t pf) const;

Parameters:

  • style: the duration style.
  • u: the period tag atto.
  • pf: the requested plural form.

Returns: the concatenation of the prefix associated to period u + the one associated to seconds.

virtual string_type do_get_ratio_prefix(duration_style style, atto u) const;
virtual string_type do_get_ratio_prefix(duration_style style, femto u) const;
virtual string_type do_get_ratio_prefix(duration_style style, pico u) const;
virtual string_type do_get_ratio_prefix(duration_style style, nano u) const;
virtual string_type do_get_ratio_prefix(duration_style style, micro u) const;
virtual string_type do_get_ratio_prefix(duration_style style, milli u) const;
virtual string_type do_get_ratio_prefix(duration_style style, centi u) const;
virtual string_type do_get_ratio_prefix(duration_style style, deci u) const;
virtual string_type do_get_ratio_prefix(duration_style style, deca u) const;
virtual string_type do_get_ratio_prefix(duration_style style, hecto u) const;
virtual string_type do_get_ratio_prefix(duration_style style, kilo u) const;
virtual string_type do_get_ratio_prefix(duration_style style, mega u) const;
virtual string_type do_get_ratio_prefix(duration_style style, giga u) const;
virtual string_type do_get_ratio_prefix(duration_style style, tera u) const;
virtual string_type do_get_ratio_prefix(duration_style style, peta u) const;
virtual string_type do_get_ratio_prefix(duration_style style, exa u) const;

Parameters:

  • style: the duration style.
  • u: the period tag atto.

Returns: depending on the value of style return the ratio_string symbol or prefix.

namespace boost {
  namespace chrono {

    // manipulators

    std::ios_base& symbol_format(ios_base& ios);
    std::ios_base& name_format(ios_base& ios);
    class duration_fmt;
    template<class CharT, class Traits>
    std::basic_ostream<CharT, Traits>&
    operator <<(std::basic_ostream<CharT, Traits>& os, duration_fmt d);

    template<class CharT, class Traits>
    std::basic_istream<CharT, Traits>&
    operator >>(std::basic_istream<CharT, Traits>& is, duration_fmt d);


    // duration I/O

    template <class CharT, class Traits, class Rep, class Period>
        std::basic_ostream<CharT, Traits>&
        operator <<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d);

    template <class CharT, class Traits, class Rep, class Period>
        std::basic_istream<CharT, Traits>&
        operator >>(std::basic_istream<CharT, Traits>& is, duration<Rep, Period>& d)

  }
}

There is a parameterized manimulator that takes the duration_style as parameter. The symbol or name format can be easily chosen by streaming a symbol_format or name_format manipulators respectively.

class duration_fmt
{
public:

  explicit duration_fmt(duration_style style) noexcept;

#ifndef BOOST_NO_EXPLICIT_CONVERSION_OPERATORS
  explicit
  operator duration_style() const noexcept;
#endif
  duration_style get_duration_style() const noexcept;
};

template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator <<(std::basic_ostream<CharT, Traits>& os, duration_fmt d);

template<class CharT, class Traits>
std::basic_istream<CharT, Traits>&
operator >>(std::basic_istream<CharT, Traits>& is, duration_fmt d);
explicit duration_fmt(duration_style f) noexcept;

Effects: Constructs a duration_fmt by storing f.

Post Conditions: static_cast<duration_style>(*this) == f.

explicit operator duration_style() const noexcept;
duration_style get_duration_style() const noexcept;

Returns:: The stored duration_fmt f.

template<class CharT, class Traits>
  basic_ostream<CharT, Traits>&
  operator<<(basic_ostream<CharT, Traits>& s, duration_fmt d);

Effects:: set_duration_style(s, static_cast<duration_style>(d)))).

Returns:: s.

template<class CharT, class Traits>
  basic_istream<CharT, Traits>&
  operator>>(basic_istream<CharT, Traits>& s, duration_fmt d);

Effects:: set_duration_style(s, static_cast<duration_style>(d)))).

Returns:: s.

std::ios_base& symbol_format(ios_base& ios);

Effects:: set_duration_style(s, duration_style::symbol).

Returns: ios

std::ios_base& name_format(ios_base& ios);

Effects:: set_duration_style(s, duration_style::prefix).

Returns: ios

Any duration can be streamed out to a basic_ostream. The run-time value of the duration is formatted according to the rules and current format settings for duration::rep and the duration_units facet.

template <class CharT, class Traits, class Rep, class Period>
    std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os, const duration<Rep, Period>& d);

Effects: Behaves as a formatted output function. After constructing a sentry object, if the sentry converts to true, calls to facet.put(os,os,os.fill(),d) where facet is the duration_put<CharT> facet associated to os or a new created instance of the default duration_put<CharT> facet.

Returns: os.

template <class CharT, class Traits, class Rep, class Period>
    std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& is, duration<Rep, Period>& d)

Effects: Behaves as a formatted input function. After constructing a sentry object, if the sentry converts to true, calls to facet.get(is,std::istreambuf_iterator<CharT, Traits>(), is, err, d) where facet is the duration_get<CharT> facet associated to is or a new created instance of the default duration_get<CharT> facet.

If any step fails, calls os.setstate(std::ios_base::failbit | std::ios_base::badbit).

Returns: is

namespace boost {
  namespace chrono {
    template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
    class time_point_get;
  }
}
template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
class time_point_get: public std::locale::facet
{
public:
  typedef CharT char_type; // Type of character the facet is instantiated on.
  typedef InputIterator iter_type; // Type of iterator used to scan the character buffer.

  explicit __time_point_get_c(size_t refs = 0);

  template <class Clock, class Duration>
  iter_type __time_point_get_get(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
      time_point<Clock, Duration> &tp, const char_type *pattern, const char_type *pat_end) const;

  template <class Clock, class Duration>
  iter_type __time_point_get_get2(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
      time_point<Clock, Duration> &tp) const;

  template <typename Rep, typename Period>
  iter_type __time_point_get_get_duration(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err,
      duration<Rep, Period>& d) const;

  template <class Clock>
  iter_type __time_point_get_get_epoch(iter_type i, iter_type e, std::ios_base& is, std::ios_base::iostate& err) const;

  static std::locale::id id; // Unique identifier for this type of facet.

  __time_point_get_d();
};

time_point_get is used to parse a character sequence, extracting the duration and the epoch into a class time_point.

The pattern can contain the format specifiers %d and %e in any order.

User confirmation is required for reliable parsing of user-entered durations, but machine-generated formats can be parsed reliably. This allows parsers to be aggressive about interpreting user variations on standard formats.

If the end iterator is reached during parsing the member function sets std::ios_base::eofbit in err.

explicit time_point_get(size_t refs);

Constructs a __time_point_get facet.

Parameters:

  • refs: references

Effects: Constructs a duration_put facet. If the refs argument is 0 then destruction of the object is delegated to the locale, or locales, containing it. This allows the user to ignore lifetime management issues. On the other had, if refs is 1 then the object must be explicitly deleted; locale will not do so. In this case, the object can be maintained across the lifetime of multiple locales.

~time_point_get();

Effects: Destroy the facet.

template <class Clock, class Duration>
iter_type get(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err,
    time_point<Clock, Duration> &tp, const char_type *pattern, const char_type *pat_end) const;

Parameters:

  • s: start input stream iterator
  • end: end input stream iterator
  • ios: a reference to a ios_base
  • err: the ios_base::iostate
  • tp: the time_point
  • pattern: begin of the formatting pattern
  • pat_end: end of the formatting pattern

Requires: [pattern,pat_end) must be a valid range.

Effects:: The function starts by evaluating err = std::ios_base::goodbit. It then enters a loop, reading zero or more characters from s at each iteration. Unless otherwise specified below, the loop terminates when the first of the following conditions holds:

  • The expression pattern == pat_end evaluates to true.
  • The expression err == std::ios_base::goodbit evaluates to false.
  • The expression s == end evaluates to true, in which case the function evaluates err = std::ios_base::eofbit | std::ios_base::failbit.
  • The next element of pattern is equal to '%', followed by a conversion specifier character, the functions get_duration or get_epoch are called depending on whether the format is 'd' or 'e'. If the number of elements in the range [pattern,pat_end) is not sufficient to unambiguously determine whether the conversion specification is complete and valid, the function evaluates err |= std::ios_base::failbit. Otherwise, the function evaluates s = do_get(s, end, ios, err, d). If err == std::ios_base::goodbit holds after the evaluation of the expression, the function increments pattern to point just past the end of the conversion specification and continues looping.
  • The expression isspace(*pattern, ios.getloc()) evaluates to true, in which case the function first increments pattern until pattern == pat_end || !isspace(*pattern, ios.getloc()) evaluates to true, then advances s until s == end || !isspace(*s, ios.getloc()) is true, and finally resumes looping.
  • The next character read from s matches the element pointed to by pattern in a case-insensitive comparison, in which case the function evaluates ++pattern, ++s and continues looping. Otherwise, the function evaluates err = std::ios_base::failbit.

Returns: An iterator pointing just beyond the last character that can be determined to be part of a valid time_point.

template <class Clock, class Duration>
iter_type get(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err,
    time_point<Clock, Duration> &tp) const;

Parameters:

  • s: start input stream iterator
  • end: end input stream iterator
  • ios: a reference to a ios_base
  • err: the ios_base::iostate
  • tp: the time_point

Effects: Stores the duration pattern from the duration_unit facet in let say str. Last as if

return get(s, end, ios, err, ios, d, str.data(), str.data() + str.size());

Returns: An iterator pointing just beyond the last character that can be determined to be part of a valid name.

template <typename Rep, typename Period>
iter_type get_duration(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err,
    duration<Rep, Period>& d) const;

Effects: As if

return facet.get(s, end, ios, err, d);

where facet is either the duration_get facet associated to the ios or an instance of the default duration_get facet.

Returns: An iterator pointing just beyond the last character that can be determined to be part of a valid duration.

template <class Clock>
iter_type get_epoch(iter_type s, iter_type end, std::ios_base& ios, std::ios_base::iostate& err) const;

Effects: Let facet be the __time_point_units facet associated to ios or a new instance of the default __time_point_units_default facet. Let epoch be the epoch string associated to the Clock using this facet. Scans s to match epoch or end is reached.

If not match before the end is reached std::ios_base::failbit is set in err. If end is reached std::ios_base::failbit is set in err.

Returns: An iterator pointing just beyond the last character that can be determined to be part of a epoch.

namespace boost {
  namespace chrono {
    template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
    class time_point_put;
  }
}

The __time_point_put facet provides facilities for formatted output of time_point values. The member function of __time_point_put take a time_point and format it into character string representation.

tparam ChatT a character type tparam OutputIterator a model of OutputIterator

template <class CharT, class OutputIterator = std::ostreambuf_iterator<CharT> >
class time_point_put: public std::locale::facet
{
public:
  typedef CharT char_type; // Type of character the facet is instantiated on.
  typedef std::basic_string<CharT> string_type; // Type of character string passed to member functions.
  typedef OutputIterator iter_type; // Type of iterator used to write in the character buffer.

  explicit time_point_put(size_t refs = 0);
  ~time_point_put();

  template <class Clock, class Duration>
  iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point<Clock, Duration> const& tp, const CharT* pattern,
      const CharT* pat_end) const;
  template <class Clock, class Duration>
  iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point<Clock, Duration> const& tp) const;
  template <typename Rep, typename Period>
  iter_type put_duration(iter_type i, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const;
  template <typename Clock>
  iter_type put_epoch(iter_type i, std::ios_base& os) const;

  static std::locale::id id; // Unique identifier for this type of facet.


};
explicit time_point_put(size_t refs = 0);

Construct a time_point_put facet.

Effects: Construct a time_point_put facet. If the refs argument is 0 then destruction of the object is delegated to the locale, or locales, containing it. This allows the user to ignore lifetime management issues. On the other had, if refs is 1 then the object must be explicitly deleted; the locale will not do so. In this case, the object can be maintained across the lifetime of multiple locales.

Parameters:

  • refs: references
template <class Clock, class Duration>
iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point<Clock, Duration> const& tp, const CharT* pattern,
    const CharT* pat_end) const;

Parameters:

  • i: an output stream iterator
  • ios: a reference to a ios_base
  • fill: the character used as filler
  • tp: the time_point
  • pattern: begin of the formatting pattern
  • pat_end: end of the formatting pattern

Effects: Steps through the sequence from pattern to pat_end, identifying characters that are part of a pattern sequence. Each character that is not part of a pattern sequence is written to s immediately, and each pattern sequence, as it is identified, results in a call to __put_duration or __put_epoch; thus, pattern elements and other characters are interleaved in the output in the order in which they appear in the pattern. Pattern sequences are identified by converting each character c to a char value as if by ct.narrow(c,0), where ct is a reference to ctype<charT> obtained from ios.getloc(). The first character of each sequence is equal to '%', followed by a pattern specifier character spec, which can be 'd' for the duration value or 'e' for the epoch. For each valid pattern sequence identified, calls put_duration(s, ios, fill, tp.time_since_epoch()) or put_epoch(s, ios).

Returns: An iterator pointing immediately after the last character produced.

template <class Clock, class Duration>
iter_type put(iter_type i, std::ios_base& ios, char_type fill, time_point<Clock, Duration> const& tp) const;

Parameters:

  • i: an output stream iterator
  • ios: a reference to a ios_base
  • fill: the character used as filler
  • tp: the time_point
  • tern: begin of the formatting pattern
  • pat_end: end of the formatting pattern

Effects: Stores the time_point pattern from the __time_point_unit facet in let say str. Last as if

return put(s, ios, fill, tp, str.data(), str.data() + str.size());

Returns: An iterator pointing immediately after the last character produced.

template <typename Rep, typename Period>
iter_type put_duration(iter_type i, std::ios_base& ios, char_type fill, duration<Rep, Period> const& d) const;

Parameters:

  • i: an output stream iterator
  • ios: a reference to a ios_base
  • fill: the character used as filler
  • d: the duration

Effects: As if facet.put(s, ios, fill, d) where facet is the duration_put<CharT> facet associated to the ios or a new instance of duration_put<CharT>.

Returns: An iterator pointing immediately after the last character produced.

template <typename Clock>
iter_type put_epoch(iter_type i, std::ios_base& os) const;

Parameters:

  • i: an output stream iterator
  • ios: a reference to a ios_base

Effects: As if

string_type str = facet.template get_epoch<Clock>();
s=std::copy(str.begin(), str.end(), s);

where facet is the __time_point_units<CharT> facet associated to the ios or a new instance of __time_point_units_default<CharT>.

Returns: s, iterator pointing immediately after the last character produced.

namespace boost {
  namespace chrono {
    template <typename CharT=char>
    class time_point_units;
    template <typename CharT=char>
    class time_point_units_default,
  }
}

__time_point_units facet gives useful information about the time_point pattern, the text associated to a time_point's epoch,

  template <typename CharT=char>
  class time_point_units: public std::locale::facet
  {
  public:
    typedef CharT char_type; // Type of character the facet is instantiated on.
    typedef std::basic_string<char_type> string_type; // Type of character string used by member functions.
    static std::locale::id id; // Unique identifier for this type of facet.

    explicit time_point_units(size_t refs = 0);

    virtual string_type get_pattern() const =0;
    template <typename Clock>
    string_type get_epoch() const;

  protected:
    virtual ~time_point_units();
    virtual string_type do_get_epoch(system_clock) const=0;
    virtual string_type do_get_epoch(steady_clock) const=0;

#if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
    virtual string_type do_get_epoch(process_real_cpu_clock) const=0;
    virtual string_type do_get_epoch(process_user_cpu_clock) const=0;
    virtual string_type do_get_epoch(process_system_cpu_clock) const=0;
    virtual string_type do_get_epoch(process_cpu_clock) const=0;
#endif
#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
    virtual string_type do_get_epoch(thread_clock) const=0;
#endif

  };
explicit time_point_units(size_t refs = 0);

Construct a __time_point_units facet.

Parameters:

  • refs: references

Effects: Construct a __time_point_units facet. If the refs argument is 0 then destruction of the object is delegated to the locale, or locales, containing it. This allows the user to ignore lifetime management issues. On the other had, if refsv is 1` then the object must be explicitly deleted; the locale will not do so. In this case, the object can be maintained across the lifetime of multiple locales.

virtual string_type get_pattern() const =0;

Returns: the pattern to be used by default.

template <typename Clock>
string_type get_epoch() const;

Returns: the epoch associated to the clock Clock calling __do_get_epoch(Clock())

virtual ~time_point_units();

Destroy the facet.

virtual string_type do_get_epoch(system_clock) const=0;

Parameters:

Returns: The epoch string associated to the system_clock.

virtual string_type do_get_epoch(steady_clock) const=0;

Parameters:

Returns: The epoch string associated to the steady_clock.

#if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
    virtual string_type do_get_epoch(process_real_cpu_clock) const=0;
#endif

Parameters:

Returns: The epoch string associated to the process_real_cpu_clock.

#if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
    virtual string_type do_get_epoch(process_user_cpu_clock) const=0;
#endif

Parameters:

Returns: The epoch string associated to the process_user_cpu_clock.

#if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
    virtual string_type do_get_epoch(process_system_cpu_clock) const=0;
#endif

Parameters:

Returns: The epoch string associated to the process_user_cpu_clock.

#if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
    virtual string_type do_get_epoch(process_cpu_clock) const=0;
#endif

Parameters:

Returns: The epoch string associated to the process_cpu_clock.

#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
    virtual string_type do_get_epoch(thread_clock) const=0;
#endif

Parameters:

Returns: The epoch string associated to the thread_clock.

  // This class is used to define the strings for the default English
  template <typename CharT=char>
  class time_point_units_default: public time_point_units<CharT>
  {
  public:
    typedef CharT char_type; // Type of character the facet is instantiated on.
    typedef std::basic_string<char_type> string_type; // Type of character string used by member functions.

    explicit time_point_units_default(size_t refs = 0);
    ~time_point_units_default();

    /**
     * __returns the default pattern "%d%e".
     */
    string_type get_pattern() const;

  protected:
    /**
     * [param c a dummy instance of __system_clock.
     * __returns The epoch string returned by `clock_string<system_clock,CharT>::since()`.
     */
    string_type do_get_epoch(system_clock ) const;
    /**
     * [param c a dummy instance of __steady_clock.
     * __returns The epoch string returned by `clock_string<steady_clock,CharT>::since()`.
     */
    string_type do_get_epoch(steady_clock ) const;
#if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
    /**
     * [param c a dummy instance of __process_real_cpu_clock.
     * __returns The epoch string returned by `clock_string<process_real_cpu_clock,CharT>::since()`.
     */
    string_type do_get_epoch(process_real_cpu_clock ) const;
    /**
     * [param c a dummy instance of __process_user_cpu_clock.
     * __returns The epoch string returned by `clock_string<process_user_cpu_clock,CharT>::since()`.
     */
    string_type do_get_epoch(process_user_cpu_clock ) const;
    /**
     * [param c a dummy instance of __process_system_cpu_clock.
     * __returns The epoch string returned by `clock_string<process_system_cpu_clock,CharT>::since()`.
     */
    string_type do_get_epoch(process_system_cpu_clock ) const;
    /**
     * [param c a dummy instance of __process_cpu_clock.
     * __returns The epoch string returned by `clock_string<process_cpu_clock,CharT>::since()`.
     */
    string_type do_get_epoch(process_cpu_clock ) const;

#endif
#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
    /**
     * [param c a dummy instance of __thread_clock.
     * __returns The epoch string returned by `clock_string<thread_clock,CharT>::since()`.
     */
    string_type do_get_epoch(thread_clock ) const;
#endif

  };
namespace boost {
namespace chrono {


    // manipulators

    unspecified time_fmt(timezone tz);
    template<class CharT>
    unspecified time_fmt(timezone tz, basic_string<CharT> f);
    template<class CharT>
    unspecified time_fmt(timezone tz, const CharT* f);

    // i/o state savers

    template<typename CharT = char, typename Traits = std::char_traits<CharT> >
    struct timezone_io_saver
    {
      typedef std::basic_ios<CharT, Traits> state_type;
      typedef timezone aspect_type;

      explicit timezone_io_saver(state_type &s);
      timezone_io_saver(state_type &s, aspect_type new_value);
      ~timezone_io_saver();
      void timezone_io_saver__restore();
    };

    template<typename CharT = char, typename Traits = std::char_traits<CharT> >
    struct time_fmt_io_saver
    {
      typedef std::basic_ios<CharT, Traits> state_type;

      explicit time_fmt_io_saver(state_type &s);
      time_fmt_io_saver(state_type &s, basic_string<CharT> const& new_value);
      ~ time_fmt_io_saver();
      void restore();
    };

    // system_clock I/O

    template <class CharT, class Traits, class Duration>
      basic_ostream<CharT, Traits>&
      operator<<(basic_ostream<CharT, Traits>& os,
                 const time_point<system_clock, Duration>& tp);

    template <class CharT, class Traits, class Duration>
      basic_istream<CharT, Traits>&
      operator>>(basic_istream<CharT, Traits>& is,
                 time_point<system_clock, Duration>& tp);

    // Other Clocks I/O

    template <class CharT, class Traits, class Clock, class Duration>
        std::basic_ostream<CharT, Traits>&
        operator<<(std::basic_ostream<CharT, Traits>& os,
               const time_point<Clock, Duration>& tp);

    template <class CharT, class Traits, class Clock, class Duration>
        std::basic_istream<CharT, Traits>&
        operator>>(std::basic_istream<CharT, Traits>& is,
               time_point<Clock, Duration>& tp);

}
}
unspecified time_fmt(timezone tz);

Returns:: An unspecified object that when streamed to a basic_ostream<CharT, Traits> or basic_istream<CharT, Traits> s will have the effects of:

set_timezone(s, tz);
template<class CharT>
  unspecified time_fmt(timezone tz, basic_string<CharT> f);
template<class CharT>
  unspecified time_fmt(timezone tz, const CharT* f);

Returns:: An unspecified object that when streamed to a basic_ostream<CharT, Traits> or basic_istream<CharT, Traits> s will have the effects of:

set_timezone(s, tz);
set_time_fmt<CharT>(s, f);
template <class CharT, class Traits, class Duration>
    std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
           const time_point<system_clock, Duration>& tp);

Effects: Behaves as a formatted output function. After constructing a sentry object, if the sentry converts to true, sets a local variable tz of type timezone to get_timezone(os). Additionally the format string obtained with get_time_fmt() is recorded as a pair of const CharT*. If the stream has no time_punct facet, then this pair of const CharT* that represent an empty range.

Next tp is converted to a time_t, and this time_t is converted to a tm. The conversion to tm use gmtime (when available) if the timezone is utc, else it use localtime (if available).

Using the std::time_put facet stored in os, this inserter writes characters to the stream using the tm and the formating string stored in the time_punct facet, unless that facet was missing, or unless it provided an empty string.

If the formatting string is empty, then output as follows:

First output tm using "%F %H:%M:"

Next output a double representing the number of seconds stored in the tm plus the fractional seconds represented in tp. The format shall be ios::fixed and the precision shall be sufficient to represent system_clock::duration exactly (e.g. if system_clock::period is micro, the precision should be sufficient to output six digits following the decimal point). If the number of seconds is less than 10, the output shall be prefixed by '0'.

Finally if tz is local, output the tm using the pattern " %z". Else append the sequence " +0000" to the stream.

If any step fails, calls os.setstate(ios_base::failbit | ios_base::badbit).

Returns: os

template <class CharT, class Traits, class Duration>
    std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& is,
           time_point<system_clock, Duration>& tp);

Effects: Behaves as a formatted input function. After constructing a sentry object, if the sentry converts to true, obtains a std::time_get facet from is, and obtains a formatting string in the same manner as described for insertion operator. Extract a tm using the supplied formatting string, or if empty, defaulted as described for the insertion operator. Note that extraction does not use the timezone data stored in the is for the defaulted string as the timezone information is stored in the stream.

Any time_point<system_clock, Duration> inserted, and then extracted should result in an equal time_point<system_clock, Duration>, excepting any precision that did not get inserted.

Example:

void test(std::chrono::system_clock::time_point tp)
{
    std::stringstream s;
    s << tp;
    boost::chrono::system_clock::time_point tp2;
    s >> tp2;
    assert(tp == tp2);
}

Returns: is

template <class CharT, class Traits, class Clock, class Duration>
    std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
           const time_point<Clock, Duration>& tp);

Effects: Behaves as a formatted output function. After constructing a sentry object, if the sentry converts to true, calls to facet.put(os,os,os.fill(),tp) where facet is the time_point_put<CharT> facet associated to os or a new created instance of the default time_point_put<CharT> facet.

Returns: os.

Example:

22644271279698 nanoseconds since boot
template <class CharT, class Traits, class Clock, class Duration>
    std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& is,
           time_point<Clock, Duration>& tp);

Extracts tp from the stream is.

Effects: Behaves as a formatted input function. After constructing a sentry object, if the sentry converts to true, calls to facet.get(is,std::istreambuf_iterator<CharT, Traits>(), is, err, tp) where facet is the time_point_get<CharT> facet associated to is or a new created instance of the default time_point_get<CharT> facet.

If any step fails, calls os.setstate(std::ios_base::failbit | std::ios_base::badbit).

Returns: is.

namespace boost { namespace chrono {
  template <class To, class Rep, class Period>
  To floor(const duration<Rep, Period>& d);
} }

This function round down the given parameter.

namespace boost { namespace chrono {
  template <class To, class Rep, class Period>
  To round(const duration<Rep, Period>& d);
} }

This function round to nearest, to even on tie the given parameter.

namespace boost { namespace chrono {
  template <class To, class Rep, class Period>
  To ceil(const duration<Rep, Period>& d);
} }

This function round up the given parameter.

Knowing how long a program takes to execute is useful in both test and production environments. It is also helpful if such timing information is broken down into real (wall clock) time, CPU time spent by the user, and CPU time spent by the operating system servicing user requests.

Process clocks don't include the time spent by the child process.

#define BOOST_CHRONO_HAS_PROCESS_CLOCKS

namespace boost { namespace chrono {

    class process_real_cpu_clock;
    class process_user_cpu_clock;
    class process_system_cpu_clock;
    class process_cpu_clock;

    template <typename Rep>
    struct process_times;
    template <class CharT, class Traits, class Rep>
    std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
            process_times<Rep> const& rhs);

    template <class CharT, class Traits, class Rep>
    std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& is,
            process_times<Rep> const& rhs);

    template <class Rep>
    struct duration_values<process_times<Rep> >;

    template <class CharT>
    struct clock_string<process_real_cpu_clock, CharT>;
    struct clock_string<process_user_cpu_clock, CharT>;
    struct clock_string<process_system_cpu_clock, CharT>;
    struct clock_string<process_cpu_clock, CharT>;

} }
namespace std {
    template <class Rep>
    class numeric_limits<boost::chrono::process_times<Rep> >;
}

This macro is defined if the platform supports process clocks.

process_real_cpu_clock satisfy the Clock requirements.

process_real_cpu_clock class provides access to the real process wall-clock steady clock, i.e. the real CPU-time clock of the calling process. The process relative current time can be obtained by calling process_real_cpu_clock::now().

class process_real_cpu_clock {
public:
    typedef nanoseconds                          duration;
    typedef duration::rep                        rep;
    typedef duration::period                     period;
    typedef chrono::time_point<process_real_cpu_clock>    time_point;
    static constexpr bool is_steady =            true;

    static time_point now(  ) noexcept;
    static time_point now( system::error_code & ec );
};

process_user_cpu_clock satisfy the Clock requirements.

process_user_cpu_clock class provides access to the user CPU-time steady clock of the calling process. The process relative user current time can be obtained by calling process_user_cpu_clock::now().

class process_user_cpu_clock {
public:
    typedef nanoseconds                          duration;
    typedef duration::rep                        rep;
    typedef duration::period                     period;
    typedef chrono::time_point<process_user_cpu_clock>    time_point;
    static constexpr bool is_steady =            true;

    static time_point now(  ) noexcept;
    static time_point now( system::error_code & ec );
};

process_system_cpu_clock satisfy the Clock requirements.

process_system_cpu_clock class provides access to the system CPU-time steady clockof the calling process. The process relative system current time can be obtained by calling process_system_cpu_clock::now().

class process_system_cpu_clock {
public:
    typedef nanoseconds                          duration;
    typedef duration::rep                        rep;
    typedef duration::period                     period;
    typedef chrono::time_point<process_system_cpu_clock>    time_point;
    static constexpr bool is_steady =            true;

    static time_point now(  ) noexcept;
    static time_point now( system::error_code & ec );
};

process_cpu_clock can be considered as a tuple<process_real_cpu_clock, process_user_cpu_clock, process_system_cpu_clock>.

process_cpu_clock provides a thin wrapper around the operating system's process time API. For POSIX-like systems, that's the times() function, while for Windows, it's the GetProcessTimes() function.

The process relative real, user and system current time can be obtained at once by calling process_clocks::now().

class process_cpu_clock
{
public:
    typedef process_times<nanoseconds::rep> times ;

    typedef duration<times,  nano>                  duration;
    typedef duration::rep                           rep;
    typedef duration::period                        period;
    typedef chrono::time_point<process_cpu_clock>   time_point;
    static constexpr bool is_steady =               true;

    static time_point now(  ) noexcept;
    static time_point now( system::error_code & ec );
};

This class is the representation of the process_cpu_clock::duration class. As such it needs to implements the arithmetic operators.

template <typename Rep>
struct process_times : arithmetic<process_times<Rep>,
    multiplicative<process_times<Rep>, Rep,
    less_than_comparable<process_times<Rep> > > >
{
    Rep real;    // real (i.e wall clock) time
    Rep user;    // user cpu time
    Rep system;  // system cpu time

    times();
    times(
        process_real_cpu_clock::rep r,
        process_user_cpu_clock::rep u,
        process_system_cpu_clock::rep s);

    template <typename Rep2>
    explicit process_times(
        Rep2 r);
    template <typename Rep2>
    explicit process_times(
        process_times<Rep2> const& rhs);
    operator rep() const;

    bool operator==(process_times const& rhs);
    template <typename Rep2>
    bool operator==(process_times<Rep2> const& rhs);

    times operator+=(process_times const& rhs);
    times operator-=(process_times const& rhs);
    times operator*=(process_times const& rhs);
    times operator/=(process_times const& rhs);
    bool operator<(process_times const & rhs) const;
};
template <class CharT, class Traits, class Rep>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os,
        process_times<Rep> const& rhs);

Effects: Output each part separated by ';' and sourrounded by '{', '}'.

Throws: None.

template <class CharT, class Traits, class Rep>
std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is,
        process_times<Rep> const& rhs);

Effects: overrides the value of rhs if the input stream has the format "{r;u;s}". Otherwise, set the input stream state as failbit | eofbit.

Throws: None.

template <class Rep>
struct duration_values<process_times<Rep> >
{
    static process_times<Rep> zero();
    static process_times<Rep> max();
    static process_times<Rep> min();
};

The times specific functions zero(), max() and min() uses the relative functions on the representation of each component.

template <class CharT>
struct clock_string<process_real_cpu_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};

clock_string<>::name() returns "process_real_cpu_clock".

clock_string<>::since() returns " since process start-up"

template <class CharT>
struct clock_string<process_user_cpu_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};

clock_string<>::name() returns "process_user_cpu_clock".

clock_string<>::since() returns " since process start-up"

template <class CharT>
struct clock_string<process_system_cpu_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};

clock_string<>::name() returns "process_system_cpu_clock".

clock_string<>::since() returns " since process start-up"

template <class CharT>
struct clock_string<process_cpu_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};

clock_string<>::name() returns "process_cpu_clock".

clock_string<>::since() returns " since process start-up"

namespace std {
    template <>
    class numeric_limits<boost::chrono::process_times<Rep>> {
        typedef boost::chrono::process_times<Rep> Res;

    public:
        static const bool is_specialized = true;
        static Res min();
        static Res max();
        static Res lowest();
        static const int digits;
        static const int digits10;
        static const bool is_signed = false;
        static const bool is_integer = true;
        static const bool is_exact = true;
        static const int radix = 0;
    };
}

The process_times<Rep> specialization functions min(), max() and lowest() uses the relative functions on the representation of each component.

Notes

  • min() returns the tuple of mins.
  • max() returns the tuple of maxs.
  • lowest() returns the tuple of lowests.
  • digits is the sum of (binary) digits.
  • digits10 is the sum of digits10s.

Knowing the time a thread takes to execute is useful in both test and production environments.

#define BOOST_CHRONO_HAS_THREAD_CLOCK
#define BOOST_CHRONO_THREAD_CLOCK_IS_STEADY
namespace boost { namespace chrono {

    class thread_clock;
    template <class CharT>
    struct clock_string<thread_clock, CharT>;

} }

This macro is defined if the platform supports thread clocks.

This macro is defined if the platform has a thread clock. Its value is true if it is steady and false otherwise.

thread_clock satisfy the Clock requirements.

thread_clock class provides access to the real thread wall-clock, i.e. the real CPU-time clock of the calling thread. The thread relative current time can be obtained by calling thread_clock::now().

class thread_clock {
public:
    typedef nanoseconds                          duration;
    typedef duration::rep                        rep;
    typedef duration::period                     period;
    typedef chrono::time_point<thread_clock>     time_point;
    static constexpr bool is_steady =            BOOST_CHRONO_THREAD_CLOCK_IS_STEADY;

    static time_point now(  ) noexcept;
    static time_point now( system::error_code & ec );
};
#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
template <class CharT>
struct clock_string<thread_clock, CharT>
{
    static std::basic_string<CharT> name();
    static std::basic_string<CharT> since();
};
#endif

clock_string<>::name() returns "thread_clock".

clock_string<>::since() returns " since thread start-up"


PrevUpHomeNext