Skip to content

Commit b278c01

Browse files
committed
Allow assignment of time points of resolutions other than that of a system clock
1 parent 38370a8 commit b278c01

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

include/pybind11/chrono.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ template <typename Duration> class type_caster<std::chrono::time_point<std::chro
140140
}
141141
else return false;
142142

143-
value = system_clock::from_time_t(std::mktime(&cal)) + msecs;
143+
value = time_point_cast<Duration>(system_clock::from_time_t(std::mktime(&cal)) + msecs);
144144
return true;
145145
}
146146

tests/test_chrono.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@
1212
#include <pybind11/chrono.h>
1313
#include <chrono>
1414

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+
1533
TEST_SUBMODULE(chrono, m) {
1634
using system_time = std::chrono::system_clock::time_point;
1735
using steady_time = std::chrono::steady_clock::time_point;
@@ -53,4 +71,14 @@ TEST_SUBMODULE(chrono, m) {
5371
m.def("test_nano_timepoint", [](timestamp start, timespan delta) -> timestamp {
5472
return start + delta;
5573
});
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+
;
5684
}

tests/test_chrono.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,13 @@ def test_nano_timepoint():
200200
time = datetime.datetime.now()
201201
time1 = m.test_nano_timepoint(time, datetime.timedelta(seconds=60))
202202
assert(time1 == time + datetime.timedelta(seconds=60))
203+
204+
205+
def test_chrono_different_resolutions():
206+
resolutions = m.different_resolutions()
207+
time = datetime.datetime.now()
208+
resolutions.timestamp_h = time
209+
resolutions.timestamp_m = time
210+
resolutions.timestamp_s = time
211+
resolutions.timestamp_ms = time
212+
resolutions.timestamp_us = time

0 commit comments

Comments
 (0)