File indexing completed on 2025-01-18 10:17:52
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <pybind11/chrono.h>
0012
0013 #include "pybind11_tests.h"
0014
0015 #include <chrono>
0016
0017 struct different_resolutions {
0018 using time_point_h = std::chrono::time_point<std::chrono::system_clock, std::chrono::hours>;
0019 using time_point_m = std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>;
0020 using time_point_s = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
0021 using time_point_ms
0022 = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>;
0023 using time_point_us
0024 = std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>;
0025 time_point_h timestamp_h;
0026 time_point_m timestamp_m;
0027 time_point_s timestamp_s;
0028 time_point_ms timestamp_ms;
0029 time_point_us timestamp_us;
0030 };
0031
0032 TEST_SUBMODULE(chrono, m) {
0033 using system_time = std::chrono::system_clock::time_point;
0034 using steady_time = std::chrono::steady_clock::time_point;
0035
0036 using timespan = std::chrono::duration<int64_t, std::nano>;
0037 using timestamp = std::chrono::time_point<std::chrono::system_clock, timespan>;
0038
0039
0040
0041 m.def("test_chrono1", []() { return std::chrono::system_clock::now(); });
0042
0043
0044
0045 m.def("test_chrono2", [](system_time t) { return t; });
0046
0047
0048
0049 m.def("test_chrono3", [](std::chrono::system_clock::duration d) { return d; });
0050
0051
0052
0053 m.def("test_chrono4", [](system_time a, system_time b) { return a - b; });
0054
0055
0056
0057 m.def("test_chrono5", []() { return std::chrono::steady_clock::now(); });
0058
0059
0060
0061 m.def("test_chrono6", [](steady_time t) { return t; });
0062
0063
0064
0065 m.def("test_chrono7", [](std::chrono::microseconds t) { return t; });
0066
0067 m.def("test_chrono_float_diff",
0068 [](std::chrono::duration<float> a, std::chrono::duration<float> b) { return a - b; });
0069
0070 m.def("test_nano_timepoint",
0071 [](timestamp start, timespan delta) -> timestamp { return start + delta; });
0072
0073
0074 py::class_<different_resolutions>(m, "different_resolutions")
0075 .def(py::init<>())
0076 .def_readwrite("timestamp_h", &different_resolutions::timestamp_h)
0077 .def_readwrite("timestamp_m", &different_resolutions::timestamp_m)
0078 .def_readwrite("timestamp_s", &different_resolutions::timestamp_s)
0079 .def_readwrite("timestamp_ms", &different_resolutions::timestamp_ms)
0080 .def_readwrite("timestamp_us", &different_resolutions::timestamp_us);
0081 }