Commit 0c34237f by Xiaofei Wang Committed by Copybara-Service

Internal change

PiperOrigin-RevId: 439435815
parent 5f2e3baf
......@@ -13,6 +13,7 @@
// - absl::Duration- converted to/from python datetime.timedelta
// - absl::CivilTime- converted to/from python datetime.datetime and from date.
// - absl::Time- converted to/from python datetime.datetime and from date.
// - absl::TimeZone- converted to/from python str and from int.
// - absl::Span- converted to python sequences and from python buffers,
// opaque std::vectors and/or sequences.
// - absl::string_view
......@@ -79,6 +80,31 @@ inline absl::TimeZone GetTimeZone(handle src) {
return absl::FixedTimeZone(offset_seconds);
}
template <>
struct type_caster<absl::TimeZone> {
public:
PYBIND11_TYPE_CASTER(absl::TimeZone, _<absl::TimeZone>());
// Conversion part 1 (Python->C++)
bool load(handle src, bool convert) {
if (PyUnicode_Check(src.ptr())) {
if (LoadTimeZone(PyUnicode_AsUTF8(src.ptr()), &value)) {
return true;
}
} else if (PyLong_Check(src.ptr())) {
value = absl::FixedTimeZone(PyLong_AsLong(src.ptr()));
return true;
}
return false;
}
// Conversion part 2 (C++ -> Python)
static handle cast(const absl::TimeZone& src, return_value_policy, handle) {
// Converts to Python str
return PyUnicode_FromStringAndSize(src.name().data(), src.name().size());
}
};
// Convert between absl::Duration and python datetime.timedelta.
template <>
struct type_caster<absl::Duration> {
......
......@@ -92,6 +92,8 @@ bool CheckCivilYear(absl::CivilYear datetime, double secs) {
return datetime == MakeCivilYear(secs);
}
absl::TimeZone RoundtripTimeZone(absl::TimeZone timezone) { return timezone; }
// Since a span does not own its elements, we must create a class to own them
// and persist beyond the function that constructs the span for testing.
class VectorContainer {
......@@ -294,6 +296,9 @@ PYBIND11_MODULE(absl_example, m) {
m.def("make_datetime", &MakeTime, arg("secs"));
m.def("check_datetime", &CheckDatetime, arg("datetime"), arg("secs"));
// absl::TimeZone bindings
m.def("roundtrip_timezone", &RoundtripTimeZone, arg("timezone"));
// absl::CivilTime bindings
m.def("make_civilsecond", &MakeCivilSecond, arg("secs"));
m.def("check_civilsecond", &CheckCivilSecond, arg("datetime"), arg("secs"));
......
......@@ -191,6 +191,15 @@ class AbslTimeTest(parameterized.TestCase):
self.assertTrue(
absl_example.check_civilyear(self.TEST_DATETIME, truncated.timestamp()))
def test_timezone(self):
expected_timezone = 'Fixed/UTC+02:00:00'
timezone = absl_example.roundtrip_timezone(expected_timezone)
self.assertEqual(expected_timezone, timezone)
timezone = absl_example.roundtrip_timezone(2 * 60 * 60)
self.assertEqual(expected_timezone, timezone)
with self.assertRaises(TypeError):
absl_example.roundtrip_timezone('Not a timezone')
def make_read_only_numpy_array():
values = np.zeros(5, dtype=np.int32)
......
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