Skip to content

Commit

Permalink
Merge pull request #48 from david1155/main
Browse files Browse the repository at this point in the history
convert milliseconds if needed
  • Loading branch information
gmr authored Jan 12, 2024
2 parents aafd31e + 6c1a1a5 commit 1195e4c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pamqp/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,13 @@ def timestamp(value: bytes) -> typing.Tuple[int, datetime.datetime]:
"""
try:
temp = common.Struct.timestamp.unpack(value[0:8])
return 8, datetime.datetime.fromtimestamp(temp[0],
tz=datetime.timezone.utc)
ts_value = temp[0]

# Anything above the year 2106 is likely milliseconds
if ts_value > 0xFFFFFFFF:
ts_value /= 1000.0

return 8, datetime.datetime.utcfromtimestamp(ts_value)
except TypeError:
raise ValueError('Could not unpack timestamp value')

Expand Down
18 changes: 18 additions & 0 deletions tests/test_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,21 @@ def test_field_embedded_value_field_table_keys(self):
value = decode.embedded_value(b'F' + self.FIELD_TBL)[1]
self.assertListEqual(sorted(value.keys()),
sorted(self.FIELD_TBL_VALUE.keys()))

def test_decode_large_timestamp_bytes_consumed(self):
dt = datetime.datetime(2107, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
large_timestamp_bytes = struct.pack('>Q', int(dt.timestamp() * 1000))
self.assertEqual(decode.timestamp(large_timestamp_bytes)[0], 8)

def test_decode_large_timestamp_data_type(self):
dt = datetime.datetime(2107, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
large_timestamp_bytes = struct.pack('>Q', int(dt.timestamp() * 1000))
self.assertIsInstance(decode.timestamp(large_timestamp_bytes)[1],
datetime.datetime)

def test_decode_large_timestamp_value(self):
dt = datetime.datetime(2107, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
large_timestamp_bytes = struct.pack('>Q',
int(dt.timestamp() * 1000))
self.assertEqual(decode.timestamp(large_timestamp_bytes)[1],
dt.replace(tzinfo=None))

0 comments on commit 1195e4c

Please sign in to comment.