@@ -150,30 +150,21 @@ template <typename Duration> class type_caster<std::chrono::time_point<std::chro
150
150
// Lazy initialise the PyDateTime import
151
151
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
152
152
153
- // Declare these special duration types so the conversions happen with the correct primitive types (int)
154
- using us_t = duration<int , std::micro>;
155
-
156
- // Get out microseconds, and make sure they are positive, to avoid bug in eastern hemisphere time zones
157
- // (cfr. https://github.com/pybind/pybind11/issues/2417)
158
- auto us = duration_cast<us_t >(src.time_since_epoch () % seconds (1 ));
159
- if (us.count () < 0 )
160
- us += seconds (1 );
161
-
162
- // Subtract microseconds BEFORE `system_clock::to_time_t`, because:
163
- // > If std::time_t has lower precision, it is implementation-defined whether the value is rounded or truncated.
164
- // (https://en.cppreference.com/w/cpp/chrono/system_clock/to_time_t)
165
- std::time_t tt = system_clock::to_time_t (time_point_cast<system_clock::duration>(src - us));
153
+ std::time_t tt = system_clock::to_time_t (time_point_cast<system_clock::duration>(src));
166
154
// this function uses static memory so it's best to copy it out asap just in case
167
155
// otherwise other code that is using localtime may break this (not just python code)
168
156
std::tm localtime = *std::localtime (&tt);
169
157
158
+ // Declare these special duration types so the conversions happen with the correct primitive types (int)
159
+ using us_t = duration<int , std::micro>;
160
+
170
161
return PyDateTime_FromDateAndTime (localtime .tm_year + 1900 ,
171
162
localtime .tm_mon + 1 ,
172
163
localtime .tm_mday ,
173
164
localtime .tm_hour ,
174
165
localtime .tm_min ,
175
166
localtime .tm_sec ,
176
- us .count ());
167
+ (duration_cast< us_t >(src. time_since_epoch () % seconds ( 1 ))) .count ());
177
168
}
178
169
PYBIND11_TYPE_CASTER (type, _(" datetime.datetime" ));
179
170
};
0 commit comments