From cd0128f70aa9b27289d29dd4c75dfe314191f2d8 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 19 Oct 2020 10:21:43 +0200 Subject: [PATCH 1/2] ARROW-9963: [Python] Recognize datetime.timezone.utc as UTC on conversion python->pyarrow --- cpp/src/arrow/python/datetime.cc | 10 ++++++++++ python/pyarrow/tests/test_types.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/python/datetime.cc b/cpp/src/arrow/python/datetime.cc index 8df2012d3a0..4b18918cbcf 100644 --- a/cpp/src/arrow/python/datetime.cc +++ b/cpp/src/arrow/python/datetime.cc @@ -415,6 +415,16 @@ Result TzinfoToString(PyObject* tzinfo) { // HH:MM offset string representation if (PyObject_IsInstance(tzinfo, class_timezone.obj()) || PyObject_IsInstance(tzinfo, class_fixedoffset.obj())) { + // still recognize datetime.timezone.utc as UTC (instead of +00:00) + OwnedRef tzname_object(PyObject_CallMethod(tzinfo, "tzname", "O", Py_None)); + RETURN_IF_PYERROR(); + if (PyUnicode_Check(tzname_object.obj())) { + std::string result; + RETURN_NOT_OK(internal::PyUnicode_AsStdString(tzname_object.obj(), &result)); + if (result == "UTC") { + return result; + } + } return PyTZInfo_utcoffset_hhmm(tzinfo); } diff --git a/python/pyarrow/tests/test_types.py b/python/pyarrow/tests/test_types.py index 9ac2f01686f..bc3a0dbf937 100644 --- a/python/pyarrow/tests/test_types.py +++ b/python/pyarrow/tests/test_types.py @@ -263,7 +263,7 @@ def test_is_primitive(): # name from the tzinfo.zone attribute (pytz.timezone('Etc/GMT-9'), 'Etc/GMT-9'), (pytz.FixedOffset(180), '+03:00'), - (datetime.timezone.utc, '+00:00'), + (datetime.timezone.utc, 'UTC'), (datetime.timezone(datetime.timedelta(hours=1, minutes=30)), '+01:30') ]) def test_tzinfo_to_string(tz, expected): From e31c7435ccf8cb0d5e05ac191612bc8ee034672e Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 21 Oct 2020 14:29:23 +0200 Subject: [PATCH 2/2] update expected return value for Python 3.5 --- python/pyarrow/tests/test_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/pyarrow/tests/test_types.py b/python/pyarrow/tests/test_types.py index bc3a0dbf937..4de5ffabfad 100644 --- a/python/pyarrow/tests/test_types.py +++ b/python/pyarrow/tests/test_types.py @@ -263,7 +263,7 @@ def test_is_primitive(): # name from the tzinfo.zone attribute (pytz.timezone('Etc/GMT-9'), 'Etc/GMT-9'), (pytz.FixedOffset(180), '+03:00'), - (datetime.timezone.utc, 'UTC'), + (datetime.timezone.utc, 'UTC' if sys.version_info >= (3, 6) else '+00:00'), (datetime.timezone(datetime.timedelta(hours=1, minutes=30)), '+01:30') ]) def test_tzinfo_to_string(tz, expected):