Boost C++ 库

世界上最受推崇和设计精良的 C++ 库项目之一。 Herb SutterAndrei AlexandrescuC++ Coding Standards

参考 - Boost C++ 函数库
PrevUpHomeNext

由于某些编译器不支持 constexpr,因此在代码中用 BOOST_CONSTEXPR 替换 constexpr 函数,用 BOOST_STATIC_CONSTEXPR 替换结构/类静态字段。对于 noexcept,同样用 noexcept 替换。

文档不使用这些宏。

包含所有 chrono 头文件。

#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 <boost/chrono/chrono.hpp>

仅包含标准文件。

#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>

目前,相对于 C++11 标准,没有已知限制。

当前实现另外提供了

当定义了 BOOST_NO_CXX11_STATIC_ASSERT 时,用户可以选择静态断言的报告方式。定义

  • BOOST_CHRONO_USES_STATIC_ASSERT:如果您想使用 Boost.StaticAssert,请定义它。
  • BOOST_CHRONO_USES_MPL_ASSERT:如果您想使用 Boost.MPL 静态断言,请定义它。
  • BOOST_CHRONO_USES_ARRAY_ASSERT:如果您想使用内部静态断言,请定义它。

默认行为如同定义了 BOOST_CHRONO_USES_ARRAY_ASSERT

当未定义 BOOST_CHRONO_USES_MPL_ASSERT 时,以下符号定义为

#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"

根据所使用的静态断言系统,您将通过符号或文本获得关于失败断言的提示。

当定义了 BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 时,该库不提供混合错误处理原型

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

这使得它更接近标准,并避免了 Boost.System 依赖,从而使 Boost.Chrono 成为一个仅头文件库。

默认情况下,定义了 BOOST_CHRONO_PROVIDE_HYBRID_ERROR_HANDLING

当定义了 BOOST_CHRONO_HEADER_ONLY 时,该库是仅头文件的。

如果另外定义了 BOOST_USE_WINDOWS_H,则包含 <windows.h>,否则使用 boost/detail/win 中的文件来减少包含 <windows.h> 的影响。

但是,您需要定义 BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING 或链接 Boost.System。

版本 2.0.0 弃用了之前的 I/O 功能。

然而,默认版本始终是版本 1。如果您不想包含已弃用的功能,可以定义 BOOST_CHRONO_DONT_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0

BOOST_CHRONO_VERSION 定义了 Boost.Chrono 的版本。默认版本为 1。在这种情况下,除非另有要求,否则会定义以下重大更改或扩展宏

  • BOOST_CHRONO_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0

用户可以通过将 BOOST_CHRONO_VERSION 定义为 2 来请求版本 2。在这种情况下,除非另有要求,否则会定义以下重大更改或扩展宏

  • 重大更改 BOOST_CHRONO_DONT_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0

从 Boost 1.55 开始,BOOST_CHRONO_VERSION 的默认值将更改为 2。

此文件包含 duration 特定的类和非成员函数。

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 frequency 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> {};

duration 模板使用 treat_as_floating_point 特征来确定一个具有一个滴答周期的 duration 是否可以转换为另一个具有不同滴答周期的 duration。如果 treat_as_floating_point<Rep>::valuetrue,则 Rep 是浮点类型,并且 duration 之间允许隐式转换。否则,隐式可转换性取决于 duration 的滴答周期。如果 Rep 是模拟浮点类型的类类型,那么 Rep 的作者可以专门化 treat_as_floating_point,以便 duration 将此 Rep 视为浮点类型。否则,假定 Rep 是整数类型或模拟整数类型的类。

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

duration 模板使用 duration_values 特征来构造 duration 表示(Rep)的特殊值。这是因为表示可能是一个具有某些其他实现来返回这些特殊值的行为的类类型。在这种情况下,该类类型的作者应该专门化 duration_values 以返回指示的值。

static constexpr Rep zero();

返回: Rep(0)注意: 指定 Rep(0) 而不是 Rep(),因为 Rep() 可能有其他含义,例如未初始化值。

备注: 返回的值对应于加法标识。

static constexpr Rep max();

返回: numeric_limits<Rep>::max()

备注: 返回的值大于 zero()。

static constexpr Rep min();

返回: numeric_limits<Rep>::lowest()

备注: 返回的值小于或等于 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;
};

common_type 特化的 duration 的周期是 Period1Period2 的最大公约数。这可以通过形成 Period1::numPeriod2::num 的最大公约数,以及 Period1::denPeriod2::den 的最小公倍数的 ratio 来计算。

注意: 类型别名是具有最大可能滴答周期的 duration,其中两个 duration 参数都可以转换为它而无需进行除法运算。此类型的表示旨在能够容纳此转换产生的任何值,除了涉及浮点 duration 时的舍入误差(但不是截断误差)。

一个 duration 测量两个时间点(time_point)之间的时间。一个 duration 具有一个表示存储滴答计数和滴答周期的字段。滴答周期是指从一个滴答到另一个滴答的时间量,单位为秒。它以有理数常量的形式表示,使用 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 必须是算术类型或模拟算术类型的类,否则会产生编译诊断。如果使用 Rep 类型为 durationduration 进行实例化,则会发出编译诊断。

Period 必须是 ratio 的实例化,否则会产生编译诊断。

Period::num 必须为正,否则会产生编译诊断。

示例

  • duration<long, ratio<60> > 存储分钟数,使用 long。
  • duration<long long, milli> 存储毫秒数,使用 long long。
  • duration<double, ratio<1, 30> > 存储使用 double 的计数,滴答周期为 1/30 秒(滴答频率为 30 Hz)。

duration 的以下成员不抛出异常,除非表示的指示操作抛出异常。

constexpr duration();

效果: 在 C++98 中,构造一个 duration 类型的对象,从 duration_values<rep>::zero() 初始化,否则 duration 未初始化。

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

备注: Rep2 可隐式转换为 rep,并且

  • treat_as_floating_point<rep>::valuetrue,或者
  • !treat_as_floating_point<rep>::value && !treat_as_floating_point<Rep2>::valuetrue

如果这些约束不满足,此构造函数将不参与重载解析。 注意: 此要求可防止用浮点表示构造基于整数的 duration。此类构造很容易导致对 duration 值的混淆。

示例

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

效果: 构造一个 duration 类型的对象。

后置条件: count() == static_cast<rep>(r)

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

备注: treat_as_floating_point<rep>::value,或 ratio_divide<Period2, period>::type::den == 1,否则此构造函数将不参与重载解析。 注意:此要求可防止在整数基 duration 之间转换时发生隐式截断误差。此类构造可能导致对 duration 值的混淆。

示例

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

效果: 构造一个 duration 类型的对象,通过 duration_cast<duration>(d).count() 构造 rep_

constexpr rep count() const;

返回: rep_

constexpr duration operator+() const;

返回: *this

constexpr duration operator-() const;

返回: duration(-rep_)

duration& operator++();

效果: ++rep_

返回: *this

duration operator++(int);

返回: duration(rep_++)

duration& operator--();

效果: --rep_

返回: *this

duration operator--(int);

返回: duration(rep_--)

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

效果: rep_ += d.count()

返回: *this

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

效果: rep_ -= d.count()

返回: *this

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

效果: rep_ %= d.count()

返回: *this

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

效果: rep_ *= rhs

返回: *this

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

效果: rep_ /= rhs

返回: *this

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

效果: rep_ %= rhs

返回: *this

static constexpr duration zero();

返回: duration(duration_values<rep>::zero())

static constexpr duration min();

返回: duration(duration_values<rep>::min()).

static constexpr duration max();

返回: 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);

返回: CD(CD(lhs).count() + CD(rhs).count()),其中 CD 是返回值的类型。

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);

返回: CD(CD(lhs).count() - CD(rhs).count()),其中 CD 是返回值的类型。

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);

要求:CR 表示 Rep1Rep2common_type。此函数不参与重载解析,除非 Rep1Rep2 均可隐式转换为 CR

返回: CD(CD(d).count() * s),其中 CD 是返回值的类型。

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);

要求:CR 表示 Rep1Rep2common_type。此函数不参与重载解析,除非 Rep1Rep2 均可隐式转换为 CR

返回: 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);

要求:CR 表示 Rep1Rep2common_type。此函数不参与重载解析,除非 Rep1Rep2 均可隐式转换为 CR,并且 Rep2 不是 duration 的实例化。

返回: CD(CD(d).count() / s),其中 CD 是返回值的类型。

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);

备注:CD 表示两个 duration 参数的 common_type返回: 返回 CD(lhs).count() / CD(rhs).count()

仅当定义了 BOOST_CHRONO_EXTENSIONS 时才包含。

此重载可用于获取由 Rep1 计数的事件的频率。

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

备注:CR 表示 Rep1Rep2common_type。此函数不参与重载解析,除非 Rep1Rep2 均可隐式转换为 CR,并且 Rep1 不是 duration 的实例化。令 CD 表示 duration<CR,Period>。

返回: CR(s)/CD(d).count(),其中 CD 是返回值的类型。

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);

备注:CR 表示 Rep1Rep2common_type。此函数不参与重载解析,除非 Rep2 可隐式转换为 CR 且 Rep2 不是 duration 的实例化。

返回: CD(CD(d).count() % s) 其中 CD 是返回值的类型。

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);

备注: 此函数不参与重载解析,除非

返回: CD(CD(lhs).count() % CD(rhs).count()) 其中 CD 是返回值的类型。

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

返回:CD 代表两个 duration 参数的 common_type

返回: 返回 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);

返回: !(lhs == rhs)

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

返回:CD 代表两个 duration 参数的 common_type。返回 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);

返回: !(rhs < lhs)

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

返回: rhs < lhs

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

返回: !(lhs < rhs)

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

要求: 此函数不参与重载解析,除非 ToDurationduration 的实例化。

返回: 形成 CF,它是一个 ratio,由 ratio_divide<Period, typename ToDuration::period>::type 得到。令 CRToDuration::repRepintmax_tcommon_type

  • 如果 CF::num == 1CF::den == 1,则返回 ToDuration(static_cast<typename ToDuration::rep>(d.count()))
  • 否则,如果 CF::num != 1CF::den == 1,则返回 ToDuration(static_cast<typename ToDuration::rep>(static_cast<CR>(d.count()) * static_cast<CR>(CF::num)))
  • 否则,如果 CF::num == 1CF::den != 1,则返回 ToDuration(static_cast<typename ToDuration::rep>(static_cast<CR>(d.count()) / static_cast<CR>(CF::den)))
  • 否则返回 ToDuration(static_cast<typename ToDuration::rep>(static_cast<CR>(d.count()) * static_cast<CR>(CF::num) / static_cast<CR>(CF::den)))

备注: 此函数不依赖于任何隐式转换。所有转换都必须通过 static_cast 完成。当可以从编译时知道可以避免乘法或除法(因为一个或多个参数为 1)时,实现会避免这些操作。所有中间计算都在尽可能宽的表示中进行,并且仅在最后一步转换为目标表示。

// 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

时钟代表由 durationtime_point 和获取当前 time_point 的函数 now() 组成的集合。时钟必须满足以下表中的要求。

在此表中,C1C2 表示 Clock 类型。t1t2 是从 C1::now() 返回的值,其中返回 t1 的调用发生在返回 t2 的调用之前,并且这两个调用都发生在 C1::time_point::max() 之前(注意:这意味着 C1 在 t1 和 t2 之间没有回绕)。

表 5.1. 时钟要求

表达式

返回类型

操作语义

C1::rep

算术类型或模拟算术类型的类。

durationtime_point 的表示类型。

C1::period

ratio

时钟的滴答周期(单位为秒)。

C1::duration

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

clockduration 类型。

C1::time_point

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

clocktime_point 类型。允许不同的时钟共享 time_point 定义,如果它们的时间点可以通过比较各自的 duration 来比较。 C1C2 必须引用相同的纪元。

C1::is_steady

constexpr bool

true 如果 t1 <= t2 始终为 true,否则为 false注意:可以向后调整的时钟不是稳定的。

C1::now()

C1::time_point

返回表示当前时间点的 time_point


Clock 模型的示例

类型 TC 满足 TrivialClock 要求,如果

  • TC 满足 Clock 要求,
  • 类型 TC::repTC::durationTC::time_point 满足 EqualityComparableLessThanComparableDefaultConstructibleCopyConstructibleCopyAssignableDestructible 以及数字类型的要求。
[Note] 注意

这意味着,特别是,这些类型的操作不会抛出异常。

  • TC::repTC::durationTC::time_point 类型的左值是可交换的,
  • 函数 TC::now() 不抛出异常且线程安全,并且
  • 类型 TC::time_point::clock 递归地满足 TrivialClock 要求。

TrivialClock 模型的示例

类型 EcC 满足 EcClock 要求,如果

  • TC 满足 TrivialClock 要求,并且
  • 它增加了 now() 接口,允许恢复内部错误码,如下表所述。
  • 新增的 now() 函数是线程安全的。

在此表中,C1 表示 EcClock 类型,ecboost::system::error_code 的实例。

表 5.2. 时钟要求

表达式

返回类型

操作语义

C1::now(ec)

C1::time_point

返回表示当前时间点的 time_pointec 将存储错误码,以防内部出现问题。

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

C1::time_point

返回表示当前时间点的 time_point。如果内部出现问题,则抛出 boost::system::system_error 异常。


Clock 模型的示例

此文件包含 time_point 特定的类和非成员函数。

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;
};

两个 time_pointcommon_type 是一个具有相同 Clock(两者具有相同的 Clock)的 time_point,以及两个 durationcommon_type

一个 time_point 代表相对于特定时钟的一个时间点。

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 必须满足 Clock 要求。

Duration 必须是 duration 的实例化,否则会产生编译诊断。

constexpr time_point();

效果: 构造一个 time_point 对象,用 duration::zero() 初始化 d_。此 time_point 代表纪元。

constexpr time_point(const duration& d);

效果: 构造一个 time_point 对象,用 d 初始化 d_。此 time_point 代表纪元 + d

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

要求: 除非 Duration2 可以隐式转换为 duration,否则此函数不会参与重载解析。

效果: 构造一个 time_point 对象,用 t.time_since_epoch() 初始化 d_

constexpr duration time_since_epoch() const;

返回: d_

constexpr time_point operator+() const;

返回: *this

constexpr time_point operator-() const;

返回: time_point(-d_)

time_point& operator++();

效果: ++d_

返回: *this

time_point operator++(int);

返回: time_point(d_++)

time_point& operator--();

效果: --d_

返回: *this

time_point operator--(int);

返回: time_point(d_--)

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

效果: d_ += duration(r)

返回: *this

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

效果: d_ -= duration(r)

返回: *this

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

效果: d_ += d

返回: *this

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

效果: d_ -= d

返回: *this

static constexpr time_point min();

返回: time_point(duration::min())

static constexpr time_point max();

返回: 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);

返回: CT(lhs.time_since_epoch() + rhs),其中 CT 是返回值的类型。

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);

返回: 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);

返回: 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);

返回: 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);

返回: 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);

返回: !(lhs == rhs)

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

返回: 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);

返回: !(rhs < lhs)

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

返回: rhs < lhs

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

返回: !(lhs < rhs)

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

要求: 此函数不参与重载解析,除非 ToDurationduration 的实例化。

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

此文件包含标准时钟类。本节定义的类型满足 TrivialClock 要求

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>;

  }
}

system_clock 类提供了一种从系统范围的实时时钟获取当前墙上时钟时间的方法。可以通过调用 system_clock::now() 来获取当前时间。可以使用 system_clock::to_time_t()system_clock::from_time_t() 函数将 system_clock::time_point 的实例转换为 time_t 并从 time_t 转换。如果系统时钟不是稳定的,后续调用 system_clock::now() 可能会返回比前一次调用更早的时间(例如,如果操作系统时钟被手动调整或与外部时钟同步)。

system_clock 的当前实现与纪元(1970 年 1 月 1 日 UTC 午夜)相关,但这不在合同中。您需要使用静态函数 static

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

它返回一个 time_t 类型,该类型基于 1970 年 1 月 1 日 UTC 午夜。

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 满足 Clock 要求

  • system_clock::duration::min() < system_clock::duration::zero()true
  • 嵌套的 duration 类型定义具有取决于平台提供的分辨率。
time_t to_time_t(const time_point& t) noexcept;

返回: 一个 time_t,使得 time_tt 表示同一时间点,截断到 time_ttime_point 之间精度更粗的那个。

time_point from_time_t(time_t t) noexcept;

返回: 一个 time_point,使得 time_pointt 表示同一时间点,截断到 time_pointtime_t 之间精度更粗的那个。

如果平台支持稳定时钟,则定义。

steady_clock 满足 Clock 要求。

steady_clock 类提供对系统范围的稳定时钟的访问。可以通过调用 steady_clock::now() 来获取当前时间。 steady_clock::now() 返回的值与墙上时钟时间之间没有固定关系。

#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 满足 Clock 要求。

#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() 返回 "system_clock"。

clock_string<>::since() 返回 " 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() 返回 "steady_clock"。

clock_string<>::since() 返回 " since boot"

namespace boost {
  namespace chrono {

    template <class Clock, class CharT>
    struct clock_string;

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

此模板必须为特定时钟进行特化。特化必须定义以下函数

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

clock_string<>::name() 返回时钟名称,通常对应于类名。

clock_string<>::since() 返回时钟纪元的文本格式。

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);

}
}

duration 的单位名称可以通过 facet 进行自定义:duration_punctduration 的单位名称有两种:长名称和短名称。默认构造的 duration_punct 提供长格式的名称。这些名称是英文描述。其他语言通过构造 duration_punct 并提供 "hours", "minutes" 和 "seconds" 的正确拼写(以及它们的缩写,用于短格式)来支持。

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;
};

可以通过流式输出 duration_shortduration_long 操纵符来轻松选择短格式或长格式。

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

效果:duration_punct facet 设置为以缩写形式流式输出 durationtime_point

返回: 输出流

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

效果:duration_punct facet 设置为以长文本形式流式输出 duration 和 time_point。

返回: 输出流

任何 duration 都可以流式输出到 basic_ostreamduration 的运行时值将根据 duration::rep 的规则和当前格式设置进行格式化。后面会跟着一个空格,然后是 duration 的编译时单位名称。此单位名称基于 ratio_string<> 返回的字符串以及用于构造插入到流的 locale 的 duration_punct 的数据。如果 duration_punct 未插入到流的 locale 中,则会向流的 locale 添加一个默认构造的 duration_punct

一个 time_point 通过输出其内部 duration,然后是一个描述 time_point::clock 纪元的字符串来格式化。此字符串对于每个不同的时钟以及提供的时钟的每个实现都会有所不同。

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);

效果: 根据 duration_punct facet 的状态,以缩写或长文本格式输出 duration

返回: 输出流

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)

效果: 从输入流读取 duration。如果找到格式错误,输入流状态将设置为 failbit

返回: 输入流

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);

效果: 根据 duration_punct facet 的状态,以缩写或长文本格式输出 time_point

返回: 输出流

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);

效果: 从输入流读取 time_point。如果找到格式错误,输入流状态将设置为 failbit

返回: 输入流

此文件包含了 duration 和 time_point 这两个主要组件的 i/o。

#include <boost/chrono/io/duration_style.hpp>
#include <boost/chrono/io/timezone.hpp>
#include <boost/chrono/io/ios_base_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 的单位名称有两种:前缀和符号。

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);

返回: 流的 duration_style 属性,与 ios 相关。

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

效果: 使用 style 参数设置与 ios 相关的流的 duration_style 属性。

timezone get_timezone(std::ios_base & ios);

返回: 流的 timezone 属性,与 ios 相关。

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

效果: 使用 tz 参数设置与 ios 相关的流的 timezone 属性。

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

返回: 流的时间格式属性,与 ios 相关。

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

效果: 使用 fmt 参数设置与 ios 相关的流的时间格式属性。

格式由零个或多个指令组成。每个指令由以下任一组成

* one or more white-space characters (as specified by isspace());
* an ordinary character (neither '%' nor a white-space character);
* or a conversion specification.

每个转换规范由一个 '%' 字符后跟一个指定所需替换的转换字符组成。应用程序应确保任何两个转换规范之间有空格或其他非字母数字字符。支持以下转换规范:

表 5.3. 格式标签

格式说明符

描述

示例

%%

被替换为 %

.

%a %A

星期几,使用 locale 的星期名称;可以使用缩写或全称。

"Monday"。

%b %B %h

月份,使用 locale 的月份名称;可以使用缩写或全称。

"February"。

%c

不支持。

.

%d %e

月份 [01,31];允许前导零,但不强制。

.

%D

日期为 %m/%d/%y

.

%F

日期为 %Y/%m/%d

.

%H

小时(24 小时制)[00,23];允许前导零,但不强制。

.

%I

小时(12 小时制)[01,12];允许前导零,但不强制。

.

%j

一年中的第几天 [001,366];允许前导零,但不强制。

"060" => Feb-29。

%m

月份 [01,12];允许前导零,但不强制。

"01" => January。

%M

分钟 [00,59];允许前导零,但不强制。

.

%n %t

任何空格。

.

%p

不支持。

.

%r

不支持。

.

%R

时间为 %H:%M

.

%S

[00,60];允许前导零,但不强制。

.

%T

时间为 %H:%M:%S

.

%U

不支持。

.

%w

星期几,以十进制数表示 [0,6],其中 0 代表星期日;允许前导零,但不强制。

"0" => Sunday。

%W

不支持。

.

%x

日期,使用 locale 的日期格式。

.

%X

不支持。

.

%y

不支持。

"2005".

%Y

年份,包括世纪(例如,1988)。

.


// 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();
};

state_type 是 IOStreams 基类 std::ios_base 的一个版本。

此构造函数接收一个流对象,并保存流的引用以及特定流属性的当前值。

explicit duration_style_io_saver(state_type &s);

效果: 通过存储 s 来构造一个 duration_style_io_saver

此构造函数类似于上一个,并另外使用其第二个参数将流的属性更改为给定的新 aspect_type 值。

explicit duration_style_io_saver(state_type &s, aspect_type new_value);

效果: 通过存储 s 来构造一个 duration_style_io_saver。使用 new_value 的值设置 state_type aspect_type

析构函数将流的属性恢复到保存的值。

~duration_style_io_saver();

效果: 就像调用 restore() 一样。

可以使用 restore 成员函数提前(并且经常)激活恢复。

void restore();

效果: 将流的 duration_style 属性恢复到保存的值。

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();
};

state_typestd::basic_ios<CharT, Traits> 的 IOStreams 基类模板的一个版本,其中 CharT 是字符类型,Traits 是字符特征类。用户通常会为 state_type 参数放置一个实际的输入、输出或组合流对象,而不是基类对象。

此构造函数接收一个流对象,并保存流的引用以及特定流属性的当前值。

explicit timezone_io_saver(state_type &s);

效果: 通过存储 s 来构造一个 timezone_io_saver

此构造函数类似于上一个,并使用其第二个参数将流的属性更改为给定的新 aspect_type 值。

explicit timezone_io_saver(state_type &s, aspect_type new_value);

效果: 通过存储 s 来构造一个 timezone_io_saver。使用 new_value 的值设置 state_type aspect_type

析构函数将流的属性恢复到保存的值。

~timezone_io_saver();

效果: 就像调用 restore() 一样。

可以使用 restore 成员函数提前(并且经常)激活恢复。

void restore();

效果: 将流的 timezone 属性恢复到保存的值。

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();
};

state_typestd::basic_ios<CharT, Traits> 的 IOStreams 基类模板的一个版本,其中 CharT 是字符类型,Traits 是字符特征类。用户通常会为 state_type 参数放置一个实际的输入、输出或组合流对象,而不是基类对象。

此构造函数接收一个流对象,并保存流的引用以及特定流属性的当前值。

explicit time_fmt_io_saver(state_type &s);

效果: 通过存储 s 来构造一个 time_fmt_io_saver

此构造函数类似于上一个,并使用其第二个参数将流的属性更改为给定的新 aspect_type 值。

explicit time_fmt_io_saver(state_type &s, aspect_type new_value);

效果: 通过存储 s 来构造一个 time_fmt_io_saver。使用 new_value 的值设置 state_type aspect_type

析构函数将流的属性恢复到保存的值。

~time_fmt_io_saver();

效果: 就像调用 restore() 一样。

可以使用 restore 成员函数提前(并且经常)激活恢复。

void restore();

效果: 将流的时间格式属性恢复到保存的值。

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()
  {
  }

};

duration_get facet 从字符字符串中提取 duration,并将结果存储在类 duration d 参数中。facet 使用特定的格式作为指南来解析字符串。如果字符串不符合格式,则 facet 会通过将 err 参数设置为 iosbase::failbit 来指示错误。换句话说,需要用户确认才能可靠地解析用户输入的 duration,但可以可靠地解析机器生成的格式。这使得解析器能够积极地解释用户对标准格式的变体。

explicit duration_get(size_t refs);

构造一个 duration_get facet。

参数

  • refs: 引用

效果: 构造一个 duration_get facet。如果 refs 参数为 0,则对象的销毁将委托给包含它的 locale。这允许用户忽略生命周期管理问题。另一方面,如果 refs1,则必须显式删除该对象;locale 不会这样做。在这种情况下,对象可以在多个 locale 的生命周期内维护。

~duration_get();

销毁一个 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;

根据模式 [pattern,pat_end) 从范围 [s,end) 中提取 duration。

参数

  • s: 开始输入流迭代器
  • end: 结束输入流迭代器
  • ios: 对 ios_base 的引用
  • err: ios_base 状态
  • d: 持续时间
  • pattern: 格式化模式的开头
  • pat_end: 格式化模式的结尾

Requires: [s, end) [pattern, pat_end) 必须是有效范围。

Effects: 函数首先评估 err = std::ios_base::goodbit

然后,根据以下规则,根据 Rep 计算中间表示形式

  • 如果 Rep 是浮点类型,则中间表示形式为 long double
  • 否则,如果 Rep 是有符号整数类型,则中间表示形式为 long long
  • 否则,如果 Rep 是无符号整数类型,则中间表示形式为 unsigned long long。
  • 否则,中间表示形式为 Rep

接下来,将默认构造类型为中间表示形式的局部变量 r 和类型为 rt_ratio 的局部变量 rt

然后进入一个循环,在每次迭代中从 s 读取零个或多个字符。除非下文另有说明,否则当以下任一条件成立时,循环终止

  • 表达式 pattern == pat_end 求值为 true
  • 表达式 err == std::ios_base::goodbit 求值为 false。
  • 表达式 s == end 求值为 true,此时函数计算 err = std::ios_base::eofbit | std::ios_base::failbit
  • 模式的下一个元素等于 '%',后跟一个转换说明符字符 format。如果范围 [pattern,pat_end) 中的元素数量不足以明确确定转换说明是否完整和有效,则函数计算 err = std::ios_base::failbit。否则,当转换说明符为 'v' 时,函数计算 s = get_value(s, end, ios, err, r),当转换说明符为 'u' 时,计算 s = get_value(s, end, ios, err, rt)。如果表达式计算后 err == std::ios_base::goodbit 成立,则函数将 pattern 增加到转换说明符的末尾之后,并继续循环。
  • 表达式 isspace(*pattern, ios.getloc()) 求值为 true,此时函数首先递增 pattern,直到 pattern == pat_end || !isspace(*pattern, ios.getloc()) 求值为 true,然后将 s 高级到 s == end || !isspace(*s, ios.getloc())true,最后恢复循环。
  • s 读取的下一个字符在不区分大小写的情况下与 pattern 指向的元素匹配,此时函数计算 ++pattern, ++s 并继续循环。否则,函数计算 err = std::ios_base::failbit

如果成功解析了持续时间表示值和单位说明符,则计算 (rt.num/rt.den)/(Period::num/Period::den) 并将其简化为最简形式。如果此比率无法存储而未发生溢出,则计算 err = std::ios_base::failbit。否则,将此除法结果存储在 numden 中。

如果除法未导致溢出,则以避免中间溢出的方式计算 r * num / den。如果 r 是整数类型并且此计算不精确,则计算 err = std::ios_base::failbit。如果计算结果将溢出 Rep,则计算 err = std::ios_base::failbit。否则,将 r * num / den 的计算结果用于构造一个 duration<Rep, Period>,并将其赋值给 d

Returns: s,一个指向最后一个可确定为有效持续时间一部分的字符之后的迭代器。

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;

从范围 [s,end) 中提取一个持续时间,遵循默认模式。

参数

  • s: 开始输入流迭代器
  • end: 结束输入流迭代器
  • ios: 对 ios_base 的引用
  • err: ios_base 状态
  • d: 持续时间

Effects: 将持续时间模式存储在与 'ios in let say str` 关联的 __duration_unit facet 中。执行过程如下:

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

Returns: s,一个指向最后一个可确定为有效持续时间一部分的字符之后的迭代器。

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;

从范围 [s,end) 中提取持续时间表示。

参数

  • s: 开始输入流迭代器
  • end: 结束输入流迭代器
  • ios: 对 ios_base 的引用
  • err: ios_base 状态
  • r: 持续时间表示的引用

Effects: 如下。

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

Returns: s,一个指向最后一个可确定为有效 Rep 值一部分的字符之后的迭代器。

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

从范围 [s,end) 中提取持续时间单位。

参数

  • s: 开始输入流迭代器
  • end: 结束输入流迭代器
  • ios: 对 ios_base 的引用
  • err: ios_base 状态
  • rt: 持续时间运行时比率的引用。

效果

  • 如果第一个字符是 '[',则尝试解析形式为 "[N/D]" 的模式,其中 ND 的类型为 unsigned long long
    • 如果成功,rt 将记录 ND 的值,否则计算 err = std::ios_base::failbit 并返回 i
    • 解析与模式 "[N/D]" 之后可能出现的持续时间单位之一匹配的最长字符串,如同
return do_get_n_d_prefix_unit(facet, i, e, is, err);
  • 否则,下一个字符不是 '['。解析与持续时间单位之一匹配的最长字符串,如同
return do_get_prefix_unit(facet, i, e, is, err, rt);

Returns: i,一个指向最后一个可确定为有效持续时间单位一部分的字符之后的迭代器。

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;

提取持续时间与 [N/D] 形式相关联的运行时比率。

这是此 facet 的扩展点,以便我们可以考虑其他可以有有用翻译的周期,例如天和周。

参数

  • s: 开始输入流迭代器
  • end: 结束输入流迭代器
  • ios: 对 ios_base 的引用
  • err: ios_base 状态
  • rt: 持续时间运行时比率的引用。

Effects: 扫描 s 以查找与持续时间单位相关联的所有复数形式中最长的。如果成功,则在 rt 中设置匹配的比率。否则,计算 err = std::ios_base::failbit

Returns: s,一个指向最后一个可确定为有效名称一部分的字符之后的迭代器。

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;

提取持续时间在前缀形式时相关的运行时比率。

这是此 facet 的扩展点,以便我们可以考虑其他可以有有用翻译的周期,例如天和周。

参数

  • s: 开始输入流迭代器
  • end: 结束输入流迭代器
  • ios: 对 ios_base 的引用
  • err: ios_base 状态
  • rt: 持续时间运行时比率的引用。

Effects: 扫描 s 以查找与持续时间单位相关联的所有复数形式中最长的。如果成功,则在 rt 中设置匹配的比率。否则,计算 err = std::ios_base::failbit

Returns: s,一个指向最后一个可确定为有效名称一部分的字符之后的迭代器。

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);

构造一个 duration_put facet。

参数

  • refs: 引用

Effects: 构造一个 duration_put facet。如果 refs 参数为 0,则对象的销毁委托给包含它的 locale。这允许用户忽略生命周期管理问题。另一方面,如果 refs1,则必须显式删除对象;locale 不会这样做。在这种情况下,对象可以在多个 locale 的生命周期内维护。

~duration_put();

销毁一个 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;

参数

  • s: 输出流迭代器
  • ios: 对 ios_base 的引用
  • d: 持续时间
  • pattern: 格式化模式的开头
  • pat_end: 格式化模式的结尾

Effects: 遍历从 patternpat_end 的序列,识别构成模式序列的字符。每个不是模式序列一部分的字符都会立即写入 s,而每个模式序列在被识别后,会调用 put_valueput_unit;因此,模式元素和其他字符会按照它们在模式中出现的顺序交错输出。模式序列通过将每个字符 c 转换为 char 值来识别,就像通过 ct.narrow(c,0) 一样,其中 ct 是从 ios.getloc() 获取的 ctype<charT> 的引用。每个序列的第一个字符等于 '%',后面跟着一个模式说明符字符 specifier,它可以是 'v' 表示持续时间值,或 'u' 表示持续时间单位。对于每个已识别的有效模式序列,会调用 put_value(s, ios, d)put_unit(s, ios, d)

Returns: 指向最后一个字符之后的迭代器。

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

参数

  • s: 输出流迭代器
  • ios: 对 ios_base 的引用
  • d: 持续时间

获取 将持续时间模式存储在 let say str 的 __duration_unit facet 中。执行过程如下:

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

Returns: 指向最后一个字符之后的迭代器。

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;

参数

  • s: 输出流迭代器
  • ios: 对 ios_base 的引用
  • d: 持续时间

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

Returns: 指向最后一个字符之后的迭代器。

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;

参数

  • s: 输出流迭代器
  • ios: 对 ios_base 的引用
  • fill: 填充字符
  • d: 持续时间

Effects:facet 为与 ios 关联的 duration_units<CharT> facet。如果关联的单位有名称,则如同

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

否则,将单位格式化为 "[Period::num/Period::den]",后跟通过 facet.get_n_d_unit(get_duration_style(ios), d) 获得的与 [N/D] 相关的单位。

Returns: s,指向最后一个字符之后的迭代器。

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 提供有关持续时间单位的有用信息,例如复数形式的数量、与持续时间相关联的复数形式、与复数形式和持续时间周期相关的文本,

explicit duration_units(size_t refs = 0);

构造一个 duration_units facet。

参数

  • refs: 引用

Effects: 构造一个 duration_units facet。如果 refs 参数为 0,则对象的销毁委托给包含它的 locale。这允许用户忽略生命周期管理问题。另一方面,如果 refs1,则必须显式删除对象;locale 不会这样做。在这种情况下,对象可以在多个 locale 的生命周期内维护。

virtual ~duration_units();

Effects: 销毁 facet。

virtual const string_type* get_n_d_valid_units_start() const =0;

Returns: 指向有效 [N/D] 单位的开始的指针。

virtual const string_type* get_n_d_valid_units_end() const=0;

Returns: 指向有效 [N/D] 单位的结束的指针。

virtual const string_type* get_valid_units_start() const=0;

Returns: 指向有效单位、符号或具有不同复数形式的前缀的开始的指针。

virtual const string_type* get_valid_units_end() const=0;

Returns: 指向有效单位的结束的指针。

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

参数

  • k: 指向 [N/D] 单位的指针。

Returns: 如果 k 匹配有效单位,则返回 true

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

参数

  • k: 指向单位的指针。

Effects:k 匹配有效单位时,将 rt 设置为有效的 Period。 Returns: 如果 k 匹配有效单位,则返回 true

virtual string_type get_pattern() const=0;

Returns: 默认使用的模式。

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())),与此持续时间相关的单位。

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())), 即与此持续时间相关的 [N/D] 后缀单位。

template <typename Period>
bool is_named_unit() const;

Returns: do_is_named_unit(rt_ratio(Period())),如果与给定 Period 相关的单位已命名,则为 true,否则为 false。

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

Returns: 与此持续时间相关的 [N/D] 后缀单位。

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

Returns: 与此持续时间相关的单位。

virtual bool do_is_named_unit(rt_ratio rt) const =0;

Returns: 如果与给定 Period 相关的单位已命名,则返回 true,否则返回 false。

template <typename CharT = char>
class duration_units_default: public duration_units<CharT>
{
protected:
  static const std::size_t pfs_ = 2; // The default 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;
};

此类用于定义默认英语的字符串。此 facet 为以下周期命名单位

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

构造一个 duration_units_default facet。

参数

  • refs: 引用

Effects: 构造一个 duration_units_default facet。如果 refs 参数为 0,则对象的销毁委托给包含它的 locale。这允许用户忽略生命周期管理问题。另一方面,如果 refs1,则必须显式删除对象;locale 不会这样做。在这种情况下,对象可以在多个 locale 的生命周期内维护。

virtual ~duration_units_default();

Effects: 销毁 facet。

virtual const string_type* get_n_d_valid_units_start() const;

Returns: 指向有效 [N/D] 单位的开始的指针。

virtual const string_type* get_n_d_valid_units_end() const;

Returns: 指向有效 [N/D] 单位的结束的指针。

virtual const string_type* get_valid_units_start() const;

Returns: 指向有效单位、符号或具有不同复数形式的前缀的开始的指针。

virtual const string_type* get_valid_units_end() const;

Returns: 指向有效单位的结束的指针。

virtual bool match_n_d_valid_unit(const string_type* k) const;

参数

  • k: 指向 [N/D] 单位的指针。

Returns: 如果 k 匹配有效单位,则返回 true

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

参数

  • k: 指向单位的指针。

Effects:k 匹配有效单位时,将 rt 设置为有效的 Period。 Returns: 如果 k 匹配有效单位,则返回 true

virtual string_type get_pattern() const;

Returns: 默认使用的模式。

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

Returns: 与此持续时间相关的 [N/D] 后缀单位。

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

Returns: 与此持续时间相关的单位。

virtual bool do_is_named_unit(rt_ratio rt) const;

Returns: 如果与给定 Period 相关的单位已命名,则返回 true,否则返回 false。

virtual std::size_t do_get_plural_forms() const;

Returns: 此 facet 管理的关联复数形式的数量。

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

获取关联的复数形式。

参数

value: 持续时间表示

Returns:value 参数关联的复数形式。在英语中,有 2 种复数形式

  • 0 单数 (-1 或 1)
  • 1 复数(所有其他情况)
virtual string_type do_get_unit(duration_style style, ratio<1> u, std::size_t pf) const;

参数

  • style: 持续时间样式。
  • period: 与持续时间小时相关的周期。
  • pf: 请求的复数形式。

Returns: 如果 style 是 symbol,则返回 "s";否则,如果 pf 为 0,则返回 "second";如果 pf 为 1,则返回 "seconds"。

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

参数

  • style: 持续时间样式。
  • period: 与持续时间小时相关的周期。
  • pf: 请求的复数形式。

Returns: 如果 style 是 symbol,则返回 "min";否则,如果 pf 为 0,则返回 "minute";如果 pf 为 1,则返回 "minutes"。

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

参数

  • style: 持续时间样式。
  • period: 与持续时间小时相关的周期。
  • pf: 请求的复数形式。

Returns: 如果 style 是 symbol,则返回 "h";否则,如果 pf 为 0,则返回 "hour";如果 pf 为 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;

参数

  • style: 持续时间样式。
  • u: 周期标签 atto。
  • pf: 请求的复数形式。

Returns: 前缀 u 关联的前缀 + 秒关联的前缀的连接。

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;

参数

  • style: 持续时间样式。
  • u: 周期标签 atto。

Returns: 根据 style 的值,返回 ratio_string 符号或前缀。

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)

  }
}

有一个参数化的操作符,它接受 duration_style 作为参数。可以通过输出 symbol_formatname_format 操作符来轻松选择符号或名称格式。

class duration_fmt
{
public:

  explicit duration_fmt(duration_style style) noexcept;

#ifndef BOOST_NO_CXX11_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: 通过存储 f 来构造一个 duration_fmt

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

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

Returns: 存储的 duration_fmt f

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

Effects: 作为格式化输出函数。构造 sentry 对象后,如果 sentry 转换为 true,则调用 facet.put(os,os,os.fill(),d),其中 facet 是与 os 关联的 duration_put<CharT> facet,或者是一个新创建的默认 duration_put<CharT> facet 实例。

Returns: os

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

Effects: 作为格式化输出函数。构造 sentry 对象后,如果 sentry 转换为 true,则调用 facet.put(os,os,os.fill(),d),其中 facet 是与 os 关联的 duration_put<CharT> facet,或者是一个新创建的默认 duration_put<CharT> facet 实例。

Returns: os

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

任何 duration 都可以输出到 basic_ostream。根据 duration::rep 的规则和当前格式设置以及 duration_units facet 来格式化 duration 的运行时值。

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: 作为格式化输出函数。构造 sentry 对象后,如果 sentry 转换为 true,则调用 facet.put(os,os,os.fill(),d),其中 facet 是与 os 关联的 duration_put<CharT> facet,或者是一个新创建的默认 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: 作为格式化输入函数。构造 sentry 对象后,如果 sentry 转换为 true,则调用 facet.get(is,std::istreambuf_iterator<CharT, Traits>(), is, err, d),其中 facet 是与 is 关联的 duration_get<CharT> facet,或者是一个新创建的默认 duration_get<CharT> facet 实例。

如果任何步骤失败,则调用 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 用于解析字符序列,将持续时间和纪元提取到 time_point 类中。

模式可以按任意顺序包含格式说明符 %d%e

用户输入的持续时间的可靠解析需要用户确认,但机器生成的格式可以可靠地解析。这允许解析器积极地解释用户对标准格式的变体。

如果在解析过程中达到结束迭代器,则成员函数在 err 中设置 std::ios_base::eofbit

explicit time_point_get(size_t refs);

构造一个 __time_point_get facet。

参数

  • refs: 引用

Effects: 构造一个 duration_put facet。如果 refs 参数为 0,则对象的销毁委托给包含它的 locale。这允许用户忽略生命周期管理问题。另一方面,如果 refs1,则必须显式删除对象;locale 不会这样做。在这种情况下,对象可以在多个 locale 的生命周期内维护。

~time_point_get();

Effects: 销毁 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;

参数

  • s: 开始输入流迭代器
  • end: 结束输入流迭代器
  • ios: ios_base 的引用
  • err: ios_base::iostate
  • tp: time_point
  • pattern: 格式化模式的开头
  • pat_end: 格式化模式的结尾

Requires: [pattern,pat_end) 必须是有效范围。

Effects: 函数首先评估 err = std::ios_base::goodbit。然后进入一个循环,在每次迭代中从 s 读取零个或多个字符。除非下文另有说明,否则当以下任一条件成立时,循环终止

  • 表达式 pattern == pat_end 求值为 true
  • 表达式 err == std::ios_base::goodbit 求值为 false
  • 表达式 s == end 求值为 true,此时函数计算 err = std::ios_base::eofbit | std::ios_base::failbit
  • 模式的下一个元素等于 '%',后跟一个转换说明符字符。函数 get_durationget_epoch 会根据格式是 'd' 还是 'e' 被调用。如果范围 [pattern,pat_end) 中的元素数量不足以明确确定转换说明是否完整和有效,则函数计算 err |= std::ios_base::failbit。否则,函数计算 s = do_get(s, end, ios, err, d)。如果表达式计算后 err == std::ios_base::goodbit 成立,则函数将 pattern 增加到转换说明符的末尾之后,并继续循环。
  • 表达式 isspace(*pattern, ios.getloc()) 求值为 true,此时函数首先递增 pattern,直到 pattern == pat_end || !isspace(*pattern, ios.getloc()) 求值为 true,然后将 s 高级到 s == end || !isspace(*s, ios.getloc())true,最后恢复循环。
  • s 读取的下一个字符在不区分大小写的情况下与 pattern 指向的元素匹配,此时函数计算 ++pattern, ++s 并继续循环。否则,函数计算 err = std::ios_base::failbit

Returns: 指向最后一个可确定为有效 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;

参数

  • s: 开始输入流迭代器
  • end: 结束输入流迭代器
  • ios: ios_base 的引用
  • err: ios_base::iostate
  • tp: time_point

Effects: 将持续时间模式存储在 let say strduration_unit facet 中。执行过程如下:

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

Returns: 指向最后一个可确定为有效名称一部分的字符之后的迭代器。

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: 如下。

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

其中 facet 是与 ios 关联的 duration_get facet,或者是默认 duration_get facet 的实例。

Returns: 指向最后一个可确定为有效持续时间一部分的字符之后的迭代器。

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

facet 为与 ios 关联的 time_point_units facet,或者是一个新的默认 __time_point_units_default facet 实例。令 epoch 为使用此 facet 与 Clock 关联的纪元字符串。扫描 s 以匹配 epoch 或达到 end

如果未在到达 end 之前匹配,则 std::ios_base::failbit 会在 err 中设置。如果到达 end,则 std::ios_base::failbit 会在 err 中设置。

返回: 指向可确定为 epoch 一部分的最后一个字符之后的迭代器。

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

__time_point_put facet 提供格式化输出 time_point 值的设施。__time_point_put 的成员函数接收一个 time_point 并将其格式化为字符字符串表示。

tparam ChatT 字符类型 tparam OutputIterator 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);

构造一个 time_point_put facet。

效果: 构造一个 time_point_put facet。如果 refs 参数为 0,则对象的销毁委托给包含它的 locale 或 locales。这允许用户忽略生命周期管理问题。另一方面,如果 refs1,则必须显式删除该对象;locale 不会这样做。在这种情况下,对象可以维护在多个 locale 的生命周期中。

参数

  • refs: 引用
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;

参数

  • i: 输出流迭代器
  • ios: 对 ios_base 的引用
  • fill: 用作填充符的字符
  • tp: time_point
  • pattern: 格式化模式的开头
  • pat_end: 格式化模式的结尾

效果: 遍历从 patternpat_end 的序列,识别构成模式序列的字符。每个不构成模式序列的字符将立即写入 s,并且每个识别出的模式序列将调用 __put_duration 或 __put_epoch;因此,模式元素和其他字符按照它们在模式中出现的顺序交错输出。模式序列通过将每个字符 c 转换为 char 值来识别,如同通过 ct.narrow(c,0),其中 ctctype<charT> 的引用,该引用从 ios.getloc() 获取。每个序列的第一个字符等于 '%',后面跟着一个模式说明符字符 spec,它可以是 'd' 表示持续时间值,或者 'e' 表示 epoch。对于每个识别出的有效模式序列,都会调用 put_duration(s, ios, fill, tp.time_since_epoch())put_epoch(s, ios)

Returns: 指向最后一个字符之后的迭代器。

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;

参数

  • i: 输出流迭代器
  • ios: 对 ios_base 的引用
  • fill: 用作填充符的字符
  • tp: time_point
  • tern: 格式化模式的开始
  • pat_end: 格式化模式的结尾

效果: 将 time_point 模式从 __time_point_unit facet 存储到(例如) str 中。最后执行如同

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

Returns: 指向最后一个字符之后的迭代器。

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;

参数

  • i: 输出流迭代器
  • ios: 对 ios_base 的引用
  • fill: 用作填充符的字符
  • d: duration

效果: 如同 facet.put(s, ios, fill, d),其中 facet 是与 ios 关联的 duration_put<CharT> facet,或者是一个新的 duration_put<CharT> 实例。

Returns: 指向最后一个字符之后的迭代器。

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

参数

  • i: 输出流迭代器
  • ios: 对 ios_base 的引用

Effects: 如下。

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

其中 facet 是与 ios 关联的 time_point_units<CharT> facet,或者是一个新的 __time_point_units_default<CharT> 实例。

Returns: s,指向最后一个字符之后的迭代器。

namespace boost {
  namespace chrono {
    template <typename CharT, typename Clock, typename TPUFacet>
    std::basic_string<CharT> get_epoch_custom(Clock, TPUFacet& f);
    template <typename CharT=char>
    class time_point_units;
    template <typename CharT=char>
    class time_point_units_default,
  }
}
template <typename CharT, typename Clock, typename TPUFacet>
std::basic_string<CharT> get_epoch_custom(Clock, TPUFacet& f);

用于 Clock 的 epoch 的自定义点。默认调用 f.do_get_epoch(Clock())

用户可以重载此函数。

返回: 将调用转发给 facet,如同

return f.do_get_epoch(Clock());

time_point_units facet 提供有关 time_point 模式、与 time_point 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);

构造一个 time_point_units facet。

参数

  • refs: 引用

效果: 构造一个 time_point_units facet。如果 refs 参数为 0,则对象的销毁委托给包含它的 locale 或 locales。这允许用户忽略生命周期管理问题。另一方面,如果 refsv is 1`,则必须显式删除该对象;locale 不会这样做。在这种情况下,对象可以维护在多个 locale 的生命周期中。

virtual string_type get_pattern() const =0;

Returns: 默认使用的模式。

template <typename Clock>
string_type get_epoch() const;

返回: 对应于 Clock 的 epoch 的字符串,如同 return get_epoch_custom<CharT>(Clock(), *this);

virtual ~time_point_units();

销毁 facet。

virtual string_type do_get_epoch(system_clock) const=0;

参数

返回: 对应于 system_clock 的 epoch 字符串。

virtual string_type do_get_epoch(steady_clock) const=0;

参数

返回: 对应于 steady_clock 的 epoch 字符串。

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

参数

返回: 对应于 process_real_cpu_clock 的 epoch 字符串。

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

参数

返回: 对应于 process_user_cpu_clock 的 epoch 字符串。

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

参数

返回: 对应于 process_user_cpu_clock 的 epoch 字符串。

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

参数

返回: 对应于 process_cpu_clock 的 epoch 字符串。

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

参数

返回: 对应于 thread_clock 的 epoch 字符串。

  // 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);

}
}
非成员函数 time_fmt(timezone)
unspecified time_fmt(timezone tz);

返回:: 一个未指定对象,当流式传输到 basic_ostream<CharT, Traits>basic_istream<CharT, Traits> s 时,将产生以下效果

set_timezone(s, tz);
非成员函数 time_fmt(timezone, string)
template<class CharT>
  unspecified time_fmt(timezone tz, basic_string<CharT> f);
template<class CharT>
  unspecified time_fmt(timezone tz, const CharT* f);

返回:: 一个未指定对象,当流式传输到 basic_ostream<CharT, Traits>basic_istream<CharT, Traits> s 时,将产生以下效果

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);

效果: 作为格式化输出函数。构造一个 sentry 对象后,如果 sentry 转换为 true,则将一个类型为 timezone 的局部变量 tz 设置为 get_timezone(os)。此外,通过 get_time_fmt() 获取的格式化字符串被记录为一对 const CharT*。如果流没有 time_punct facet,则此 const CharT* 对表示一个空范围。

接下来,tp 被转换为 time_t,然后该 time_t 被转换为 tm。转换为 tm 时,如果时区是 utc,则使用 gmtime (如果可用),否则使用 localtime (如果可用)。

使用存储在 os 中的 std::time_put facet,此插入器使用 tm 和存储在 time_punct facet 中的格式化字符串将字符写入流,除非该 facet 丢失,或者它提供了一个空字符串。

如果格式化字符串为空,则按以下方式输出

首先使用 "%F %H:%M:" 输出 tm

接下来输出一个 double,表示 tm 中存储的秒数加上 tp 中表示的分数秒。格式应为 ios::fixed,精度应足以精确表示 system_clock::duration (例如,如果 system_clock::period 是微秒,则精度应足以在小数点后输出六位数字)。如果秒数小于 10,则输出应以 '0' 作为前缀。

最后,如果 tzlocal,则使用模式 " %z" 输出 tm。否则,将字符串 " +0000" 追加到流。

如果任何步骤失败,将调用 os.setstate(ios_base::failbit | ios_base::badbit)

返回: 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);

效果: 作为格式化输入函数。构造一个 sentry 对象后,如果 sentry 转换为 true,则从 is 获取 std::time_get facet,并以与插入运算符相同的方式获取格式化字符串。使用提供的格式化字符串提取 tm,或者如果为空,则默认使用插入运算符中描述的方式。请注意,提取不使用 is 中存储的 timezone 数据(对于默认字符串),因为 timezone 信息存储在流中。

任何插入然后提取的 time_point<system_clock, Duration> 应该产生一个相等的 time_point<system_clock, Duration>,除了任何未被插入的精度。

示例

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);
}

返回: 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);

效果: 作为格式化输出函数。构造一个 sentry 对象后,如果 sentry 转换为 true,则调用 facet.put(os,os,os.fill(),tp),其中 facet 是与 os 关联的 time_point_put<CharT> facet,或者是一个新创建的默认 time_point_put<CharT> facet 实例。

Returns: os

示例

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);

从流 is 中提取 tp

效果: 作为格式化输入函数。构造一个 sentry 对象后,如果 sentry 转换为 true,则调用 facet.get(is,std::istreambuf_iterator<CharT, Traits>(), is, err, tp),其中 facet 是与 is 关联的 time_point_get<CharT> facet,或者是一个新创建的默认 time_point_get<CharT> facet 实例。

如果任何步骤失败,则调用 os.setstate(std::ios_base::failbit | std::ios_base::badbit)

返回: is

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

此函数向下舍入给定参数。

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

此函数将给定参数四舍五入到最近的偶数(平局时)。

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

此函数向上舍入给定参数。

了解程序执行时间在测试和生产环境中都很有用。将此类计时信息细分为实际(挂钟)时间、用户花费的 CPU 时间以及操作系统为响应用户请求而花费的 CPU 时间也很有帮助。

进程时钟不包括子进程花费的时间。

#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> >;
}

如果平台支持进程时钟,则定义此宏。

process_real_cpu_clock 满足 Clock 要求。

process_real_cpu_clock 类提供对实际进程挂钟稳定时钟的访问,即调用进程的实际 CPU 时间时钟。可以通过调用 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 满足 Clock 要求。

process_user_cpu_clock 类提供对调用进程的用户 CPU 时间稳定时钟的访问。可以通过调用 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 满足 Clock 要求。

process_system_cpu_clock 类提供对调用进程的系统 CPU 时间稳定时钟的访问。可以通过调用 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 可以被视为 tuple<process_real_cpu_clock, process_user_cpu_clock, process_system_cpu_clock>

process_cpu_clock 提供了一个围绕操作系统进程时间 API 的薄包装器。对于 POSIX 类系统,它是 times() 函数,而对于 Windows,它是 GetProcessTimes() 函数。

可以通过调用 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 );
};

此类是 process_cpu_clock::duration 类的表示。因此,它需要实现算术运算符。

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);

效果: 每个部分用 ';' 分隔,并用 '{', '}' 包围。

抛出: 无。

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

效果: 如果输入流的格式为 "{r;u;s}",则覆盖 rhs 的值。否则,将输入流状态设置为 failbit | eofbit。

抛出: 无。

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

times 特定的函数 zero()max()min() 使用每个组件表示上的相对函数。

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() 返回 "process_real_cpu_clock"。

clock_string<>::since() 返回 " 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() 返回 "process_user_cpu_clock"。

clock_string<>::since() 返回 " 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() 返回 "process_system_cpu_clock"。

clock_string<>::since() 返回 " 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() 返回 "process_cpu_clock"。

clock_string<>::since() 返回 " 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;
    };
}

process_times<Rep> 的特化函数 min()max()lowest() 使用每个组件表示上的相对函数。

注意

  • min() 返回 min 的元组。
  • max() 返回 max 的元组。
  • lowest() 返回 lowest 的元组。
  • digits 是 (二进制) 位数的总和。
  • digits10 是 decimal 位数的总和。

了解线程执行时间在测试和生产环境中都很有用。

#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>;

} }

如果平台支持线程时钟,则定义此宏。

如果平台有线程时钟,则定义此宏。其值为 true (如果稳定) 或 false (否则)。

thread_clock 满足 Clock 要求。

thread_clock 类提供对实际线程挂钟的访问,即调用线程的实际 CPU 时间时钟。可以通过调用 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() 返回 "thread_clock"。

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


PrevUpHomeNext