Skip to content

Commit

Permalink
Merge pull request #14 from mosquito/hotfix/numeric-timestamp
Browse files Browse the repository at this point in the history
Allow numeric values for timestamp fields
  • Loading branch information
gmr committed Apr 18, 2019
2 parents 7297f96 + 6106e67 commit bd13a60
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pamqp/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,17 @@ def short_string(value):
def timestamp(value):
"""Encode a datetime.datetime object or time.struct_time.
:param datetime.datetime or time.struct_time value value: Value to encode
:param value: Value to encode
:type value: datetime.datetime, time.struct_time, integer
:rtype: bytes
:raises: TypeError
"""
if isinstance(value, datetime.datetime):
value = value.timetuple()
elif isinstance(value, int):
value = time.gmtime(value)

if isinstance(value, time.struct_time):
return struct.pack('>Q', calendar.timegm(value))
raise TypeError('datetime.datetime or time.struct_time type required')
Expand Down
5 changes: 4 additions & 1 deletion tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,13 @@ def test_encode_timestamp_from_struct_time(self):
encode.timestamp(datetime(2006, 11, 21, 16, 30, 10).timetuple())
self.assertEqual(value, b'\x00\x00\x00\x00Ec)\x92')

def test_encode_timestamp_from_numeric(self):
value = encode.timestamp(1555574815)
self.assertEqual(value, b'\x00\x00\x00\x00\\\xb80\x1f')

def test_encode_timestamp_error(self):
self.assertRaises(TypeError, encode.timestamp, 'hi')


def test_encode_field_array(self):
expectation = (b'\x00\x00\x006s\x00\x01u\xaf\xc8S\x00\x00\x00\x04TestT'
b'\x00\x00\x00\x00Ec)\x92I\xbb\x9a\xca\x00D\x02\x00\x00'
Expand Down

0 comments on commit bd13a60

Please sign in to comment.