Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dfdatetime/golang_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _GetNumberOfSeconds(self, golang_timestamp):

# TODO: add support for version 2 time zone offset in seconds

except struct.error as exception:
except (TypeError, struct.error) as exception:
raise ValueError((
f'Unable to unpacked Golang time.Time timestamp with error: '
f'{exception!s}'))
Expand Down
9 changes: 7 additions & 2 deletions tests/golang_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def testGetNumberOfSeconds(self):
golang_time_object._GetNumberOfSeconds(golang_timestamp)

with self.assertRaises(ValueError):
golang_timestamp = bytes.fromhex('ffffffffffffffffffffffffffff01')
golang_timestamp = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
golang_time_object._GetNumberOfSeconds(golang_timestamp)

def testCopyFromDateTimeString(self):
Expand Down Expand Up @@ -174,8 +174,13 @@ def testCopyFromDateTimeString(self):
self.assertEqual(golang_time_object._nanoseconds, 567890000)
self.assertEqual(golang_time_object._time_zone_offset, 60)

# Test with invalid date string.
with self.assertRaises(ValueError):
golang_time_object.CopyFromDateTimeString('-0001-01-01')
golang_time_object.CopyFromDateTimeString('0001:01-01 00:01:00')

# Test with negative year.
with self.assertRaises(ValueError):
golang_time_object.CopyFromDateTimeString('-001-01-01 00:01:00')

def testCopyToDateTimeString(self):
"""Test the CopyToDateTimeString function."""
Expand Down
26 changes: 25 additions & 1 deletion tests/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ def testConvertDateTimeValuesToJSON(self):
rfc2579_date_time_object)
self.assertEqual(json_dict, expected_json_dict)

negative_timezone_rfc2579_object = rfc2579_date_time.RFC2579DateTime(
rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '-', 2, 0))

expected_json_dict = {
'__class_name__': 'RFC2579DateTime',
'__type__': 'DateTimeValues',
'rfc2579_date_time_tuple': (2010, 8, 12, 20, 6, 31, 6, '-', 2, 0)}

json_dict = serializer.Serializer.ConvertDateTimeValuesToJSON(
negative_timezone_rfc2579_object)
self.assertEqual(json_dict, expected_json_dict)

systemtime_object = systemtime.Systemtime(
system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142))

Expand Down Expand Up @@ -266,7 +278,8 @@ def testConvertJSONToDateTimeValues(self):
'__class_name__': 'GolangTime',
'__type__': 'DateTimeValues',
'golang_timestamp': (
b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03\xff\xff')}
b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03\xff\xff'),
'time_zone_offset': 60}

golang_timestamp = bytes.fromhex('01000000000000000200000003ffff')
expected_date_time_object = golang_time.GolangTime(
Expand Down Expand Up @@ -338,6 +351,17 @@ def testConvertJSONToDateTimeValues(self):
json_dict)
self.assertEqual(date_time_object, expected_date_time_object)

# Test if is_delta is removed.
json_dict = {
'__class_name__': 'PosixTime',
'__type__': 'DateTimeValues',
'timestamp': 1281643591,
'is_delta': True}

date_time_object = serializer.Serializer.ConvertJSONToDateTimeValues(
json_dict)
self.assertFalse(date_time_object.is_delta)

with self.assertRaises(KeyError):
json_dict = {
'__class_name__': 'UnknownType', '__type__': 'DateTimeValues'}
Expand Down
27 changes: 27 additions & 0 deletions tests/time_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,33 @@ def testCopyTimeFromStringRFC(self):
with self.assertRaises(ValueError):
time_elements_object._CopyTimeFromStringRFC('11:57:09', 'XXX')

with self.assertRaises(ValueError):
time_elements_object._CopyTimeFromStringRFC('12:34:56:78', 'GMT')

with self.assertRaises(ValueError):
time_elements_object._CopyTimeFromStringRFC('12X34:56', 'GMT')

with self.assertRaises(ValueError):
time_elements_object._CopyTimeFromStringRFC('12:34X56', 'GMT')

with self.assertRaises(ValueError):
time_elements_object._CopyTimeFromStringRFC('11:57', '+01000')

with self.assertRaises(ValueError):
time_elements_object._CopyTimeFromStringRFC('11:57', 'ZZZ')

with self.assertRaises(ValueError):
time_elements_object._CopyTimeFromStringRFC('11:57', '+A500')

with self.assertRaises(ValueError):
time_elements_object._CopyTimeFromStringRFC('11:57', '+1600')

with self.assertRaises(ValueError):
time_elements_object._CopyTimeFromStringRFC('11:57', '+01A0')

with self.assertRaises(ValueError):
time_elements_object._CopyTimeFromStringRFC('11:57', '+0160')

def testCopyFromDatetime(self):
"""Tests the CopyFromDatetime function."""
time_elements_object = time_elements.TimeElements()
Expand Down
Loading