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