Skip to content

Commit 7f6506d

Browse files
authored
Fix #772: Speed up DateTime.to_clock_time (#781)
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 71f745f commit 7f6506d

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
@@ -2642,14 +2642,9 @@ def to_clock_time(self):
26422642
26432643
:rtype: ClockTime
26442644
"""
2645-
total_seconds = 0
2646-
for year in range(1, self.year):
2647-
total_seconds += 86400 * DAYS_IN_YEAR[year]
2648-
for month in range(1, self.month):
2649-
total_seconds += 86400 * Date.days_in_month(self.year, month)
2650-
total_seconds += 86400 * (self.day - 1)
2651-
seconds, nanoseconds = divmod(self.__time.ticks_ns, NANO_SECONDS)
2652-
return ClockTime(total_seconds + seconds, nanoseconds)
2645+
ordinal_seconds = 86400 * (self.__date.to_ordinal() - 1)
2646+
time_seconds, nanoseconds = divmod(self.__time.ticks_ns, NANO_SECONDS)
2647+
return ClockTime(ordinal_seconds + time_seconds, nanoseconds)
26532648

26542649
def to_native(self):
26552650
"""Convert to a native Python :class:`datetime.datetime` value.

0 commit comments

Comments
 (0)