You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.8 KiB

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