Skip to content

Commit

Permalink
ARROW-9963: [Python] Recognize datetime.timezone.utc as UTC on conver…
Browse files Browse the repository at this point in the history
…sion python->pyarrow

Closes apache#8489 from jorisvandenbossche/ARROW-9963

Authored-by: Joris Van den Bossche <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
  • Loading branch information
jorisvandenbossche authored and pitrou committed Oct 21, 2020
1 parent f06c68a commit bb4f2a0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cpp/src/arrow/python/datetime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,16 @@ Result<std::string> 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);
}

Expand Down
2 changes: 1 addition & 1 deletion python/pyarrow/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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' 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):
Expand Down

0 comments on commit bb4f2a0

Please sign in to comment.