Commit 633f2917 by Ralf W. Grosse-Kunstleve Committed by Copybara-Service

Civil Time Unit conversions compatibility between pybind11_abseil &…

Civil Time Unit conversions compatibility between pybind11_abseil & Google-internal Clif_PyObjAs/From implementations.

This is to support the PyCLIF-pybind11 integration.

PiperOrigin-RevId: 525462353
parent e336d492
...@@ -235,10 +235,10 @@ struct type_caster<absl::Time> { ...@@ -235,10 +235,10 @@ struct type_caster<absl::Time> {
} }
}; };
template <typename CivilTimeType> template <typename CivilTimeUnitType>
struct absl_civil_time_caster { struct absl_civil_datetime_caster {
public: public:
PYBIND11_TYPE_CASTER(CivilTimeType, const_name("CivilTimeType")); PYBIND11_TYPE_CASTER(CivilTimeUnitType, const_name("CivilDateTime"));
bool load(handle src, bool convert) { bool load(handle src, bool convert) {
if (!convert || !hasattr(src, "year") || !hasattr(src, "month") || if (!convert || !hasattr(src, "year") || !hasattr(src, "month") ||
...@@ -252,12 +252,14 @@ struct absl_civil_time_caster { ...@@ -252,12 +252,14 @@ struct absl_civil_time_caster {
minute = GetInt64Attr(src, "minute"); minute = GetInt64Attr(src, "minute");
second = GetInt64Attr(src, "second"); second = GetInt64Attr(src, "second");
} }
value = CivilTimeType(GetInt64Attr(src, "year"), GetInt64Attr(src, "month"), value =
CivilTimeUnitType(GetInt64Attr(src, "year"), GetInt64Attr(src, "month"),
GetInt64Attr(src, "day"), hour, minute, second); GetInt64Attr(src, "day"), hour, minute, second);
return true; return true;
} }
static handle cast(const CivilTimeType& src, return_value_policy, handle) { static handle cast(const CivilTimeUnitType& src, return_value_policy,
handle) {
auto py_datetime_t = module::import("datetime").attr("datetime"); auto py_datetime_t = module::import("datetime").attr("datetime");
auto py_datetime = py_datetime_t(src.year(), src.month(), src.day(), auto py_datetime = py_datetime_t(src.year(), src.month(), src.day(),
src.hour(), src.minute(), src.second()); src.hour(), src.minute(), src.second());
...@@ -265,29 +267,53 @@ struct absl_civil_time_caster { ...@@ -265,29 +267,53 @@ struct absl_civil_time_caster {
} }
}; };
template <typename CivilTimeUnitType>
struct absl_civil_date_caster {
public:
PYBIND11_TYPE_CASTER(CivilTimeUnitType, const_name("CivilDate"));
bool load(handle src, bool convert) {
if (!convert || !hasattr(src, "year") || !hasattr(src, "month") ||
!hasattr(src, "day")) {
return false;
}
value =
CivilTimeUnitType(GetInt64Attr(src, "year"), GetInt64Attr(src, "month"),
GetInt64Attr(src, "day"));
return true;
}
static handle cast(const CivilTimeUnitType& src, return_value_policy,
handle) {
auto py_datetime_t = module::import("datetime").attr("date");
auto py_datetime = py_datetime_t(src.year(), src.month(), src.day());
return py_datetime.release();
}
};
template <> template <>
struct type_caster<absl::CivilSecond> struct type_caster<absl::CivilSecond>
: public absl_civil_time_caster<absl::CivilSecond> {}; : public absl_civil_datetime_caster<absl::CivilSecond> {};
template <> template <>
struct type_caster<absl::CivilMinute> struct type_caster<absl::CivilMinute>
: public absl_civil_time_caster<absl::CivilMinute> {}; : public absl_civil_datetime_caster<absl::CivilMinute> {};
template <> template <>
struct type_caster<absl::CivilHour> struct type_caster<absl::CivilHour>
: public absl_civil_time_caster<absl::CivilHour> {}; : public absl_civil_datetime_caster<absl::CivilHour> {};
template <> template <>
struct type_caster<absl::CivilDay> struct type_caster<absl::CivilDay>
: public absl_civil_time_caster<absl::CivilDay> {}; : public absl_civil_date_caster<absl::CivilDay> {};
template <> template <>
struct type_caster<absl::CivilMonth> struct type_caster<absl::CivilMonth>
: public absl_civil_time_caster<absl::CivilMonth> {}; : public absl_civil_date_caster<absl::CivilMonth> {};
template <> template <>
struct type_caster<absl::CivilYear> struct type_caster<absl::CivilYear>
: public absl_civil_time_caster<absl::CivilYear> {}; : public absl_civil_date_caster<absl::CivilYear> {};
// Returns {true, a span referencing the data contained by src} without copying // Returns {true, a span referencing the data contained by src} without copying
// or converting the data if possible. Otherwise returns {false, an empty span}. // or converting the data if possible. Otherwise returns {false, an empty span}.
......
...@@ -194,8 +194,9 @@ class AbslTimeTest(parameterized.TestCase): ...@@ -194,8 +194,9 @@ class AbslTimeTest(parameterized.TestCase):
truncated = self.TEST_DATETIME.replace( truncated = self.TEST_DATETIME.replace(
hour=0, minute=0, second=0, microsecond=0) hour=0, minute=0, second=0, microsecond=0)
self.assertEqual( self.assertEqual(
truncated, truncated.date(),
absl_example.make_civilday(self.TEST_DATETIME_UTC.timestamp())) absl_example.make_civilday(self.TEST_DATETIME_UTC.timestamp()),
)
def test_pass_datetime_as_civilday(self): def test_pass_datetime_as_civilday(self):
truncated = self.TEST_DATETIME_UTC.replace( truncated = self.TEST_DATETIME_UTC.replace(
...@@ -207,8 +208,9 @@ class AbslTimeTest(parameterized.TestCase): ...@@ -207,8 +208,9 @@ class AbslTimeTest(parameterized.TestCase):
truncated = self.TEST_DATETIME.replace( truncated = self.TEST_DATETIME.replace(
day=1, hour=0, minute=0, second=0, microsecond=0) day=1, hour=0, minute=0, second=0, microsecond=0)
self.assertEqual( self.assertEqual(
truncated, truncated.date(),
absl_example.make_civilmonth(self.TEST_DATETIME_UTC.timestamp())) absl_example.make_civilmonth(self.TEST_DATETIME_UTC.timestamp()),
)
def test_pass_datetime_as_civilmonth(self): def test_pass_datetime_as_civilmonth(self):
truncated = self.TEST_DATETIME_UTC.replace( truncated = self.TEST_DATETIME_UTC.replace(
...@@ -221,8 +223,9 @@ class AbslTimeTest(parameterized.TestCase): ...@@ -221,8 +223,9 @@ class AbslTimeTest(parameterized.TestCase):
truncated = self.TEST_DATETIME.replace( truncated = self.TEST_DATETIME.replace(
month=1, day=1, hour=0, minute=0, second=0, microsecond=0) month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
self.assertEqual( self.assertEqual(
truncated, truncated.date(),
absl_example.make_civilyear(self.TEST_DATETIME_UTC.timestamp())) absl_example.make_civilyear(self.TEST_DATETIME_UTC.timestamp()),
)
def test_pass_datetime_as_civilyear(self): def test_pass_datetime_as_civilyear(self):
truncated = self.TEST_DATETIME_UTC.replace( truncated = self.TEST_DATETIME_UTC.replace(
......
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