Commit 23b460eb by Ralf W. Grosse-Kunstleve Committed by Copybara-Service

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
parent fbaebd60
......@@ -191,8 +191,6 @@ struct type_caster<absl::Time> {
// Conversion part 1 (Python->C++)
bool load(handle src, bool convert) {
// As early as possible to avoid mid-process surprises.
internal::EnsurePyDateTime_IMPORT();
if (convert) {
if (PyLong_Check(src.ptr())) {
value = absl::FromUnixSeconds(src.cast<int64_t>());
......@@ -203,20 +201,6 @@ struct type_caster<absl::Time> {
src.cast<double>()));
return true;
}
if (PyTime_Check(src.ptr())) {
// Adapted from absl/python/time.cc
// Copyright 2018 The Abseil Authors.
timeval tv{PyDateTime_TIME_GET_HOUR(src.ptr()) * 3600 +
PyDateTime_TIME_GET_MINUTE(src.ptr()) * 60 +
PyDateTime_TIME_GET_SECOND(src.ptr()),
PyDateTime_TIME_GET_MICROSECOND(src.ptr())};
value = absl::TimeFromTimeval(tv);
int utcoffset;
if (PyTzOffset(src.ptr(), &utcoffset)) {
value += absl::Seconds(utcoffset);
}
return true;
}
}
if (!hasattr(src, "year") || !hasattr(src, "month") ||
!hasattr(src, "day")) {
......@@ -269,25 +253,6 @@ struct type_caster<absl::Time> {
auto py_datetime = py_from_timestamp(as_seconds, "tz"_a = py_timezone);
return py_datetime.release();
}
private:
// Adapted from absl/python/time.cc
// Copyright 2018 The Abseil Authors.
bool PyTzOffset(PyObject* datetime, int* utcoffset) {
PyObject* offset = PyObject_CallMethod(datetime, "utcoffset", nullptr);
if (!offset || !PyDelta_Check(offset)) {
return false;
}
if (utcoffset) {
*utcoffset = PyDateTime_DELTA_GET_SECONDS(offset) +
PyDateTime_DELTA_GET_DAYS(offset) * 24 * 3600;
}
Py_DECREF(offset);
return true;
}
};
template <typename CivilTimeUnitType>
......
......@@ -248,10 +248,8 @@ class AbslTimeTest(parameterized.TestCase):
with self.assertRaises(TypeError):
absl_example.roundtrip_timezone('Not a timezone')
@parameterized.parameters(
absl_example.roundtrip_duration, absl_example.roundtrip_time
)
def test_from_datetime_time(self, rt):
def test_from_datetime_time(self):
rt = absl_example.roundtrip_duration
dt1 = rt(dt_time(h=13))
dt2 = rt(dt_time(h=15))
self.assertEqual((dt2 - dt1).seconds, 2 * 3600)
......@@ -267,9 +265,7 @@ class AbslTimeTest(parameterized.TestCase):
dt1 = rt(dt_time(tzoff=9))
dt2 = rt(dt_time(tzoff=19))
# Conversion from datetime.time to absl::Duration ignores tzinfo!
self.assertEqual(
(dt2 - dt1).seconds, 0 if rt is absl_example.roundtrip_duration else 10
)
self.assertEqual((dt2 - dt1).seconds, 0)
def make_read_only_numpy_array():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment