@@ -537,7 +537,7 @@ cdef inline void localize_tso(_TSObject obj, tzinfo tz):
537
537
elif obj.value == NPY_NAT:
538
538
pass
539
539
elif is_tzlocal(tz):
540
- local_val = tz_convert_utc_to_tzlocal (obj.value, tz)
540
+ local_val = _tz_convert_tzlocal_utc (obj.value, tz, to_utc = False )
541
541
dt64_to_dtstruct(local_val, & obj.dts)
542
542
else :
543
543
# Adjust datetime64 timestamp, recompute datetimestruct
@@ -608,32 +608,33 @@ cpdef inline datetime localize_pydatetime(datetime dt, object tz):
608
608
# ----------------------------------------------------------------------
609
609
# Timezone Conversion
610
610
611
- cdef inline int64_t tz_convert_tzlocal_to_utc(int64_t val, tzinfo tz):
611
+ cdef inline int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz,
612
+ bint to_utc = True ):
612
613
"""
613
614
Parameters
614
615
----------
615
616
val : int64_t
616
617
tz : tzinfo
618
+ to_utc : bint
619
+ True if converting tzlocal _to_ UTC, False if going the other direction
617
620
618
621
Returns
619
622
-------
620
- utc_date : int64_t
621
-
622
- See Also
623
- --------
624
- tz_convert_utc_to_tzlocal
623
+ result : int64_t
625
624
"""
626
625
cdef:
627
626
pandas_datetimestruct dts
628
- int64_t utc_date , delta
627
+ int64_t result , delta
629
628
datetime dt
630
629
631
630
dt64_to_dtstruct(val, & dts)
632
631
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
633
632
dts.min, dts.sec, dts.us, tz)
634
633
delta = int (get_utcoffset(tz, dt).total_seconds()) * 1000000000
635
- utc_date = val - delta
636
- return utc_date
634
+
635
+ if not to_utc:
636
+ return val + delta
637
+ return val - delta
637
638
638
639
639
640
cdef inline int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz):
@@ -646,27 +647,8 @@ cdef inline int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz):
646
647
Returns
647
648
-------
648
649
local_val : int64_t
649
-
650
- See Also
651
- --------
652
- tz_convert_tzlocal_to_utc
653
-
654
- Notes
655
- -----
656
- The key difference between this and tz_convert_tzlocal_to_utc is a
657
- an addition flipped to a subtraction in the last line.
658
650
"""
659
- cdef:
660
- pandas_datetimestruct dts
661
- int64_t local_val, delta
662
- datetime dt
663
-
664
- dt64_to_dtstruct(utc_val, & dts)
665
- dt = datetime(dts.year, dts.month, dts.day, dts.hour,
666
- dts.min, dts.sec, dts.us, tz)
667
- delta = int (get_utcoffset(tz, dt).total_seconds()) * 1000000000
668
- local_val = utc_val + delta
669
- return local_val
651
+ return _tz_convert_tzlocal_utc(utc_val, tz, to_utc = False )
670
652
671
653
672
654
cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
@@ -699,7 +681,7 @@ cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
699
681
700
682
# Convert to UTC
701
683
if is_tzlocal(tz1):
702
- utc_date = tz_convert_tzlocal_to_utc (val, tz1)
684
+ utc_date = _tz_convert_tzlocal_utc (val, tz1, to_utc = True )
703
685
elif get_timezone(tz1) != ' UTC' :
704
686
trans, deltas, typ = get_dst_info(tz1)
705
687
pos = trans.searchsorted(val, side = ' right' ) - 1
@@ -713,7 +695,7 @@ cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
713
695
if get_timezone(tz2) == ' UTC' :
714
696
return utc_date
715
697
elif is_tzlocal(tz2):
716
- return tz_convert_utc_to_tzlocal (utc_date, tz2)
698
+ return _tz_convert_tzlocal_utc (utc_date, tz2, to_utc = False )
717
699
718
700
# Convert UTC to other timezone
719
701
trans, deltas, typ = get_dst_info(tz2)
@@ -762,7 +744,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
762
744
if v == NPY_NAT:
763
745
utc_dates[i] = NPY_NAT
764
746
else :
765
- utc_dates[i] = tz_convert_tzlocal_to_utc (v, tz1)
747
+ utc_dates[i] = _tz_convert_tzlocal_utc (v, tz1, to_utc = True )
766
748
else :
767
749
trans, deltas, typ = get_dst_info(tz1)
768
750
@@ -797,7 +779,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
797
779
if v == NPY_NAT:
798
780
result[i] = NPY_NAT
799
781
else :
800
- result[i] = tz_convert_utc_to_tzlocal (v, tz2)
782
+ result[i] = _tz_convert_tzlocal_utc (v, tz2, to_utc = False )
801
783
return result
802
784
803
785
# Convert UTC to other timezone
@@ -809,7 +791,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
809
791
return result
810
792
811
793
# if all NaT, return all NaT
812
- tt = utc_dates[utc_dates! = NPY_NAT]
794
+ tt = utc_dates[utc_dates ! = NPY_NAT]
813
795
if not len (tt):
814
796
return utc_dates
815
797
@@ -874,7 +856,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None,
874
856
if is_tzlocal(tz):
875
857
for i in range (n):
876
858
v = vals[i]
877
- result[i] = tz_convert_tzlocal_to_utc (v, tz)
859
+ result[i] = _tz_convert_tzlocal_utc (v, tz, to_utc = True )
878
860
return result
879
861
880
862
if is_string_object(ambiguous):
@@ -1116,7 +1098,7 @@ cdef ndarray[int64_t] _normalize_local(ndarray[int64_t] stamps, object tz):
1116
1098
if stamps[i] == NPY_NAT:
1117
1099
result[i] = NPY_NAT
1118
1100
continue
1119
- local_val = tz_convert_utc_to_tzlocal (stamps[i], tz)
1101
+ local_val = _tz_convert_tzlocal_utc (stamps[i], tz, to_utc = False )
1120
1102
dt64_to_dtstruct(local_val, & dts)
1121
1103
result[i] = _normalized_stamp(& dts)
1122
1104
else :
@@ -1195,7 +1177,7 @@ def is_date_array_normalized(ndarray[int64_t] stamps, tz=None):
1195
1177
return False
1196
1178
elif is_tzlocal(tz):
1197
1179
for i in range (n):
1198
- local_val = tz_convert_utc_to_tzlocal (stamps[i], tz)
1180
+ local_val = _tz_convert_tzlocal_utc (stamps[i], tz, to_utc = False )
1199
1181
dt64_to_dtstruct(local_val, & dts)
1200
1182
if (dts.hour + dts.min + dts.sec + dts.us) > 0 :
1201
1183
return False
@@ -1205,7 +1187,6 @@ def is_date_array_normalized(ndarray[int64_t] stamps, tz=None):
1205
1187
for i in range (n):
1206
1188
# Adjust datetime64 timestamp, recompute datetimestruct
1207
1189
pos = trans.searchsorted(stamps[i]) - 1
1208
- inf = tz._transition_info[pos]
1209
1190
1210
1191
dt64_to_dtstruct(stamps[i] + deltas[pos], & dts)
1211
1192
if (dts.hour + dts.min + dts.sec + dts.us) > 0 :
0 commit comments