Skip to content

Commit

Permalink
Use nanoseconds in string to date time conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed Dec 10, 2023
1 parent 566e25e commit 3f626aa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
5 changes: 2 additions & 3 deletions dfdatetime/cocoa_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,15 @@ 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(
year, month, day_of_month, hours, minutes, seconds)
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
Expand Down
5 changes: 2 additions & 3 deletions dfdatetime/delphi_date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions dfdatetime/fat_date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 8 additions & 10 deletions dfdatetime/posix_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3f626aa

Please sign in to comment.