Copyright © 2008 Howard Hinnant
Copyright © 2006, 2008 Beman Dawes
Copyright © 2009-2013 Vicente J. Botet Escriba
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Table of Contents
“What is time, then? If nobody asks me, I know; if I have to explain it to someone who has asked me, I do not know."”
This documentation makes use of the following naming and formatting conventions.
fixed width fontand is syntax-highlighted.
(), as in
class_template<>; that is, it is in code font and its name is followed by
<>to indicate that it is a class template.
MACRO(); that is, it is uppercase in code font and its name is followed by
()to indicate that it is a function-like macro. Object-like macros appear without the trailing
In addition, notes such as this one specify non-essential information that provides additional background or rationale.
Finally, you can mentally add the following to any code fragments in this document:
// Include all of Chrono files #include <boost/chrono.hpp>
We all deal with time every day of our lives. We've intuitively known it since birth. Thus we are all very familiar with it and believe it to be a simple matter. The modeling of time in computer programs should be similarly simple. The unfortunate truth is that this perceived simplicity is only skin deep. Fortunately, we do not need a terribly complicated solution to meet the bulk of our needs. However, overly simplistic solutions can be dangerous and inefficient, and won't adapt as the computer industry evolves.
Boost.Chrono implements the new time facilities in C++11, as proposed in N2661 - A Foundation to Sleep On. That document provides background and motivation for key design decisions and is the source of a good deal of information in this documentation.
In addition to the clocks provided by the standard proposal, Boost.Chrono provides specific process and thread clocks.
To make the timing facilities of Boost.Chrono more generally useful, the
library provides a number of clocks that are thin wrappers around the operating
system's process time API, thereby allowing the extraction of wall clock
time, user CPU time, and system CPU time of the process. Wall clock time
is the sum of CPU time and system CPU time. (On POSIX-like systems, this
On Windows, it relies on
The Boost.Chrono library provides:
durationclass . Examples of time durations include days,
nanoseconds, which can be represented with a fixed number of clock ticks per unit. All of these units of time duration are united with a generic interface by the
time_pointrepresents an epoch plus or minus a
duration. The library leaves epochs unspecified. A
time_pointis associated with a clock.
high_resolution_clock. A clock is a pairing of a
duration, and a function which returns a
To make the timing facilities more generally useful, Boost.Chrono provides a number of clocks that are thin wrappers around the operating system's time APIs, thereby allowing the extraction of wall clock time, user CPU time, system CPU time spent by the process,
process_real_cpu_clock, captures wall clock CPU time spent by the current process.
process_user_cpu_clock, captures user-CPU time spent by the current process.
process_system_cpu_clock, captures system-CPU time spent by the current process.
process_cpu_clock, that captures real, user-CPU, and system-CPU process times together.
thread_clockthread steady clock giving the time spent by the current thread (when supported by a platform).
It provides I/O for
time_point. This I/O makes use of
these types much more convenient. In following the "you only pay for
what you use" philosophy, this extra functionality is located in a header
separate from <boost/chrono/chrono.hpp>, namely <boost/chrono/chrono_io.hpp>.
It builds on
to provide readable and flexible formatting and parsing for types in
This textural representation uses SI
prefixes whenever possible. This makes it easy for
boost::milliseconds to be represented by the text
"milliseconds", or a hypothetical meter class to print out "millimeter".
duration and the
time_point i/o can be customized
through the new facets:
time_point_units. The user can specialize
these facets so that the chrono i/o could be localizable. However Boost.Chrono
doesn't provides a complete locale solution.
system_clock::time_point I/O is proposed in terms of
UTC timepoints, strongly guided by ISO 9899:1999, Programming languages -
C, ISO 9945:2003, Information Technology - Portable Operating System Interface
(POSIX) and ISO 8601:2004, Data elements and interchange formats - Information
interchange - Representation of dates and times.
A few simple rounding utility functions for working with durations.
The underlying clocks provided by operating systems are subject to many seemingly arbitrary policies and implementation irregularities. That's a polite way of saying they tend to be flakey, and each operating system or even each clock has its own cruel and unusual forms of flakiness. Don't bet the farm on their accuracy, unless you have become deeply familiar with exactly what the specific operating system is guaranteeing, which is often very little.