Skip to content

Commit d916188

Browse files
authored
CLN: de-duplicate tzlocal conversion function (#34301)
1 parent 42a5c1c commit d916188

File tree

3 files changed

+17
-37
lines changed

3 files changed

+17
-37
lines changed

pandas/_libs/tslibs/conversion.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ from pandas._libs.tslibs.nattype cimport (
4242
from pandas._libs.tslibs.tzconversion import tz_localize_to_utc
4343
from pandas._libs.tslibs.tzconversion cimport (
4444
tz_convert_utc_to_tzlocal,
45-
_tz_convert_tzlocal_fromutc,
4645
tz_convert_single,
4746
)
4847

@@ -482,7 +481,7 @@ cdef _TSObject create_tsobject_tz_using_offset(npy_datetimestruct dts,
482481
if is_utc(tz):
483482
pass
484483
elif is_tzlocal(tz):
485-
_tz_convert_tzlocal_fromutc(obj.value, tz, &obj.fold)
484+
tz_convert_utc_to_tzlocal(obj.value, tz, &obj.fold)
486485
else:
487486
trans, deltas, typ = get_dst_info(tz)
488487

@@ -644,7 +643,7 @@ cdef inline void localize_tso(_TSObject obj, tzinfo tz):
644643
elif obj.value == NPY_NAT:
645644
pass
646645
elif is_tzlocal(tz):
647-
local_val = _tz_convert_tzlocal_fromutc(obj.value, tz, &obj.fold)
646+
local_val = tz_convert_utc_to_tzlocal(obj.value, tz, &obj.fold)
648647
dt64_to_dtstruct(local_val, &obj.dts)
649648
else:
650649
# Adjust datetime64 timestamp, recompute datetimestruct

pandas/_libs/tslibs/tzconversion.pxd

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ from cpython.datetime cimport tzinfo
22
from numpy cimport int64_t
33

44

5-
cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz)
6-
cdef int64_t _tz_convert_tzlocal_fromutc(int64_t val, tzinfo tz, bint *fold)
5+
cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz, bint* fold=*)
76
cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2)

pandas/_libs/tslibs/tzconversion.pyx

+14-32
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,21 @@ cdef inline str _render_tstamp(int64_t val):
312312
# ----------------------------------------------------------------------
313313
# Timezone Conversion
314314

315-
cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz):
315+
cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz, bint* fold=NULL):
316316
"""
317317
Parameters
318318
----------
319319
utc_val : int64_t
320320
tz : tzinfo
321+
fold : bint*
322+
pointer to fold: whether datetime ends up in a fold or not
323+
after adjustment
321324
322325
Returns
323326
-------
324327
local_val : int64_t
325328
"""
326-
return _tz_convert_tzlocal_utc(utc_val, tz, to_utc=False)
329+
return _tz_convert_tzlocal_utc(utc_val, tz, to_utc=False, fold=fold)
327330

328331

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

491494

492-
cdef int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, bint to_utc=True):
495+
cdef int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, bint to_utc=True,
496+
bint* fold=NULL):
493497
"""
494498
Convert the i8 representation of a datetime from a tzlocal timezone to
495499
UTC, or vice-versa.
@@ -502,32 +506,6 @@ cdef int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, bint to_utc=True):
502506
tz : tzinfo
503507
to_utc : bint
504508
True if converting tzlocal _to_ UTC, False if going the other direction
505-
506-
Returns
507-
-------
508-
result : int64_t
509-
"""
510-
cdef int64_t delta
511-
512-
delta = _tzlocal_get_offset_components(val, tz, to_utc, NULL)
513-
514-
if to_utc:
515-
return val - delta
516-
else:
517-
return val + delta
518-
519-
520-
cdef int64_t _tz_convert_tzlocal_fromutc(int64_t val, tzinfo tz, bint *fold):
521-
"""
522-
Convert the i8 representation of a datetime from UTC to local timezone,
523-
set fold by pointer
524-
525-
Private, not intended for use outside of tslibs.conversion
526-
527-
Parameters
528-
----------
529-
val : int64_t
530-
tz : tzinfo
531509
fold : bint*
532510
pointer to fold: whether datetime ends up in a fold or not
533511
after adjustment
@@ -540,11 +518,15 @@ cdef int64_t _tz_convert_tzlocal_fromutc(int64_t val, tzinfo tz, bint *fold):
540518
-----
541519
Sets fold by pointer
542520
"""
543-
cdef int64_t delta
521+
cdef:
522+
int64_t delta
544523

545-
delta = _tzlocal_get_offset_components(val, tz, False, fold)
524+
delta = _tzlocal_get_offset_components(val, tz, to_utc, fold)
546525

547-
return val + delta
526+
if to_utc:
527+
return val - delta
528+
else:
529+
return val + delta
548530

549531

550532
@cython.boundscheck(False)

0 commit comments

Comments
 (0)