@@ -785,7 +785,6 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
785
785
int64_t[:] deltas
786
786
str typ
787
787
Py_ssize_t[:] pos
788
- npy_datetimestruct dts
789
788
int64_t delta, local_val
790
789
791
790
if tz is None or is_utc(tz):
@@ -795,16 +794,14 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
795
794
result[i] = NPY_NAT
796
795
continue
797
796
local_val = stamps[i]
798
- dt64_to_dtstruct(local_val, & dts)
799
- result[i] = _normalized_stamp(& dts)
797
+ result[i] = _normalize_i8_stamp(local_val)
800
798
elif is_tzlocal(tz):
801
799
for i in range (n):
802
800
if stamps[i] == NPY_NAT:
803
801
result[i] = NPY_NAT
804
802
continue
805
803
local_val = tz_convert_utc_to_tzlocal(stamps[i], tz)
806
- dt64_to_dtstruct(local_val, & dts)
807
- result[i] = _normalized_stamp(& dts)
804
+ result[i] = _normalize_i8_stamp(local_val)
808
805
else :
809
806
# Adjust datetime64 timestamp, recompute datetimestruct
810
807
trans, deltas, typ = get_dst_info(tz)
@@ -816,38 +813,36 @@ cpdef ndarray[int64_t] normalize_i8_timestamps(const int64_t[:] stamps, tzinfo t
816
813
if stamps[i] == NPY_NAT:
817
814
result[i] = NPY_NAT
818
815
continue
819
- dt64_to_dtstruct( stamps[i] + delta, & dts)
820
- result[i] = _normalized_stamp( & dts )
816
+ local_val = stamps[i] + delta
817
+ result[i] = _normalize_i8_stamp(local_val )
821
818
else :
822
819
pos = trans.searchsorted(stamps, side = ' right' ) - 1
823
820
for i in range (n):
824
821
if stamps[i] == NPY_NAT:
825
822
result[i] = NPY_NAT
826
823
continue
827
- dt64_to_dtstruct( stamps[i] + deltas[pos[i]], & dts)
828
- result[i] = _normalized_stamp( & dts )
824
+ local_val = stamps[i] + deltas[pos[i]]
825
+ result[i] = _normalize_i8_stamp(local_val )
829
826
830
827
return result.base # `.base` to access underlying ndarray
831
828
832
829
833
- cdef inline int64_t _normalized_stamp(npy_datetimestruct * dts) nogil:
830
+ @cython.cdivision
831
+ cdef inline int64_t _normalize_i8_stamp(int64_t local_val) nogil:
834
832
"""
835
- Normalize the given datetimestruct to midnight, then convert to int64_t .
833
+ Round the localized nanosecond timestamp down to the previous midnight .
836
834
837
835
Parameters
838
836
----------
839
- *dts : pointer to npy_datetimestruct
837
+ local_val : int64_t
840
838
841
839
Returns
842
840
-------
843
- stamp : int64
844
- """
845
- dts.hour = 0
846
- dts.min = 0
847
- dts.sec = 0
848
- dts.us = 0
849
- dts.ps = 0
850
- return dtstruct_to_dt64(dts)
841
+ int64_t
842
+ """
843
+ cdef:
844
+ int64_t day_nanos = 24 * 3600 * 1 _000_000_000
845
+ return local_val - (local_val % day_nanos)
851
846
852
847
853
848
@ cython.wraparound (False )
0 commit comments