Skip to content

Commit 23b460e

Browse files
rwgkcopybara-github
authored andcommitted
Remove datetime.time to absl::Time conversion.
This conversion muddles the concepts of * a particular point of time in a day, **independent of a particular date**, and * a particular date + time. This conversion was added only recently, but after more consideration, that was a mistake. PiperOrigin-RevId: 529062347
1 parent fbaebd6 commit 23b460e

File tree

2 files changed

+3
-42
lines changed

2 files changed

+3
-42
lines changed

pybind11_abseil/absl_casters.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,6 @@ struct type_caster<absl::Time> {
191191

192192
// Conversion part 1 (Python->C++)
193193
bool load(handle src, bool convert) {
194-
// As early as possible to avoid mid-process surprises.
195-
internal::EnsurePyDateTime_IMPORT();
196194
if (convert) {
197195
if (PyLong_Check(src.ptr())) {
198196
value = absl::FromUnixSeconds(src.cast<int64_t>());
@@ -203,20 +201,6 @@ struct type_caster<absl::Time> {
203201
src.cast<double>()));
204202
return true;
205203
}
206-
if (PyTime_Check(src.ptr())) {
207-
// Adapted from absl/python/time.cc
208-
// Copyright 2018 The Abseil Authors.
209-
timeval tv{PyDateTime_TIME_GET_HOUR(src.ptr()) * 3600 +
210-
PyDateTime_TIME_GET_MINUTE(src.ptr()) * 60 +
211-
PyDateTime_TIME_GET_SECOND(src.ptr()),
212-
PyDateTime_TIME_GET_MICROSECOND(src.ptr())};
213-
value = absl::TimeFromTimeval(tv);
214-
int utcoffset;
215-
if (PyTzOffset(src.ptr(), &utcoffset)) {
216-
value += absl::Seconds(utcoffset);
217-
}
218-
return true;
219-
}
220204
}
221205
if (!hasattr(src, "year") || !hasattr(src, "month") ||
222206
!hasattr(src, "day")) {
@@ -269,25 +253,6 @@ struct type_caster<absl::Time> {
269253
auto py_datetime = py_from_timestamp(as_seconds, "tz"_a = py_timezone);
270254
return py_datetime.release();
271255
}
272-
273-
private:
274-
// Adapted from absl/python/time.cc
275-
// Copyright 2018 The Abseil Authors.
276-
bool PyTzOffset(PyObject* datetime, int* utcoffset) {
277-
PyObject* offset = PyObject_CallMethod(datetime, "utcoffset", nullptr);
278-
279-
if (!offset || !PyDelta_Check(offset)) {
280-
return false;
281-
}
282-
283-
if (utcoffset) {
284-
*utcoffset = PyDateTime_DELTA_GET_SECONDS(offset) +
285-
PyDateTime_DELTA_GET_DAYS(offset) * 24 * 3600;
286-
}
287-
288-
Py_DECREF(offset);
289-
return true;
290-
}
291256
};
292257

293258
template <typename CivilTimeUnitType>

pybind11_abseil/tests/absl_test.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,8 @@ def test_timezone(self):
248248
with self.assertRaises(TypeError):
249249
absl_example.roundtrip_timezone('Not a timezone')
250250

251-
@parameterized.parameters(
252-
absl_example.roundtrip_duration, absl_example.roundtrip_time
253-
)
254-
def test_from_datetime_time(self, rt):
251+
def test_from_datetime_time(self):
252+
rt = absl_example.roundtrip_duration
255253
dt1 = rt(dt_time(h=13))
256254
dt2 = rt(dt_time(h=15))
257255
self.assertEqual((dt2 - dt1).seconds, 2 * 3600)
@@ -267,9 +265,7 @@ def test_from_datetime_time(self, rt):
267265
dt1 = rt(dt_time(tzoff=9))
268266
dt2 = rt(dt_time(tzoff=19))
269267
# Conversion from datetime.time to absl::Duration ignores tzinfo!
270-
self.assertEqual(
271-
(dt2 - dt1).seconds, 0 if rt is absl_example.roundtrip_duration else 10
272-
)
268+
self.assertEqual((dt2 - dt1).seconds, 0)
273269

274270

275271
def make_read_only_numpy_array():

0 commit comments

Comments
 (0)