From 3f626aaa686450ed8e5d21009322fd838b6b0c0f Mon Sep 17 00:00:00 2001 From: Joachim Metz Date: Sun, 10 Dec 2023 10:10:11 +0100 Subject: [PATCH] Use nanoseconds in string to date time conversions --- dfdatetime/cocoa_time.py | 5 ++--- dfdatetime/delphi_date_time.py | 5 ++--- dfdatetime/fat_date_time.py | 7 +++---- dfdatetime/posix_time.py | 18 ++++++++---------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/dfdatetime/cocoa_time.py b/dfdatetime/cocoa_time.py index daecd3f..17cb853 100644 --- a/dfdatetime/cocoa_time.py +++ b/dfdatetime/cocoa_time.py @@ -93,7 +93,7 @@ def CopyFromDateTimeString(self, time_string): hours = date_time_values.get('hours', 0) minutes = date_time_values.get('minutes', 0) seconds = date_time_values.get('seconds', 0) - nanoseconds = date_time_values.get('nanoseconds', None) + nanoseconds = date_time_values.get('nanoseconds', 0) time_zone_offset = date_time_values.get('time_zone_offset', 0) timestamp = self._GetNumberOfSecondsFromElements( @@ -101,8 +101,7 @@ def CopyFromDateTimeString(self, time_string): timestamp += self._COCOA_TO_POSIX_BASE timestamp = float(timestamp) - if nanoseconds is not None: - timestamp += float(nanoseconds) / definitions.NANOSECONDS_PER_SECOND + timestamp += float(nanoseconds) / definitions.NANOSECONDS_PER_SECOND self._normalized_timestamp = None self._timestamp = timestamp diff --git a/dfdatetime/delphi_date_time.py b/dfdatetime/delphi_date_time.py index 9e130e3..a844599 100644 --- a/dfdatetime/delphi_date_time.py +++ b/dfdatetime/delphi_date_time.py @@ -101,7 +101,7 @@ def CopyFromDateTimeString(self, time_string): hours = date_time_values.get('hours', 0) minutes = date_time_values.get('minutes', 0) seconds = date_time_values.get('seconds', 0) - nanoseconds = date_time_values.get('nanoseconds', None) + nanoseconds = date_time_values.get('nanoseconds', 0) time_zone_offset = date_time_values.get('time_zone_offset', 0) if year > 9999: @@ -112,8 +112,7 @@ def CopyFromDateTimeString(self, time_string): timestamp = float(timestamp) / definitions.SECONDS_PER_DAY timestamp += self._DELPHI_TO_POSIX_BASE - if nanoseconds is not None: - timestamp += float(nanoseconds) / definitions.NANOSECONDS_PER_DAY + timestamp += float(nanoseconds) / definitions.NANOSECONDS_PER_DAY self._normalized_timestamp = None self._timestamp = timestamp diff --git a/dfdatetime/fat_date_time.py b/dfdatetime/fat_date_time.py index 00270d1..76c9494 100644 --- a/dfdatetime/fat_date_time.py +++ b/dfdatetime/fat_date_time.py @@ -271,14 +271,13 @@ def CopyFromDateTimeString(self, time_string): if year < 1980 or year > (1980 + 0x7f): raise ValueError(f'Year value not supported: {year!s}.') + milliseconds, _ = divmod(nanoseconds, 10000000) + timestamp = self._GetNumberOfSecondsFromElements( year, month, day_of_month, hours, minutes, seconds) timestamp -= self._FAT_DATE_TO_POSIX_BASE timestamp *= 100 - - if nanoseconds: - milliseconds, _ = divmod(nanoseconds, 10000000) - timestamp += milliseconds + timestamp += milliseconds self._timestamp = timestamp self._time_zone_offset = time_zone_offset diff --git a/dfdatetime/posix_time.py b/dfdatetime/posix_time.py index 40f2177..d446bb4 100644 --- a/dfdatetime/posix_time.py +++ b/dfdatetime/posix_time.py @@ -190,14 +190,13 @@ def CopyFromDateTimeString(self, time_string): nanoseconds = date_time_values.get('nanoseconds', 0) time_zone_offset = date_time_values.get('time_zone_offset', 0) + milliseconds, _ = divmod( + nanoseconds, definitions.NANOSECONDS_PER_MILLISECOND) + timestamp = self._GetNumberOfSecondsFromElements( year, month, day_of_month, hours, minutes, seconds) timestamp *= definitions.MILLISECONDS_PER_SECOND - - if nanoseconds: - milliseconds, _ = divmod( - nanoseconds, definitions.NANOSECONDS_PER_MILLISECOND) - timestamp += milliseconds + timestamp += milliseconds self._timestamp = timestamp self._time_zone_offset = time_zone_offset @@ -297,14 +296,13 @@ def CopyFromDateTimeString(self, time_string): nanoseconds = date_time_values.get('nanoseconds', 0) time_zone_offset = date_time_values.get('time_zone_offset', 0) + milliseconds, _ = divmod( + nanoseconds, definitions.NANOSECONDS_PER_MICROSECOND) + timestamp = self._GetNumberOfSecondsFromElements( year, month, day_of_month, hours, minutes, seconds) timestamp *= definitions.MICROSECONDS_PER_SECOND - - if nanoseconds: - milliseconds, _ = divmod( - nanoseconds, definitions.NANOSECONDS_PER_MICROSECOND) - timestamp += milliseconds + timestamp += milliseconds self._timestamp = timestamp self._time_zone_offset = time_zone_offset