Standard library header <chrono>

From cppreference.com
< cpp‎ | header

This header is part of the date and time library.

Contents

[edit] Classes

Defined in namespace std::chrono
(C++11)
a time interval
(class template)
(C++11)
wall clock time from the system-wide realtime clock
(class)
(C++11)
monotonic clock that will never be adjusted
(class)
the clock with the shortest tick period available
(class)
(C++11)
a point in time
(class template)
indicates that a duration is convertible to duration with different tick period
(class template)
constructs zero, min, and max values of a tick count of given type
(class template)
Convenience Typedefs
Defined in namespace std::chrono
std::chrono::nanoseconds duration type with Period std::nano
std::chrono::microseconds duration type with Period std::micro
std::chrono::milliseconds duration type with Period std::milli
std::chrono::seconds duration type with Period std::ratio<1>
std::chrono::minutes duration type with Period std::ratio<60>
std::chrono::hours duration type with Period std::ratio<3600>
Specializations
Defined in namespace std
specializes the std::common_type trait
(class template specialization)
specializes the std::common_type trait
(class template specialization)

[edit] Functions

Duration
Defined in namespace std::chrono
implements arithmetic operations with durations as arguments
(function template)
compares two durations
(function template)
converts a duration to another, with a different tick interval
(function template)
Time point
Defined in namespace std::chrono
modifies the time point by the given duration
(function template)
compares two time points
(function template)
converts a time point to another time point on the same clock, with a different duration
(function template)


[edit] Synopsis

namespace std {
namespace chrono {
 
    // class template duration
    template <class Rep, class Period = ratio<1> > class duration;
 
    // class template time_point
    template <class Clock, class Duration = typename Clock::duration> class time_point;
 
} // namespace chrono
 
// common_type specializations
template <class Rep1, class Period1, class Rep2, class Period2>
    struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>>;
 
template <class Clock, class Duration1, class Duration2>
    struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>>;
 
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>
        typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
        constexpr operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period1, class Rep2, class Period2>
        typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
        constexpr operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period, class Rep2>
        duration<typename common_type<Rep1, Rep2>::type, Period>
        constexpr operator*(const duration<Rep1, Period>& d, const Rep2& s);
    template <class Rep1, class Rep2, class Period>
        duration<typename common_type<Rep1, Rep2>::type, Period>
        constexpr operator*(const Rep1& s, const duration<Rep2, Period>& d);
    template <class Rep1, class Period, class Rep2>
        duration<typename common_type<Rep1, Rep2>::type, Period>
        constexpr operator/(const duration<Rep1, Period>& d, const Rep2& s);
    template <class Rep1, class Period1, class Rep2, class Period2>
        typename common_type<Rep1, Rep2>::type
        constexpr operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period, class Rep2>
        duration<typename common_type<Rep1, Rep2>::type, Period>
        constexpr operator%(const duration<Rep1, Period>& d, const Rep2& s);
    template <class Rep1, class Period1, class Rep2, class Period2>
        typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
        constexpr operator%(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
 
    // 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 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);
    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</*signed integer type of at least 64 bits*/, nano>        nanoseconds;
    typedef duration</*signed integer type of at least 55 bits*/, micro>       microseconds;
    typedef duration</*signed integer type of at least 45 bits*/, milli>       milliseconds;
    typedef duration</*signed integer type of at least 35 bits*/ >             seconds;
    typedef duration</*signed integer type of at least 29 bits*/, ratio< 60>>  minutes; 
    typedef duration</*signed integer type of at least 23 bits*/, ratio<3600>> hours; 
 
    // time_point arithmetic
    template <class Clock, class Duration1, class Rep2, class Period2>
        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>
        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>
        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>
        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>
        bool operator==(const time_point<Clock, Duration1>& lhs,
                        const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
        bool operator!=(const time_point<Clock, Duration1>& lhs,
                        const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
        bool operator< (const time_point<Clock, Duration1>& lhs,
                        const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
        bool operator<=(const time_point<Clock, Duration1>& lhs,
                        const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
        bool operator> (const time_point<Clock, Duration1>& lhs,
                        const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
        bool operator>=(const time_point<Clock, Duration1>& lhs,
                        const time_point<Clock, Duration2>& rhs);
 
    // time_point_cast
    template <class ToDuration, class Clock, class Duration>
        time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
 
    // clocks
    class system_clock;
    class steady_clock;
    class high_resolution_clock;
 
} // namespace chrono
}// namespace std

[edit] std::chrono::treat_as_floating_point

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

[edit] std::chrono::duration_values

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

[edit] std::chrono::duration

template <class Rep, class Period = ratio<1>>
class duration {
public:
    typedef Rep rep;
    typedef Period period;
private:
    rep rep_; // exposition only
public:
    // 20.12.5.1, construct/copy/destroy:
    constexpr duration() = default;
    template <class Rep2>
    constexpr explicit duration(const Rep2& r);
    template <class Rep2, class Period2>
    constexpr duration(const duration<Rep2, Period2>& d);
    ~duration() = default;
    duration(const duration&) = default;
    duration& operator=(const duration&) = default;
 
    // 20.12.5.2, observer:
    constexpr rep count() const;
 
    // 20.12.5.3, arithmetic:
    constexpr duration operator+() const;
    constexpr duration operator-() const;
    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);
 
    // 20.12.5.4, special values:
    static constexpr duration zero();
    static constexpr duration min();
    static constexpr duration max();
};

[edit] std::chrono::time_point

template <class Clock, class Duration = typename Clock::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:
    // 20.12.6.1, construct:
    constexpr time_point(); // has value epoch
    constexpr explicit time_point(const duration& d); // same as time_point() + d
    template <class Duration2>
        constexpr time_point(const time_point<clock, Duration2>& t);
 
    // 20.12.6.2, observer:
    constexpr duration time_since_epoch() const;
 
    // 20.12.6.3, arithmetic:
    time_point& operator+=(const duration& d);
    time_point& operator-=(const duration& d);
 
    // 20.12.6.4, special values:
    static constexpr time_point min();
    static constexpr time_point max();
};

[edit] std::chrono::system_clock

class system_clock {
public:
    typedef /*unspecified*/                             rep;
    typedef ratio</*unspecified*/, /*unspecified*/>     period;
    typedef chrono::duration<rep, period>               duration;
    typedef chrono::time_point<system_clock>            time_point;
    static constexpr bool is_steady = /*unspecified*/;
 
    static time_point now() noexcept;
 
    // Map to C API
    static time_t to_time_t (const time_point& t) noexcept;
    static time_point from_time_t(time_t t) noexcept;
};

[edit] std::chrono::steady_clock

class steady_clock {
public:
    typedef /*unspecified*/                                     rep;
    typedef ratio</*unspecified*/, /*unspecified*/>             period;
    typedef chrono::duration<rep, period>                       duration;
    typedef chrono::time_point</*unspecified*/,/*unspecified*/> time_point;
    static constexpr bool is_steady =                           true;
 
    static time_point now() noexcept;
};

[edit] std::chrono::high_resolution_clock

class high_resolution_clock {
public:
    typedef /*unspecified*/                                     rep;
    typedef ratio</*unspecified*/, /*unspecified*/>             period;
    typedef chrono::duration<rep, period>                       duration;
    typedef chrono::time_point</*unspecified*/, duration>       time_point;
    static constexpr bool is_steady = /*unspecified*/;
 
    static time_point now() noexcept;
};