Blame external/pybind11/docs/advanced/cast/chrono.rst

Packit 534379
Chrono
Packit 534379
======
Packit 534379
Packit 534379
When including the additional header file :file:`pybind11/chrono.h` conversions
Packit 534379
from C++11 chrono datatypes to python datetime objects are automatically enabled.
Packit 534379
This header also enables conversions of python floats (often from sources such
Packit 534379
as ``time.monotonic()``, ``time.perf_counter()`` and ``time.process_time()``)
Packit 534379
into durations.
Packit 534379
Packit 534379
An overview of clocks in C++11
Packit 534379
------------------------------
Packit 534379
Packit 534379
A point of confusion when using these conversions is the differences between
Packit 534379
clocks provided in C++11. There are three clock types defined by the C++11
Packit 534379
standard and users can define their own if needed. Each of these clocks have
Packit 534379
different properties and when converting to and from python will give different
Packit 534379
results.
Packit 534379
Packit 534379
The first clock defined by the standard is ``std::chrono::system_clock``. This
Packit 534379
clock measures the current date and time. However, this clock changes with to
Packit 534379
updates to the operating system time. For example, if your time is synchronised
Packit 534379
with a time server this clock will change. This makes this clock a poor choice
Packit 534379
for timing purposes but good for measuring the wall time.
Packit 534379
Packit 534379
The second clock defined in the standard is ``std::chrono::steady_clock``.
Packit 534379
This clock ticks at a steady rate and is never adjusted. This makes it excellent
Packit 534379
for timing purposes, however the value in this clock does not correspond to the
Packit 534379
current date and time. Often this clock will be the amount of time your system
Packit 534379
has been on, although it does not have to be. This clock will never be the same
Packit 534379
clock as the system clock as the system clock can change but steady clocks
Packit 534379
cannot.
Packit 534379
Packit 534379
The third clock defined in the standard is ``std::chrono::high_resolution_clock``.
Packit 534379
This clock is the clock that has the highest resolution out of the clocks in the
Packit 534379
system. It is normally a typedef to either the system clock or the steady clock
Packit 534379
but can be its own independent clock. This is important as when using these
Packit 534379
conversions as the types you get in python for this clock might be different
Packit 534379
depending on the system.
Packit 534379
If it is a typedef of the system clock, python will get datetime objects, but if
Packit 534379
it is a different clock they will be timedelta objects.
Packit 534379
Packit 534379
Provided conversions
Packit 534379
--------------------
Packit 534379
Packit 534379
.. rubric:: C++ to Python
Packit 534379
Packit 534379
- ``std::chrono::system_clock::time_point`` → ``datetime.datetime``
Packit 534379
    System clock times are converted to python datetime instances. They are
Packit 534379
    in the local timezone, but do not have any timezone information attached
Packit 534379
    to them (they are naive datetime objects).
Packit 534379
Packit 534379
- ``std::chrono::duration`` → ``datetime.timedelta``
Packit 534379
    Durations are converted to timedeltas, any precision in the duration
Packit 534379
    greater than microseconds is lost by rounding towards zero.
Packit 534379
Packit 534379
- ``std::chrono::[other_clocks]::time_point`` → ``datetime.timedelta``
Packit 534379
    Any clock time that is not the system clock is converted to a time delta.
Packit 534379
    This timedelta measures the time from the clocks epoch to now.
Packit 534379
Packit 534379
.. rubric:: Python to C++
Packit 534379
Packit 534379
- ``datetime.datetime`` or ``datetime.date`` or ``datetime.time`` → ``std::chrono::system_clock::time_point``
Packit 534379
    Date/time objects are converted into system clock timepoints. Any
Packit 534379
    timezone information is ignored and the type is treated as a naive
Packit 534379
    object.
Packit 534379
Packit 534379
- ``datetime.timedelta`` → ``std::chrono::duration``
Packit 534379
    Time delta are converted into durations with microsecond precision.
Packit 534379
Packit 534379
- ``datetime.timedelta`` → ``std::chrono::[other_clocks]::time_point``
Packit 534379
    Time deltas that are converted into clock timepoints are treated as
Packit 534379
    the amount of time from the start of the clocks epoch.
Packit 534379
Packit 534379
- ``float`` → ``std::chrono::duration``
Packit 534379
    Floats that are passed to C++ as durations be interpreted as a number of
Packit 534379
    seconds. These will be converted to the duration using ``duration_cast``
Packit 534379
    from the float.
Packit 534379
Packit 534379
- ``float`` → ``std::chrono::[other_clocks]::time_point``
Packit 534379
    Floats that are passed to C++ as time points will be interpreted as the
Packit 534379
    number of seconds from the start of the clocks epoch.