Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 1 | Chrono |
| 2 | ====== |
| 3 | |
| 4 | When including the additional header file :file:`pybind11/chrono.h` conversions |
| 5 | from C++11 chrono datatypes to python datetime objects are automatically enabled. |
| 6 | This header also enables conversions of python floats (often from sources such |
| 7 | as `time.monotonic()`, `time.perf_counter()` and `time.process_time()`) into |
| 8 | durations. |
| 9 | |
| 10 | An overview of clocks in C++11 |
| 11 | ------------------------------ |
| 12 | |
| 13 | A point of confusion when using these conversions is the differences between |
| 14 | clocks provided in C++11. There are three clock types defined by the C++11 |
| 15 | standard and users can define their own if needed. Each of these clocks have |
| 16 | different properties and when converting to and from python will give different |
| 17 | results. |
| 18 | |
| 19 | The first clock defined by the standard is ``std::chrono::system_clock``. This |
| 20 | clock measures the current date and time. However, this clock changes with to |
| 21 | updates to the operating system time. For example, if your time is synchronised |
| 22 | with a time server this clock will change. This makes this clock a poor choice |
| 23 | for timing purposes but good for measuring the wall time. |
| 24 | |
| 25 | The second clock defined in the standard is ``std::chrono::steady_clock``. |
| 26 | This clock ticks at a steady rate and is never adjusted. This makes it excellent |
| 27 | for timing purposes, however the value in this clock does not correspond to the |
| 28 | current date and time. Often this clock will be the amount of time your system |
| 29 | has been on, although it does not have to be. This clock will never be the same |
| 30 | clock as the system clock as the system clock can change but steady clocks |
| 31 | cannot. |
| 32 | |
| 33 | The third clock defined in the standard is ``std::chrono::high_resolution_clock``. |
| 34 | This clock is the clock that has the highest resolution out of the clocks in the |
| 35 | system. It is normally a typedef to either the system clock or the steady clock |
| 36 | but can be its own independent clock. This is important as when using these |
| 37 | conversions as the types you get in python for this clock might be different |
| 38 | depending on the system. |
| 39 | If it is a typedef of the system clock, python will get datetime objects, but if |
| 40 | it is a different clock they will be timedelta objects. |
| 41 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 42 | Provided conversions |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 43 | -------------------- |
| 44 | |
| 45 | .. rubric:: C++ to Python |
| 46 | |
| 47 | - ``std::chrono::system_clock::time_point`` → ``datetime.datetime`` |
| 48 | System clock times are converted to python datetime instances. They are |
| 49 | in the local timezone, but do not have any timezone information attached |
| 50 | to them (they are naive datetime objects). |
| 51 | |
| 52 | - ``std::chrono::duration`` → ``datetime.timedelta`` |
| 53 | Durations are converted to timedeltas, any precision in the duration |
| 54 | greater than microseconds is lost by rounding towards zero. |
| 55 | |
| 56 | - ``std::chrono::[other_clocks]::time_point`` → ``datetime.timedelta`` |
| 57 | Any clock time that is not the system clock is converted to a time delta. |
| 58 | This timedelta measures the time from the clocks epoch to now. |
| 59 | |
| 60 | .. rubric:: Python to C++ |
| 61 | |
| 62 | - ``datetime.datetime`` → ``std::chrono::system_clock::time_point`` |
| 63 | Date/time objects are converted into system clock timepoints. Any |
| 64 | timezone information is ignored and the type is treated as a naive |
| 65 | object. |
| 66 | |
| 67 | - ``datetime.timedelta`` → ``std::chrono::duration`` |
| 68 | Time delta are converted into durations with microsecond precision. |
| 69 | |
| 70 | - ``datetime.timedelta`` → ``std::chrono::[other_clocks]::time_point`` |
| 71 | Time deltas that are converted into clock timepoints are treated as |
| 72 | the amount of time from the start of the clocks epoch. |
| 73 | |
| 74 | - ``float`` → ``std::chrono::duration`` |
| 75 | Floats that are passed to C++ as durations be interpreted as a number of |
| 76 | seconds. These will be converted to the duration using ``duration_cast`` |
| 77 | from the float. |
| 78 | |
| 79 | - ``float`` → ``std::chrono::[other_clocks]::time_point`` |
| 80 | Floats that are passed to C++ as time points will be interpreted as the |
| 81 | number of seconds from the start of the clocks epoch. |