Skip to content

Commit

Permalink
Changes to handle exception in decimal quantize (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz authored Feb 25, 2023
1 parent 2e97aed commit 6154b76
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions config/dpkg/changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dfdatetime (20221218-1) unstable; urgency=low
dfdatetime (20230225-1) unstable; urgency=low

* Auto-generated

-- Log2Timeline maintainers <[email protected]> Sun, 18 Dec 2022 13:12:18 +0100
-- Log2Timeline maintainers <[email protected]> Sat, 25 Feb 2023 08:38:51 +0100
2 changes: 1 addition & 1 deletion dfdatetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
from dfdatetime import webkit_time


__version__ = '20221218'
__version__ = '20230225'
15 changes: 12 additions & 3 deletions dfdatetime/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,21 +969,30 @@ def GetDateWithTimeOfDay(self):
"""
return self._GetDateWithTimeOfDay()

# TODO: remove this method when there is no more need for it in plaso.
# TODO: remove this method when there is no more need for it in Plaso.
def GetPlasoTimestamp(self):
"""Retrieves a timestamp that is compatible with Plaso.
Returns:
int: a POSIX timestamp in microseconds or None if no timestamp is
available.
Raises:
ValueError: if the timestamp cannot be determined.
"""
normalized_timestamp = self._GetNormalizedTimestamp()
if normalized_timestamp is None:
return None

normalized_timestamp *= definitions.MICROSECONDS_PER_SECOND
normalized_timestamp = normalized_timestamp.quantize(
1, rounding=decimal.ROUND_HALF_UP)

try:
normalized_timestamp = normalized_timestamp.quantize(
1, rounding=decimal.ROUND_HALF_UP)
except decimal.InvalidOperation as exception:
raise ValueError(
f'Unable to round normalized timestamp with error: {exception!s}')

return int(normalized_timestamp)

def GetTimeOfDay(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/cocoa_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def testGetDateWithTimeOfDay(self):
self.assertEqual(
date_with_time_of_day_tuple, (None, None, None, None, None, None))

# TODO: remove this method when there is no more need for it in plaso.
# TODO: remove this method when there is no more need for it in Plaso.
def testGetPlasoTimestamp(self):
"""Tests the GetPlasoTimestamp function."""
cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.0)
Expand Down
20 changes: 20 additions & 0 deletions tests/delphi_date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ def testGetDateWithTimeOfDay(self):
self.assertEqual(
date_with_time_of_day_tuple, (None, None, None, None, None, None))

# TODO: remove this method when there is no more need for it in Plaso.
def testGetPlasoTimestamp(self):
"""Tests the GetPlasoTimestamp function."""
delphi_date_time_object = delphi_date_time.DelphiDateTime(
timestamp=41443.8263953)

micro_posix_timestamp = delphi_date_time_object.GetPlasoTimestamp()
self.assertEqual(micro_posix_timestamp, 1371585000553920)

delphi_date_time_object = delphi_date_time.DelphiDateTime()

micro_posix_timestamp = delphi_date_time_object.GetPlasoTimestamp()
self.assertIsNone(micro_posix_timestamp)

delphi_date_time_object = delphi_date_time.DelphiDateTime(
timestamp=8.0e+174)

with self.assertRaises(ValueError):
delphi_date_time_object.GetPlasoTimestamp()

def testGetTimeOfDay(self):
"""Tests the GetTimeOfDay function."""
delphi_date_time_object = delphi_date_time.DelphiDateTime(
Expand Down

0 comments on commit 6154b76

Please sign in to comment.