1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the Delphi TDateTime implementation."""
import decimal
import unittest
from dfdatetime import delphi_date_time
class DelphiDateTimeEpochTest(unittest.TestCase):
"""Tests for the Delphi TDateTime epoch."""
def testInitialize(self):
"""Tests the __init__ function."""
delphi_date_time_epoch = delphi_date_time.DelphiDateTimeEpoch()
self.assertIsNotNone(delphi_date_time_epoch)
class DelphiDateTimeInvalidYear(delphi_date_time.DelphiDateTime):
"""Delphi TDateTime timestamp for testing invalid year."""
def _CopyDateTimeFromString(self, time_string):
"""Copies a date and time from a string.
Args:
time_string (str): date and time value formatted as:
YYYY-MM-DD hh:mm:ss.######[+-]##:##
Where # are numeric digits ranging from 0 to 9 and the seconds
fraction can be either 3 or 6 digits. The time of day, seconds
fraction and time zone offset are optional. The default time zone
is UTC.
Returns:
dict[str, int]: date and time values, such as year, month, day of month,
hours, minutes, seconds, microseconds.
Raises:
ValueError: if the time string is invalid or not supported.
"""
return {
'year': 10000,
'month': 1,
'day_of_month': 2,
'hours': 0,
'minutes': 0,
'seconds': 0}
class DelphiDateTimeTest(unittest.TestCase):
"""Tests for the Delphi TDateTime timestamp."""
# pylint: disable=protected-access
def testProperties(self):
"""Tests the properties."""
delphi_date_time_object = delphi_date_time.DelphiDateTime(
timestamp=41443.8263953)
self.assertEqual(delphi_date_time_object.timestamp, 41443.8263953)
delphi_date_time_object = delphi_date_time.DelphiDateTime()
self.assertIsNone(delphi_date_time_object.timestamp)
def testGetNormalizedTimestamp(self):
"""Tests the _GetNormalizedTimestamp function."""
delphi_date_time_object = delphi_date_time.DelphiDateTime(
timestamp=41443.8263953)
expected_normalized_timestamp = decimal.Decimal(
'1371585000.553919887170195579')
normalized_timestamp = delphi_date_time_object._GetNormalizedTimestamp()
self.assertEqual(normalized_timestamp, expected_normalized_timestamp)
delphi_date_time_object = delphi_date_time.DelphiDateTime(
time_zone_offset=60, timestamp=41443.8263953)
expected_normalized_timestamp = decimal.Decimal(
'1371581400.553919887170195579')
normalized_timestamp = delphi_date_time_object._GetNormalizedTimestamp()
self.assertEqual(normalized_timestamp, expected_normalized_timestamp)
delphi_date_time_object = delphi_date_time.DelphiDateTime(
timestamp=41443.8263953)
delphi_date_time_object.time_zone_offset = 60
expected_normalized_timestamp = decimal.Decimal(
'1371581400.553919887170195579')
normalized_timestamp = delphi_date_time_object._GetNormalizedTimestamp()
self.assertEqual(normalized_timestamp, expected_normalized_timestamp)
delphi_date_time_object = delphi_date_time.DelphiDateTime()
normalized_timestamp = delphi_date_time_object._GetNormalizedTimestamp()
self.assertIsNone(normalized_timestamp)
def testCopyFromDateTimeString(self):
"""Tests the CopyFromDateTimeString function."""
delphi_date_time_object = delphi_date_time.DelphiDateTime()
delphi_date_time_object.CopyFromDateTimeString('2013-06-18')
self.assertEqual(delphi_date_time_object._timestamp, 41443.0)
self.assertEqual(delphi_date_time_object._time_zone_offset, None)
delphi_date_time_object.CopyFromDateTimeString('2013-06-18 19:50:00')
self.assertEqual(delphi_date_time_object._timestamp, 41443.82638888889)
self.assertEqual(delphi_date_time_object._time_zone_offset, None)
delphi_date_time_object.CopyFromDateTimeString('2013-06-18 19:50:00.546875')
self.assertEqual(delphi_date_time_object._timestamp, 41443.826395218464)
self.assertEqual(delphi_date_time_object._time_zone_offset, None)
delphi_date_time_object.CopyFromDateTimeString(
'2013-06-18 19:50:00.546875-01:00')
self.assertEqual(delphi_date_time_object._timestamp, 41443.826395218464)
self.assertEqual(delphi_date_time_object._time_zone_offset, -60)
delphi_date_time_object.CopyFromDateTimeString(
'2013-06-18 19:50:00.546875+01:00')
self.assertEqual(delphi_date_time_object._timestamp, 41443.826395218464)
self.assertEqual(delphi_date_time_object._time_zone_offset, 60)
delphi_date_time_object.CopyFromDateTimeString('1899-12-31 00:00:00')
self.assertEqual(delphi_date_time_object._timestamp, 1.0)
self.assertEqual(delphi_date_time_object._time_zone_offset, None)
delphi_date_time_object = DelphiDateTimeInvalidYear()
with self.assertRaises(ValueError):
delphi_date_time_object.CopyFromDateTimeString('9999-01-02 00:00:00')
def testCopyToDateTimeString(self):
"""Tests the CopyToDateTimeString function."""
delphi_date_time_object = delphi_date_time.DelphiDateTime(
timestamp=41443.8263953)
date_time_string = delphi_date_time_object.CopyToDateTimeString()
self.assertEqual(date_time_string, '2013-06-18 19:50:00.553919')
delphi_date_time_object = delphi_date_time.DelphiDateTime()
date_time_string = delphi_date_time_object.CopyToDateTimeString()
self.assertIsNone(date_time_string)
def testCopyToDateTimeStringISO8601(self):
"""Tests the CopyToDateTimeStringISO8601 function."""
delphi_date_time_object = delphi_date_time.DelphiDateTime(
timestamp=41443.8263953)
date_time_string = delphi_date_time_object.CopyToDateTimeStringISO8601()
self.assertEqual(date_time_string, '2013-06-18T19:50:00.553919+00:00')
delphi_date_time_object = delphi_date_time.DelphiDateTime(
timestamp=8.0e+174)
date_time_string = delphi_date_time_object.CopyToDateTimeStringISO8601()
self.assertIsNone(date_time_string)
def testGetDate(self):
"""Tests the GetDate function."""
delphi_date_time_object = delphi_date_time.DelphiDateTime(
timestamp=41443.8263953)
date_tuple = delphi_date_time_object.GetDate()
self.assertEqual(date_tuple, (2013, 6, 18))
delphi_date_time_object = delphi_date_time.DelphiDateTime()
date_tuple = delphi_date_time_object.GetDate()
self.assertEqual(date_tuple, (None, None, None))
def testGetDateWithTimeOfDay(self):
"""Tests the GetDateWithTimeOfDay function."""
delphi_date_time_object = delphi_date_time.DelphiDateTime(
timestamp=41443.8263953)
date_with_time_of_day_tuple = delphi_date_time_object.GetDateWithTimeOfDay()
self.assertEqual(date_with_time_of_day_tuple, (2013, 6, 18, 19, 50, 0))
delphi_date_time_object = delphi_date_time.DelphiDateTime()
date_with_time_of_day_tuple = delphi_date_time_object.GetDateWithTimeOfDay()
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(
timestamp=41443.8263953)
time_of_day_tuple = delphi_date_time_object.GetTimeOfDay()
self.assertEqual(time_of_day_tuple, (19, 50, 0))
delphi_date_time_object = delphi_date_time.DelphiDateTime()
time_of_day_tuple = delphi_date_time_object.GetTimeOfDay()
self.assertEqual(time_of_day_tuple, (None, None, None))
if __name__ == '__main__':
unittest.main()
|