Skip to content

Commit 12c65ad

Browse files
authored
Fix #772: Speed up DateTime.to_clock_time (#782)
Speedup is around x100 to x200. Since this function is used when serializing DateTime objects to Bolt, this also affects the driver's speed when handling certain DateTime objects.
1 parent 8674957 commit 12c65ad

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

neo4j/time/__init__.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,14 +2563,9 @@ def to_ordinal(self) -> int:
25632563

25642564
def to_clock_time(self) -> ClockTime:
25652565
"""Convert to :class:`.ClockTime`."""
2566-
total_seconds = 0
2567-
for year in range(1, self.year):
2568-
total_seconds += 86400 * DAYS_IN_YEAR[year]
2569-
for month in range(1, self.month):
2570-
total_seconds += 86400 * Date.days_in_month(self.year, month)
2571-
total_seconds += 86400 * (self.day - 1)
2572-
seconds, nanoseconds = divmod(self.__time.ticks, NANO_SECONDS)
2573-
return ClockTime(total_seconds + seconds, nanoseconds)
2566+
ordinal_seconds = 86400 * (self.__date.to_ordinal() - 1)
2567+
time_seconds, nanoseconds = divmod(self.__time.ticks, NANO_SECONDS)
2568+
return ClockTime(ordinal_seconds + time_seconds, nanoseconds)
25742569

25752570
def to_native(self) -> datetime:
25762571
"""Convert to a native Python :class:`datetime.datetime` value.

0 commit comments

Comments
 (0)