| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Tests for the HFS time implementation.""" |
| 3 |
|
| 4 |
import decimal |
| 5 |
import unittest |
| 6 |
|
| 7 |
from dfdatetime import hfs_time |
| 8 |
|
| 9 |
|
| 10 |
class HFSTimeEpochTest(unittest.TestCase): |
| 11 |
"""Tests for the HFS time epoch.""" |
| 12 |
|
| 13 |
def testInitialize(self): |
| 14 |
"""Tests the __init__ function.""" |
| 15 |
hfs_time_epoch = hfs_time.HFSTimeEpoch() |
| 16 |
self.assertIsNotNone(hfs_time_epoch) |
| 17 |
|
| 18 |
|
| 19 |
class HFSTimeTest(unittest.TestCase): |
| 20 |
"""Tests for the HFS timestamp.""" |
| 21 |
|
| 22 |
# pylint: disable=protected-access |
| 23 |
|
| 24 |
def testProperties(self): |
| 25 |
"""Tests the properties.""" |
| 26 |
hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) |
| 27 |
self.assertEqual(hfs_time_object.timestamp, 3458215528) |
| 28 |
|
| 29 |
hfs_time_object = hfs_time.HFSTime() |
| 30 |
self.assertIsNone(hfs_time_object.timestamp) |
| 31 |
|
| 32 |
def testGetNormalizedTimestamp(self): |
| 33 |
"""Tests the _GetNormalizedTimestamp function.""" |
| 34 |
hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) |
| 35 |
|
| 36 |
normalized_timestamp = hfs_time_object._GetNormalizedTimestamp() |
| 37 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1375370728.0")) |
| 38 |
|
| 39 |
hfs_time_object = hfs_time.HFSTime(time_zone_offset=60, timestamp=3458215528) |
| 40 |
|
| 41 |
normalized_timestamp = hfs_time_object._GetNormalizedTimestamp() |
| 42 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1375367128.0")) |
| 43 |
|
| 44 |
hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) |
| 45 |
hfs_time_object.time_zone_offset = 60 |
| 46 |
|
| 47 |
normalized_timestamp = hfs_time_object._GetNormalizedTimestamp() |
| 48 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1375367128.0")) |
| 49 |
|
| 50 |
hfs_time_object = hfs_time.HFSTime(timestamp=0x1FFFFFFFF) |
| 51 |
|
| 52 |
normalized_timestamp = hfs_time_object._GetNormalizedTimestamp() |
| 53 |
self.assertIsNone(normalized_timestamp) |
| 54 |
|
| 55 |
hfs_time_object = hfs_time.HFSTime(timestamp=-0x1FFFFFFFF) |
| 56 |
|
| 57 |
normalized_timestamp = hfs_time_object._GetNormalizedTimestamp() |
| 58 |
self.assertIsNone(normalized_timestamp) |
| 59 |
|
| 60 |
hfs_time_object = hfs_time.HFSTime() |
| 61 |
|
| 62 |
normalized_timestamp = hfs_time_object._GetNormalizedTimestamp() |
| 63 |
self.assertIsNone(normalized_timestamp) |
| 64 |
|
| 65 |
def testCopyFromDateTimeString(self): |
| 66 |
"""Tests the CopyFromDateTimeString function.""" |
| 67 |
hfs_time_object = hfs_time.HFSTime() |
| 68 |
|
| 69 |
hfs_time_object.CopyFromDateTimeString("2013-08-01") |
| 70 |
self.assertEqual(hfs_time_object._timestamp, 3458160000) |
| 71 |
self.assertEqual(hfs_time_object._time_zone_offset, None) |
| 72 |
|
| 73 |
hfs_time_object.CopyFromDateTimeString("2013-08-01 15:25:28") |
| 74 |
self.assertEqual(hfs_time_object._timestamp, 3458215528) |
| 75 |
self.assertEqual(hfs_time_object._time_zone_offset, None) |
| 76 |
|
| 77 |
hfs_time_object.CopyFromDateTimeString("2013-08-01 15:25:28.546875") |
| 78 |
self.assertEqual(hfs_time_object._timestamp, 3458215528) |
| 79 |
self.assertEqual(hfs_time_object._time_zone_offset, None) |
| 80 |
|
| 81 |
hfs_time_object.CopyFromDateTimeString("2013-08-01 15:25:28.546875-01:00") |
| 82 |
self.assertEqual(hfs_time_object._timestamp, 3458215528) |
| 83 |
self.assertEqual(hfs_time_object._time_zone_offset, -60) |
| 84 |
|
| 85 |
hfs_time_object.CopyFromDateTimeString("2013-08-01 15:25:28.546875+01:00") |
| 86 |
self.assertEqual(hfs_time_object._timestamp, 3458215528) |
| 87 |
self.assertEqual(hfs_time_object._time_zone_offset, 60) |
| 88 |
|
| 89 |
hfs_time_object.CopyFromDateTimeString("1904-01-02 00:00:00") |
| 90 |
self.assertEqual(hfs_time_object._timestamp, 86400) |
| 91 |
self.assertEqual(hfs_time_object._time_zone_offset, None) |
| 92 |
|
| 93 |
with self.assertRaises(ValueError): |
| 94 |
hfs_time_object.CopyFromDateTimeString("1600-01-02 00:00:00") |
| 95 |
|
| 96 |
def testCopyToDateTimeString(self): |
| 97 |
"""Tests the CopyToDateTimeString function.""" |
| 98 |
hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) |
| 99 |
|
| 100 |
date_time_string = hfs_time_object.CopyToDateTimeString() |
| 101 |
self.assertEqual(date_time_string, "2013-08-01 15:25:28") |
| 102 |
|
| 103 |
hfs_time_object = hfs_time.HFSTime() |
| 104 |
|
| 105 |
date_time_string = hfs_time_object.CopyToDateTimeString() |
| 106 |
self.assertIsNone(date_time_string) |
| 107 |
|
| 108 |
def testCopyToDateTimeStringISO8601(self): |
| 109 |
"""Tests the CopyToDateTimeStringISO8601 function.""" |
| 110 |
hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) |
| 111 |
|
| 112 |
date_time_string = hfs_time_object.CopyToDateTimeStringISO8601() |
| 113 |
self.assertEqual(date_time_string, "2013-08-01T15:25:28+00:00") |
| 114 |
|
| 115 |
def testCopyToSerializableDict(self): |
| 116 |
"""Test the CopyToSerializableDict function.""" |
| 117 |
hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) |
| 118 |
|
| 119 |
expected_serializable_dict = { |
| 120 |
"__class_name__": "HFSTime", |
| 121 |
"__type__": "DateTimeValues", |
| 122 |
"timestamp": 3458215528, |
| 123 |
} |
| 124 |
serializable_dict = hfs_time_object.CopyToSerializableDict() |
| 125 |
self.assertEqual(serializable_dict, expected_serializable_dict) |
| 126 |
|
| 127 |
def testGetDate(self): |
| 128 |
"""Tests the GetDate function.""" |
| 129 |
hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) |
| 130 |
|
| 131 |
date_tuple = hfs_time_object.GetDate() |
| 132 |
self.assertEqual(date_tuple, (2013, 8, 1)) |
| 133 |
|
| 134 |
hfs_time_object = hfs_time.HFSTime() |
| 135 |
|
| 136 |
date_tuple = hfs_time_object.GetDate() |
| 137 |
self.assertEqual(date_tuple, (None, None, None)) |
| 138 |
|
| 139 |
def testGetDateWithTimeOfDay(self): |
| 140 |
"""Tests the GetDateWithTimeOfDay function.""" |
| 141 |
hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) |
| 142 |
|
| 143 |
date_with_time_of_day_tuple = hfs_time_object.GetDateWithTimeOfDay() |
| 144 |
self.assertEqual(date_with_time_of_day_tuple, (2013, 8, 1, 15, 25, 28)) |
| 145 |
|
| 146 |
hfs_time_object = hfs_time.HFSTime() |
| 147 |
|
| 148 |
date_with_time_of_day_tuple = hfs_time_object.GetDateWithTimeOfDay() |
| 149 |
self.assertEqual( |
| 150 |
date_with_time_of_day_tuple, (None, None, None, None, None, None) |
| 151 |
) |
| 152 |
|
| 153 |
def testGetTimeOfDay(self): |
| 154 |
"""Tests the GetTimeOfDay function.""" |
| 155 |
hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) |
| 156 |
|
| 157 |
time_of_day_tuple = hfs_time_object.GetTimeOfDay() |
| 158 |
self.assertEqual(time_of_day_tuple, (15, 25, 28)) |
| 159 |
|
| 160 |
hfs_time_object = hfs_time.HFSTime() |
| 161 |
|
| 162 |
time_of_day_tuple = hfs_time_object.GetTimeOfDay() |
| 163 |
self.assertEqual(time_of_day_tuple, (None, None, None)) |
| 164 |
|
| 165 |
|
| 166 |
if __name__ == "__main__": |
| 167 |
unittest.main() |