Skip to content

Commit 93ad0f2

Browse files
CopilotLorak-mmk
andcommitted
Remove unnecessary negative remainder check in timestamp conversion
Python's modulo operator always returns non-negative result when divisor is positive, regardless of dividend sign. The check for remainder_ms < 0 is therefore unnecessary in both Python and Cython implementations. Co-authored-by: Lorak-mmk <5958117+Lorak-mmk@users.noreply.github.com>
1 parent fbab5cd commit 93ad0f2

2 files changed

Lines changed: 2 additions & 9 deletions

File tree

cassandra/cython_utils.pyx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,9 @@ cdef datetime_from_timestamp_ms(int64_t timestamp_ms):
7070
:param timestamp_ms: a unix timestamp, in milliseconds
7171
"""
7272
# Break down milliseconds into components to avoid float conversion
73+
# Cython's % operator uses Python semantics, always returns non-negative for positive divisor
7374
cdef int64_t timestamp_seconds = timestamp_ms // 1000
7475
cdef int64_t remainder_ms = timestamp_ms % 1000
75-
# Handle negative timestamps correctly
76-
if remainder_ms < 0:
77-
remainder_ms += 1000
78-
timestamp_seconds -= 1
7976

8077
cdef int days = <int> (timestamp_seconds // DAY_IN_SECONDS)
8178
cdef int64_t days_in_seconds = (<int64_t> days) * DAY_IN_SECONDS

cassandra/util.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,9 @@ def datetime_from_timestamp_ms(timestamp_ms):
7373
:param timestamp_ms: a unix timestamp, in milliseconds (as integer)
7474
"""
7575
# Break down milliseconds into components to avoid float conversion
76+
# Python's % operator always returns non-negative result for positive divisor
7677
timestamp_seconds = timestamp_ms // 1000
7778
remainder_ms = timestamp_ms % 1000
78-
# Handle negative timestamps correctly
79-
if remainder_ms < 0:
80-
remainder_ms += 1000
81-
timestamp_seconds -= 1
82-
8379
microseconds = remainder_ms * 1000
8480
dt = DATETIME_EPOC + datetime.timedelta(seconds=timestamp_seconds, microseconds=microseconds)
8581
return dt

0 commit comments

Comments
 (0)