From 83d49cb4ec31cce0f3cff60664480aa0c6a9fb2d 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 81d64499..6016e3e8 100644 --- a/neo4j/time/__init__.py +++ b/neo4j/time/__init__.py @@ -2642,14 +2642,9 @@ def to_clock_time(self): :rtype: 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_ns, NANO_SECONDS) - return ClockTime(total_seconds + seconds, nanoseconds) + ordinal_seconds = 86400 * (self.__date.to_ordinal() - 1) + time_seconds, nanoseconds = divmod(self.__time.ticks_ns, NANO_SECONDS) + return ClockTime(ordinal_seconds + time_seconds, nanoseconds) def to_native(self): """Convert to a native Python :class:`datetime.datetime` value.