From cd548d3b9ac3a58e4be6737a1ea013a0cc65b141 Mon Sep 17 00:00:00 2001 From: Rouven Bauer Date: Mon, 29 Aug 2022 14:43:40 +0200 Subject: [PATCH] Fix #772: Speed up `DateTime.to_clock_time` 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. --- neo4j/time/__init__.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/neo4j/time/__init__.py b/neo4j/time/__init__.py index cd4232e45..fe71e472e 100644 --- a/neo4j/time/__init__.py +++ b/neo4j/time/__init__.py @@ -2563,14 +2563,9 @@ def to_ordinal(self) -> int: def to_clock_time(self) -> ClockTime: """Convert to :class:`.ClockTime`.""" - total_seconds = 0 - for year in range(1, self.year): - total_seconds += 86400 * DAYS_IN_YEAR[year] - for month in range(1, self.month): - total_seconds += 86400 * Date.days_in_month(self.year, month) - total_seconds += 86400 * (self.day - 1) - seconds, nanoseconds = divmod(self.__time.ticks, NANO_SECONDS) - return ClockTime(total_seconds + seconds, nanoseconds) + ordinal_seconds = 86400 * (self.__date.to_ordinal() - 1) + time_seconds, nanoseconds = divmod(self.__time.ticks, NANO_SECONDS) + return ClockTime(ordinal_seconds + time_seconds, nanoseconds) def to_native(self) -> datetime: """Convert to a native Python :class:`datetime.datetime` value.