Skip to content

CLN: de-duplicate tzlocal conversion function #34301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ from pandas._libs.tslibs.nattype cimport (
from pandas._libs.tslibs.tzconversion import tz_localize_to_utc
from pandas._libs.tslibs.tzconversion cimport (
tz_convert_utc_to_tzlocal,
_tz_convert_tzlocal_fromutc,
tz_convert_single,
)

Expand Down Expand Up @@ -482,7 +481,7 @@ cdef _TSObject create_tsobject_tz_using_offset(npy_datetimestruct dts,
if is_utc(tz):
pass
elif is_tzlocal(tz):
_tz_convert_tzlocal_fromutc(obj.value, tz, &obj.fold)
tz_convert_utc_to_tzlocal(obj.value, tz, &obj.fold)
else:
trans, deltas, typ = get_dst_info(tz)

Expand Down Expand Up @@ -644,7 +643,7 @@ cdef inline void localize_tso(_TSObject obj, tzinfo tz):
elif obj.value == NPY_NAT:
pass
elif is_tzlocal(tz):
local_val = _tz_convert_tzlocal_fromutc(obj.value, tz, &obj.fold)
local_val = tz_convert_utc_to_tzlocal(obj.value, tz, &obj.fold)
dt64_to_dtstruct(local_val, &obj.dts)
else:
# Adjust datetime64 timestamp, recompute datetimestruct
Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/tzconversion.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ from cpython.datetime cimport tzinfo
from numpy cimport int64_t


cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz)
cdef int64_t _tz_convert_tzlocal_fromutc(int64_t val, tzinfo tz, bint *fold)
cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz, bint* fold=*)
cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2)
46 changes: 14 additions & 32 deletions pandas/_libs/tslibs/tzconversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,21 @@ cdef inline str _render_tstamp(int64_t val):
# ----------------------------------------------------------------------
# Timezone Conversion

cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz):
cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz, bint* fold=NULL):
"""
Parameters
----------
utc_val : int64_t
tz : tzinfo
fold : bint*
pointer to fold: whether datetime ends up in a fold or not
after adjustment

Returns
-------
local_val : int64_t
"""
return _tz_convert_tzlocal_utc(utc_val, tz, to_utc=False)
return _tz_convert_tzlocal_utc(utc_val, tz, to_utc=False, fold=fold)


cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
Expand Down Expand Up @@ -489,7 +492,8 @@ cdef inline int64_t _tzlocal_get_offset_components(int64_t val, tzinfo tz,
return int(get_utcoffset(tz, dt).total_seconds()) * 1000000000


cdef int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, bint to_utc=True):
cdef int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, bint to_utc=True,
bint* fold=NULL):
"""
Convert the i8 representation of a datetime from a tzlocal timezone to
UTC, or vice-versa.
Expand All @@ -502,32 +506,6 @@ cdef int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, bint to_utc=True):
tz : tzinfo
to_utc : bint
True if converting tzlocal _to_ UTC, False if going the other direction

Returns
-------
result : int64_t
"""
cdef int64_t delta

delta = _tzlocal_get_offset_components(val, tz, to_utc, NULL)

if to_utc:
return val - delta
else:
return val + delta


cdef int64_t _tz_convert_tzlocal_fromutc(int64_t val, tzinfo tz, bint *fold):
"""
Convert the i8 representation of a datetime from UTC to local timezone,
set fold by pointer

Private, not intended for use outside of tslibs.conversion

Parameters
----------
val : int64_t
tz : tzinfo
fold : bint*
pointer to fold: whether datetime ends up in a fold or not
after adjustment
Expand All @@ -540,11 +518,15 @@ cdef int64_t _tz_convert_tzlocal_fromutc(int64_t val, tzinfo tz, bint *fold):
-----
Sets fold by pointer
"""
cdef int64_t delta
cdef:
int64_t delta

delta = _tzlocal_get_offset_components(val, tz, False, fold)
delta = _tzlocal_get_offset_components(val, tz, to_utc, fold)

return val + delta
if to_utc:
return val - delta
else:
return val + delta


@cython.boundscheck(False)
Expand Down