test_chrono.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. tests/test_chrono.cpp -- test conversions to/from std::chrono types
  3. Copyright (c) 2016 Trent Houliston <trent@houliston.me> and
  4. Wenzel Jakob <wenzel.jakob@epfl.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #include "pybind11_tests.h"
  9. #include <pybind11/chrono.h>
  10. #include <chrono>
  11. struct different_resolutions {
  12. using time_point_h = std::chrono::time_point<
  13. std::chrono::system_clock, std::chrono::hours>;
  14. using time_point_m = std::chrono::time_point<
  15. std::chrono::system_clock, std::chrono::minutes>;
  16. using time_point_s = std::chrono::time_point<
  17. std::chrono::system_clock, std::chrono::seconds>;
  18. using time_point_ms = std::chrono::time_point<
  19. std::chrono::system_clock, std::chrono::milliseconds>;
  20. using time_point_us = std::chrono::time_point<
  21. std::chrono::system_clock, std::chrono::microseconds>;
  22. time_point_h timestamp_h;
  23. time_point_m timestamp_m;
  24. time_point_s timestamp_s;
  25. time_point_ms timestamp_ms;
  26. time_point_us timestamp_us;
  27. };
  28. TEST_SUBMODULE(chrono, m) {
  29. using system_time = std::chrono::system_clock::time_point;
  30. using steady_time = std::chrono::steady_clock::time_point;
  31. using timespan = std::chrono::duration<int64_t, std::nano>;
  32. using timestamp = std::chrono::time_point<std::chrono::system_clock, timespan>;
  33. // test_chrono_system_clock
  34. // Return the current time off the wall clock
  35. m.def("test_chrono1", []() { return std::chrono::system_clock::now(); });
  36. // test_chrono_system_clock_roundtrip
  37. // Round trip the passed in system clock time
  38. m.def("test_chrono2", [](system_time t) { return t; });
  39. // test_chrono_duration_roundtrip
  40. // Round trip the passed in duration
  41. m.def("test_chrono3", [](std::chrono::system_clock::duration d) { return d; });
  42. // test_chrono_duration_subtraction_equivalence
  43. // Difference between two passed in time_points
  44. m.def("test_chrono4", [](system_time a, system_time b) { return a - b; });
  45. // test_chrono_steady_clock
  46. // Return the current time off the steady_clock
  47. m.def("test_chrono5", []() { return std::chrono::steady_clock::now(); });
  48. // test_chrono_steady_clock_roundtrip
  49. // Round trip a steady clock timepoint
  50. m.def("test_chrono6", [](steady_time t) { return t; });
  51. // test_floating_point_duration
  52. // Roundtrip a duration in microseconds from a float argument
  53. m.def("test_chrono7", [](std::chrono::microseconds t) { return t; });
  54. // Float durations (issue #719)
  55. m.def("test_chrono_float_diff", [](std::chrono::duration<float> a, std::chrono::duration<float> b) {
  56. return a - b; });
  57. m.def("test_nano_timepoint", [](timestamp start, timespan delta) -> timestamp {
  58. return start + delta;
  59. });
  60. // Test different resolutions
  61. py::class_<different_resolutions>(m, "different_resolutions")
  62. .def(py::init<>())
  63. .def_readwrite("timestamp_h", &different_resolutions::timestamp_h)
  64. .def_readwrite("timestamp_m", &different_resolutions::timestamp_m)
  65. .def_readwrite("timestamp_s", &different_resolutions::timestamp_s)
  66. .def_readwrite("timestamp_ms", &different_resolutions::timestamp_ms)
  67. .def_readwrite("timestamp_us", &different_resolutions::timestamp_us)
  68. ;
  69. }