| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Tests for the POSIX time implementation.""" |
| 3 |
|
| 4 |
import decimal |
| 5 |
import unittest |
| 6 |
|
| 7 |
from dfdatetime import posix_time |
| 8 |
|
| 9 |
|
| 10 |
class PosixTimeEpochTest(unittest.TestCase): |
| 11 |
"""Tests for the POSIX time epoch.""" |
| 12 |
|
| 13 |
def testInitialize(self): |
| 14 |
"""Tests the __init__ function.""" |
| 15 |
posix_epoch = posix_time.PosixTimeEpoch() |
| 16 |
self.assertIsNotNone(posix_epoch) |
| 17 |
|
| 18 |
|
| 19 |
class PosixTimeTest(unittest.TestCase): |
| 20 |
"""Tests for the POSIX timestamp.""" |
| 21 |
|
| 22 |
# pylint: disable=protected-access |
| 23 |
|
| 24 |
def testProperties(self): |
| 25 |
"""Tests the properties.""" |
| 26 |
posix_time_object = posix_time.PosixTime(timestamp=1281643591) |
| 27 |
self.assertEqual(posix_time_object.timestamp, 1281643591) |
| 28 |
|
| 29 |
posix_time_object = posix_time.PosixTime() |
| 30 |
self.assertIsNone(posix_time_object.timestamp) |
| 31 |
|
| 32 |
def testGetNormalizedTimestamp(self): |
| 33 |
"""Tests the _GetNormalizedTimestamp function.""" |
| 34 |
posix_time_object = posix_time.PosixTime(timestamp=1281643591) |
| 35 |
|
| 36 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 37 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.0")) |
| 38 |
|
| 39 |
posix_time_object = posix_time.PosixTime( |
| 40 |
time_zone_offset=60, timestamp=1281643591 |
| 41 |
) |
| 42 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 43 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.0")) |
| 44 |
|
| 45 |
posix_time_object = posix_time.PosixTime(timestamp=1281643591) |
| 46 |
posix_time_object.time_zone_offset = 60 |
| 47 |
|
| 48 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 49 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.0")) |
| 50 |
|
| 51 |
posix_time_object = posix_time.PosixTime() |
| 52 |
|
| 53 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 54 |
self.assertIsNone(normalized_timestamp) |
| 55 |
|
| 56 |
def testCopyFromDateTimeString(self): |
| 57 |
"""Tests the CopyFromDateTimeString function.""" |
| 58 |
posix_time_object = posix_time.PosixTime() |
| 59 |
|
| 60 |
posix_time_object.CopyFromDateTimeString("2010-08-12") |
| 61 |
self.assertEqual(posix_time_object._timestamp, 1281571200) |
| 62 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 63 |
|
| 64 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31") |
| 65 |
self.assertEqual(posix_time_object._timestamp, 1281647191) |
| 66 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 67 |
|
| 68 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.546875") |
| 69 |
self.assertEqual(posix_time_object._timestamp, 1281647191) |
| 70 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 71 |
|
| 72 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.546875-01:00") |
| 73 |
self.assertEqual(posix_time_object._timestamp, 1281647191) |
| 74 |
self.assertEqual(posix_time_object._time_zone_offset, -60) |
| 75 |
|
| 76 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.546875+01:00") |
| 77 |
self.assertEqual(posix_time_object._timestamp, 1281647191) |
| 78 |
self.assertEqual(posix_time_object._time_zone_offset, 60) |
| 79 |
|
| 80 |
posix_time_object.CopyFromDateTimeString("1601-01-02 00:00:00") |
| 81 |
self.assertEqual(posix_time_object._timestamp, -11644387200) |
| 82 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 83 |
|
| 84 |
def testCopyToDateTimeString(self): |
| 85 |
"""Tests the CopyToDateTimeString function.""" |
| 86 |
posix_time_object = posix_time.PosixTime(timestamp=1281643591) |
| 87 |
|
| 88 |
date_time_string = posix_time_object.CopyToDateTimeString() |
| 89 |
self.assertEqual(date_time_string, "2010-08-12 20:06:31") |
| 90 |
|
| 91 |
posix_time_object = posix_time.PosixTime() |
| 92 |
|
| 93 |
date_time_string = posix_time_object.CopyToDateTimeString() |
| 94 |
self.assertIsNone(date_time_string) |
| 95 |
|
| 96 |
def testCopyToDateTimeStringISO8601(self): |
| 97 |
"""Tests the CopyToDateTimeStringISO8601 function.""" |
| 98 |
posix_time_object = posix_time.PosixTime(timestamp=1281643591) |
| 99 |
|
| 100 |
date_time_string = posix_time_object.CopyToDateTimeStringISO8601() |
| 101 |
self.assertEqual(date_time_string, "2010-08-12T20:06:31+00:00") |
| 102 |
|
| 103 |
posix_time_object = posix_time.PosixTime(timestamp=-11644468446) |
| 104 |
|
| 105 |
date_time_string = posix_time_object.CopyToDateTimeStringISO8601() |
| 106 |
self.assertEqual(date_time_string, "1601-01-01T01:25:54+00:00") |
| 107 |
|
| 108 |
def testCopyToPosixTimestampWithFractionOfSecond(self): |
| 109 |
"""Tests the CopyToPosixTimestampWithFractionOfSecond function.""" |
| 110 |
posix_time_object = posix_time.PosixTime(timestamp=1281643591) |
| 111 |
|
| 112 |
posix_timestamp, fraction_of_second = ( |
| 113 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 114 |
) |
| 115 |
self.assertEqual(posix_timestamp, 1281643591) |
| 116 |
self.assertIsNone(fraction_of_second) |
| 117 |
|
| 118 |
posix_time_object = posix_time.PosixTime(timestamp=-11644468446) |
| 119 |
|
| 120 |
posix_timestamp, fraction_of_second = ( |
| 121 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 122 |
) |
| 123 |
self.assertEqual(posix_timestamp, -11644468446) |
| 124 |
self.assertIsNone(fraction_of_second) |
| 125 |
|
| 126 |
posix_time_object = posix_time.PosixTime() |
| 127 |
|
| 128 |
posix_timestamp, fraction_of_second = ( |
| 129 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 130 |
) |
| 131 |
self.assertIsNone(posix_timestamp) |
| 132 |
self.assertIsNone(fraction_of_second) |
| 133 |
|
| 134 |
def testCopyToSerializableDict(self): |
| 135 |
"""Test the CopyToSerializableDict function.""" |
| 136 |
posix_time_object = posix_time.PosixTime(timestamp=1281643591) |
| 137 |
|
| 138 |
expected_serializable_dict = { |
| 139 |
"__class_name__": "PosixTime", |
| 140 |
"__type__": "DateTimeValues", |
| 141 |
"timestamp": 1281643591, |
| 142 |
} |
| 143 |
serializable_dict = posix_time_object.CopyToSerializableDict() |
| 144 |
self.assertEqual(serializable_dict, expected_serializable_dict) |
| 145 |
|
| 146 |
def testGetDate(self): |
| 147 |
"""Tests the GetDate function.""" |
| 148 |
posix_time_object = posix_time.PosixTime(timestamp=1281643591) |
| 149 |
|
| 150 |
date_tuple = posix_time_object.GetDate() |
| 151 |
self.assertEqual(date_tuple, (2010, 8, 12)) |
| 152 |
|
| 153 |
posix_time_object = posix_time.PosixTime() |
| 154 |
|
| 155 |
date_tuple = posix_time_object.GetDate() |
| 156 |
self.assertEqual(date_tuple, (None, None, None)) |
| 157 |
|
| 158 |
def testGetDateWithTimeOfDay(self): |
| 159 |
"""Tests the GetDateWithTimeOfDay function.""" |
| 160 |
posix_time_object = posix_time.PosixTime(timestamp=1281643591) |
| 161 |
|
| 162 |
date_with_time_of_day_tuple = posix_time_object.GetDateWithTimeOfDay() |
| 163 |
self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) |
| 164 |
|
| 165 |
posix_time_object = posix_time.PosixTime() |
| 166 |
|
| 167 |
date_with_time_of_day_tuple = posix_time_object.GetDateWithTimeOfDay() |
| 168 |
self.assertEqual( |
| 169 |
date_with_time_of_day_tuple, (None, None, None, None, None, None) |
| 170 |
) |
| 171 |
|
| 172 |
def testGetTimeOfDay(self): |
| 173 |
"""Tests the GetTimeOfDay function.""" |
| 174 |
posix_time_object = posix_time.PosixTime(timestamp=1281643591) |
| 175 |
|
| 176 |
time_of_day_tuple = posix_time_object.GetTimeOfDay() |
| 177 |
self.assertEqual(time_of_day_tuple, (20, 6, 31)) |
| 178 |
|
| 179 |
posix_time_object = posix_time.PosixTime() |
| 180 |
|
| 181 |
time_of_day_tuple = posix_time_object.GetTimeOfDay() |
| 182 |
self.assertEqual(time_of_day_tuple, (None, None, None)) |
| 183 |
|
| 184 |
|
| 185 |
class PosixTimeInMillisecondsTest(unittest.TestCase): |
| 186 |
"""Tests for the POSIX timestamp in milliseconds.""" |
| 187 |
|
| 188 |
# pylint: disable=protected-access |
| 189 |
|
| 190 |
def testProperties(self): |
| 191 |
"""Tests the properties.""" |
| 192 |
posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) |
| 193 |
self.assertEqual(posix_time_object.timestamp, 1281643591546) |
| 194 |
|
| 195 |
posix_time_object = posix_time.PosixTimeInMilliseconds() |
| 196 |
self.assertIsNone(posix_time_object.timestamp) |
| 197 |
|
| 198 |
def testGetNormalizedTimestamp(self): |
| 199 |
"""Tests the _GetNormalizedTimestamp function.""" |
| 200 |
posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) |
| 201 |
|
| 202 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 203 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.546")) |
| 204 |
|
| 205 |
posix_time_object = posix_time.PosixTimeInMilliseconds( |
| 206 |
time_zone_offset=60, timestamp=1281643591546 |
| 207 |
) |
| 208 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 209 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.546")) |
| 210 |
|
| 211 |
posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) |
| 212 |
posix_time_object.time_zone_offset = 60 |
| 213 |
|
| 214 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 215 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.546")) |
| 216 |
|
| 217 |
posix_time_object = posix_time.PosixTimeInMilliseconds() |
| 218 |
|
| 219 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 220 |
self.assertIsNone(normalized_timestamp) |
| 221 |
|
| 222 |
# pylint: disable=protected-access |
| 223 |
|
| 224 |
def testCopyFromDateTimeString(self): |
| 225 |
"""Tests the CopyFromDateTimeString function.""" |
| 226 |
posix_time_object = posix_time.PosixTimeInMilliseconds() |
| 227 |
|
| 228 |
posix_time_object.CopyFromDateTimeString("2010-08-12") |
| 229 |
self.assertEqual(posix_time_object._timestamp, 1281571200000) |
| 230 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 231 |
|
| 232 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31") |
| 233 |
self.assertEqual(posix_time_object._timestamp, 1281647191000) |
| 234 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 235 |
|
| 236 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.546") |
| 237 |
self.assertEqual(posix_time_object._timestamp, 1281647191546) |
| 238 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 239 |
|
| 240 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.546-01:00") |
| 241 |
self.assertEqual(posix_time_object._timestamp, 1281647191546) |
| 242 |
self.assertEqual(posix_time_object._time_zone_offset, -60) |
| 243 |
|
| 244 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.546+01:00") |
| 245 |
self.assertEqual(posix_time_object._timestamp, 1281647191546) |
| 246 |
self.assertEqual(posix_time_object._time_zone_offset, 60) |
| 247 |
|
| 248 |
posix_time_object.CopyFromDateTimeString("1601-01-02 00:00:00") |
| 249 |
self.assertEqual(posix_time_object._timestamp, -11644387200000) |
| 250 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 251 |
|
| 252 |
def testCopyToDateTimeString(self): |
| 253 |
"""Tests the CopyToDateTimeString function.""" |
| 254 |
posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) |
| 255 |
|
| 256 |
date_time_string = posix_time_object.CopyToDateTimeString() |
| 257 |
self.assertEqual(date_time_string, "2010-08-12 20:06:31.546") |
| 258 |
|
| 259 |
posix_time_object = posix_time.PosixTimeInMilliseconds() |
| 260 |
|
| 261 |
date_time_string = posix_time_object.CopyToDateTimeString() |
| 262 |
self.assertIsNone(date_time_string) |
| 263 |
|
| 264 |
def testCopyToDateTimeStringISO8601(self): |
| 265 |
"""Tests the CopyToDateTimeStringISO8601 function.""" |
| 266 |
posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) |
| 267 |
|
| 268 |
date_time_string = posix_time_object.CopyToDateTimeStringISO8601() |
| 269 |
self.assertEqual(date_time_string, "2010-08-12T20:06:31.546+00:00") |
| 270 |
|
| 271 |
posix_time_object = posix_time.PosixTimeInMilliseconds( |
| 272 |
timestamp=-11644468446327 |
| 273 |
) |
| 274 |
date_time_string = posix_time_object.CopyToDateTimeStringISO8601() |
| 275 |
self.assertEqual(date_time_string, "1601-01-01T01:25:53.673+00:00") |
| 276 |
|
| 277 |
def testCopyToPosixTimestampWithFractionOfSecond(self): |
| 278 |
"""Tests the CopyToPosixTimestampWithFractionOfSecond function.""" |
| 279 |
posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) |
| 280 |
|
| 281 |
posix_timestamp, fraction_of_second = ( |
| 282 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 283 |
) |
| 284 |
self.assertEqual(posix_timestamp, 1281643591) |
| 285 |
self.assertEqual(fraction_of_second, 546) |
| 286 |
|
| 287 |
posix_time_object = posix_time.PosixTimeInMilliseconds( |
| 288 |
timestamp=-11644468446327 |
| 289 |
) |
| 290 |
posix_timestamp, fraction_of_second = ( |
| 291 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 292 |
) |
| 293 |
self.assertEqual(posix_timestamp, -11644468446) |
| 294 |
self.assertEqual(fraction_of_second, 327) |
| 295 |
|
| 296 |
posix_time_object = posix_time.PosixTimeInMilliseconds() |
| 297 |
|
| 298 |
posix_timestamp, fraction_of_second = ( |
| 299 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 300 |
) |
| 301 |
self.assertIsNone(posix_timestamp) |
| 302 |
self.assertIsNone(fraction_of_second) |
| 303 |
|
| 304 |
def testCopyToSerializableDict(self): |
| 305 |
"""Test the CopyToSerializableDict function.""" |
| 306 |
posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) |
| 307 |
|
| 308 |
expected_serializable_dict = { |
| 309 |
"__class_name__": "PosixTimeInMilliseconds", |
| 310 |
"__type__": "DateTimeValues", |
| 311 |
"timestamp": 1281643591546, |
| 312 |
} |
| 313 |
serializable_dict = posix_time_object.CopyToSerializableDict() |
| 314 |
self.assertEqual(serializable_dict, expected_serializable_dict) |
| 315 |
|
| 316 |
def testGetDate(self): |
| 317 |
"""Tests the GetDate function.""" |
| 318 |
posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) |
| 319 |
|
| 320 |
date_tuple = posix_time_object.GetDate() |
| 321 |
self.assertEqual(date_tuple, (2010, 8, 12)) |
| 322 |
|
| 323 |
posix_time_object = posix_time.PosixTimeInMilliseconds() |
| 324 |
|
| 325 |
date_tuple = posix_time_object.GetDate() |
| 326 |
self.assertEqual(date_tuple, (None, None, None)) |
| 327 |
|
| 328 |
def testGetDateWithTimeOfDay(self): |
| 329 |
"""Tests the GetDateWithTimeOfDay function.""" |
| 330 |
posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) |
| 331 |
|
| 332 |
date_with_time_of_day_tuple = posix_time_object.GetDateWithTimeOfDay() |
| 333 |
self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) |
| 334 |
|
| 335 |
posix_time_object = posix_time.PosixTimeInMilliseconds() |
| 336 |
|
| 337 |
date_with_time_of_day_tuple = posix_time_object.GetDateWithTimeOfDay() |
| 338 |
self.assertEqual( |
| 339 |
date_with_time_of_day_tuple, (None, None, None, None, None, None) |
| 340 |
) |
| 341 |
|
| 342 |
def testGetTimeOfDay(self): |
| 343 |
"""Tests the GetTimeOfDay function.""" |
| 344 |
posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) |
| 345 |
|
| 346 |
time_of_day_tuple = posix_time_object.GetTimeOfDay() |
| 347 |
self.assertEqual(time_of_day_tuple, (20, 6, 31)) |
| 348 |
|
| 349 |
posix_time_object = posix_time.PosixTimeInMilliseconds() |
| 350 |
|
| 351 |
time_of_day_tuple = posix_time_object.GetTimeOfDay() |
| 352 |
self.assertEqual(time_of_day_tuple, (None, None, None)) |
| 353 |
|
| 354 |
|
| 355 |
class PosixTimeInMicrosecondsTest(unittest.TestCase): |
| 356 |
"""Tests for the POSIX timestamp in microseconds.""" |
| 357 |
|
| 358 |
# pylint: disable=protected-access |
| 359 |
|
| 360 |
def testProperties(self): |
| 361 |
"""Tests the properties.""" |
| 362 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 363 |
timestamp=1281643591546875 |
| 364 |
) |
| 365 |
self.assertEqual(posix_time_object.timestamp, 1281643591546875) |
| 366 |
|
| 367 |
posix_time_object = posix_time.PosixTimeInMicroseconds() |
| 368 |
self.assertIsNone(posix_time_object.timestamp) |
| 369 |
|
| 370 |
def testGetNormalizedTimestamp(self): |
| 371 |
"""Tests the _GetNormalizedTimestamp function.""" |
| 372 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 373 |
timestamp=1281643591546875 |
| 374 |
) |
| 375 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 376 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.546875")) |
| 377 |
|
| 378 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 379 |
time_zone_offset=60, timestamp=1281643591546875 |
| 380 |
) |
| 381 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 382 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.546875")) |
| 383 |
|
| 384 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 385 |
timestamp=1281643591546875 |
| 386 |
) |
| 387 |
posix_time_object.time_zone_offset = 60 |
| 388 |
|
| 389 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 390 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.546875")) |
| 391 |
|
| 392 |
posix_time_object = posix_time.PosixTimeInMicroseconds() |
| 393 |
|
| 394 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 395 |
self.assertIsNone(normalized_timestamp) |
| 396 |
|
| 397 |
# pylint: disable=protected-access |
| 398 |
|
| 399 |
def testCopyFromDateTimeString(self): |
| 400 |
"""Tests the CopyFromDateTimeString function.""" |
| 401 |
posix_time_object = posix_time.PosixTimeInMicroseconds() |
| 402 |
|
| 403 |
posix_time_object.CopyFromDateTimeString("2010-08-12") |
| 404 |
self.assertEqual(posix_time_object._timestamp, 1281571200000000) |
| 405 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 406 |
|
| 407 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31") |
| 408 |
self.assertEqual(posix_time_object._timestamp, 1281647191000000) |
| 409 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 410 |
|
| 411 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.546875") |
| 412 |
self.assertEqual(posix_time_object._timestamp, 1281647191546875) |
| 413 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 414 |
|
| 415 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.546875-01:00") |
| 416 |
self.assertEqual(posix_time_object._timestamp, 1281647191546875) |
| 417 |
self.assertEqual(posix_time_object._time_zone_offset, -60) |
| 418 |
|
| 419 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.546875+01:00") |
| 420 |
self.assertEqual(posix_time_object._timestamp, 1281647191546875) |
| 421 |
self.assertEqual(posix_time_object._time_zone_offset, 60) |
| 422 |
|
| 423 |
posix_time_object.CopyFromDateTimeString("1601-01-02 00:00:00") |
| 424 |
self.assertEqual(posix_time_object._timestamp, -11644387200000000) |
| 425 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 426 |
|
| 427 |
def testCopyToDateTimeString(self): |
| 428 |
"""Tests the CopyToDateTimeString function.""" |
| 429 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 430 |
timestamp=1281643591546875 |
| 431 |
) |
| 432 |
date_time_string = posix_time_object.CopyToDateTimeString() |
| 433 |
self.assertEqual(date_time_string, "2010-08-12 20:06:31.546875") |
| 434 |
|
| 435 |
posix_time_object = posix_time.PosixTimeInMicroseconds() |
| 436 |
|
| 437 |
date_time_string = posix_time_object.CopyToDateTimeString() |
| 438 |
self.assertIsNone(date_time_string) |
| 439 |
|
| 440 |
def testCopyToDateTimeStringISO8601(self): |
| 441 |
"""Tests the CopyToDateTimeStringISO8601 function.""" |
| 442 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 443 |
timestamp=1281643591546875 |
| 444 |
) |
| 445 |
date_time_string = posix_time_object.CopyToDateTimeStringISO8601() |
| 446 |
self.assertEqual(date_time_string, "2010-08-12T20:06:31.546875+00:00") |
| 447 |
|
| 448 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 449 |
timestamp=-11644468446327447 |
| 450 |
) |
| 451 |
date_time_string = posix_time_object.CopyToDateTimeStringISO8601() |
| 452 |
self.assertEqual(date_time_string, "1601-01-01T01:25:53.672553+00:00") |
| 453 |
|
| 454 |
def testCopyToPosixTimestampWithFractionOfSecond(self): |
| 455 |
"""Tests the CopyToPosixTimestampWithFractionOfSecond function.""" |
| 456 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 457 |
timestamp=1281643591546875 |
| 458 |
) |
| 459 |
posix_timestamp, fraction_of_second = ( |
| 460 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 461 |
) |
| 462 |
self.assertEqual(posix_timestamp, 1281643591) |
| 463 |
self.assertEqual(fraction_of_second, 546875) |
| 464 |
|
| 465 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 466 |
timestamp=-11644468446327447 |
| 467 |
) |
| 468 |
posix_timestamp, fraction_of_second = ( |
| 469 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 470 |
) |
| 471 |
self.assertEqual(posix_timestamp, -11644468446) |
| 472 |
self.assertEqual(fraction_of_second, 327447) |
| 473 |
|
| 474 |
posix_time_object = posix_time.PosixTimeInMicroseconds() |
| 475 |
|
| 476 |
posix_timestamp, fraction_of_second = ( |
| 477 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 478 |
) |
| 479 |
self.assertIsNone(posix_timestamp) |
| 480 |
self.assertIsNone(fraction_of_second) |
| 481 |
|
| 482 |
def testCopyToSerializableDict(self): |
| 483 |
"""Test the CopyToSerializableDict function.""" |
| 484 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 485 |
timestamp=1281643591546875 |
| 486 |
) |
| 487 |
expected_serializable_dict = { |
| 488 |
"__class_name__": "PosixTimeInMicroseconds", |
| 489 |
"__type__": "DateTimeValues", |
| 490 |
"timestamp": 1281643591546875, |
| 491 |
} |
| 492 |
serializable_dict = posix_time_object.CopyToSerializableDict() |
| 493 |
self.assertEqual(serializable_dict, expected_serializable_dict) |
| 494 |
|
| 495 |
def testGetDate(self): |
| 496 |
"""Tests the GetDate function.""" |
| 497 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 498 |
timestamp=1281643591546875 |
| 499 |
) |
| 500 |
date_tuple = posix_time_object.GetDate() |
| 501 |
self.assertEqual(date_tuple, (2010, 8, 12)) |
| 502 |
|
| 503 |
posix_time_object = posix_time.PosixTimeInMicroseconds() |
| 504 |
|
| 505 |
date_tuple = posix_time_object.GetDate() |
| 506 |
self.assertEqual(date_tuple, (None, None, None)) |
| 507 |
|
| 508 |
def testGetDateWithTimeOfDay(self): |
| 509 |
"""Tests the GetDateWithTimeOfDay function.""" |
| 510 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 511 |
timestamp=1281643591546875 |
| 512 |
) |
| 513 |
date_with_time_of_day_tuple = posix_time_object.GetDateWithTimeOfDay() |
| 514 |
self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) |
| 515 |
|
| 516 |
posix_time_object = posix_time.PosixTimeInMicroseconds() |
| 517 |
|
| 518 |
date_with_time_of_day_tuple = posix_time_object.GetDateWithTimeOfDay() |
| 519 |
self.assertEqual( |
| 520 |
date_with_time_of_day_tuple, (None, None, None, None, None, None) |
| 521 |
) |
| 522 |
|
| 523 |
def testGetTimeOfDay(self): |
| 524 |
"""Tests the GetTimeOfDay function.""" |
| 525 |
posix_time_object = posix_time.PosixTimeInMicroseconds( |
| 526 |
timestamp=1281643591546875 |
| 527 |
) |
| 528 |
time_of_day_tuple = posix_time_object.GetTimeOfDay() |
| 529 |
self.assertEqual(time_of_day_tuple, (20, 6, 31)) |
| 530 |
|
| 531 |
posix_time_object = posix_time.PosixTimeInMicroseconds() |
| 532 |
|
| 533 |
time_of_day_tuple = posix_time_object.GetTimeOfDay() |
| 534 |
self.assertEqual(time_of_day_tuple, (None, None, None)) |
| 535 |
|
| 536 |
|
| 537 |
class PosixTimeInNanoSecondsTest(unittest.TestCase): |
| 538 |
"""Tests for the POSIX timestamp in nanoseconds.""" |
| 539 |
|
| 540 |
# pylint: disable=protected-access |
| 541 |
|
| 542 |
def testProperties(self): |
| 543 |
"""Tests the properties.""" |
| 544 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 545 |
timestamp=1281643591987654321 |
| 546 |
) |
| 547 |
self.assertEqual(posix_time_object.timestamp, 1281643591987654321) |
| 548 |
|
| 549 |
posix_time_object = posix_time.PosixTimeInNanoseconds() |
| 550 |
self.assertIsNone(posix_time_object.timestamp) |
| 551 |
|
| 552 |
def testGetNormalizedTimestamp(self): |
| 553 |
"""Tests the _GetNormalizedTimestamp function.""" |
| 554 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 555 |
timestamp=1281643591987654321 |
| 556 |
) |
| 557 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 558 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.987654321")) |
| 559 |
|
| 560 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 561 |
time_zone_offset=60, timestamp=1281643591987654321 |
| 562 |
) |
| 563 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 564 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.987654321")) |
| 565 |
|
| 566 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 567 |
timestamp=1281643591987654321 |
| 568 |
) |
| 569 |
posix_time_object.time_zone_offset = 60 |
| 570 |
|
| 571 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 572 |
self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.987654321")) |
| 573 |
|
| 574 |
posix_time_object = posix_time.PosixTimeInNanoseconds() |
| 575 |
|
| 576 |
normalized_timestamp = posix_time_object._GetNormalizedTimestamp() |
| 577 |
self.assertIsNone(normalized_timestamp) |
| 578 |
|
| 579 |
def testCopyFromDateTimeString(self): |
| 580 |
"""Tests the CopyFromDateTimeString function.""" |
| 581 |
posix_time_object = posix_time.PosixTimeInNanoseconds() |
| 582 |
|
| 583 |
posix_time_object.CopyFromDateTimeString("2010-08-12") |
| 584 |
self.assertEqual(posix_time_object.timestamp, 1281571200000000000) |
| 585 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 586 |
|
| 587 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31") |
| 588 |
self.assertEqual(posix_time_object.timestamp, 1281647191000000000) |
| 589 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 590 |
|
| 591 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.654321") |
| 592 |
self.assertEqual(posix_time_object.timestamp, 1281647191654321000) |
| 593 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 594 |
|
| 595 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.654321-01:00") |
| 596 |
self.assertEqual(posix_time_object.timestamp, 1281647191654321000) |
| 597 |
self.assertEqual(posix_time_object._time_zone_offset, -60) |
| 598 |
|
| 599 |
posix_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.654321+01:00") |
| 600 |
self.assertEqual(posix_time_object.timestamp, 1281647191654321000) |
| 601 |
self.assertEqual(posix_time_object._time_zone_offset, 60) |
| 602 |
|
| 603 |
posix_time_object.CopyFromDateTimeString("1601-01-02 00:00:00") |
| 604 |
self.assertEqual(posix_time_object.timestamp, -11644387200000000000) |
| 605 |
self.assertEqual(posix_time_object._time_zone_offset, None) |
| 606 |
|
| 607 |
def testCopyToDateTimeString(self): |
| 608 |
"""Tests the CopyToDateTimeString function.""" |
| 609 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 610 |
timestamp=1281643591987654321 |
| 611 |
) |
| 612 |
date_time_string = posix_time_object.CopyToDateTimeString() |
| 613 |
self.assertEqual(date_time_string, "2010-08-12 20:06:31.987654321") |
| 614 |
|
| 615 |
posix_time_object = posix_time.PosixTimeInNanoseconds() |
| 616 |
|
| 617 |
date_time_string = posix_time_object.CopyToDateTimeString() |
| 618 |
self.assertIsNone(date_time_string) |
| 619 |
|
| 620 |
def testCopyToDateTimeStringISO8601(self): |
| 621 |
"""Tests the CopyToDateTimeStringISO8601 function.""" |
| 622 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 623 |
timestamp=1281643591987654321 |
| 624 |
) |
| 625 |
date_time_string = posix_time_object.CopyToDateTimeStringISO8601() |
| 626 |
self.assertEqual(date_time_string, "2010-08-12T20:06:31.987654321+00:00") |
| 627 |
|
| 628 |
def testCopyToPosixTimestampWithFractionOfSecond(self): |
| 629 |
"""Tests the CopyToPosixTimestampWithFractionOfSecond function.""" |
| 630 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 631 |
timestamp=1281643591987654321 |
| 632 |
) |
| 633 |
posix_timestamp, fraction_of_second = ( |
| 634 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 635 |
) |
| 636 |
self.assertEqual(posix_timestamp, 1281643591) |
| 637 |
self.assertEqual(fraction_of_second, 987654321) |
| 638 |
|
| 639 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 640 |
timestamp=-11644468446327447321 |
| 641 |
) |
| 642 |
posix_timestamp, fraction_of_second = ( |
| 643 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 644 |
) |
| 645 |
self.assertEqual(posix_timestamp, -11644468446) |
| 646 |
self.assertEqual(fraction_of_second, 327447321) |
| 647 |
|
| 648 |
posix_time_object = posix_time.PosixTimeInNanoseconds() |
| 649 |
|
| 650 |
posix_timestamp, fraction_of_second = ( |
| 651 |
posix_time_object.CopyToPosixTimestampWithFractionOfSecond() |
| 652 |
) |
| 653 |
self.assertIsNone(posix_timestamp) |
| 654 |
self.assertIsNone(fraction_of_second) |
| 655 |
|
| 656 |
def testCopyToSerializableDict(self): |
| 657 |
"""Test the CopyToSerializableDict function.""" |
| 658 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 659 |
timestamp=1281643591987654321 |
| 660 |
) |
| 661 |
expected_serializable_dict = { |
| 662 |
"__class_name__": "PosixTimeInNanoseconds", |
| 663 |
"__type__": "DateTimeValues", |
| 664 |
"timestamp": 1281643591987654321, |
| 665 |
} |
| 666 |
serializable_dict = posix_time_object.CopyToSerializableDict() |
| 667 |
self.assertEqual(serializable_dict, expected_serializable_dict) |
| 668 |
|
| 669 |
def testGetDate(self): |
| 670 |
"""Tests the GetDate function.""" |
| 671 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 672 |
timestamp=1281643591987654321 |
| 673 |
) |
| 674 |
date_tuple = posix_time_object.GetDate() |
| 675 |
self.assertEqual(date_tuple, (2010, 8, 12)) |
| 676 |
|
| 677 |
posix_time_object = posix_time.PosixTimeInNanoseconds() |
| 678 |
|
| 679 |
date_tuple = posix_time_object.GetDate() |
| 680 |
self.assertEqual(date_tuple, (None, None, None)) |
| 681 |
|
| 682 |
def testGetDateWithTimeOfDay(self): |
| 683 |
"""Tests the GetDateWithTimeOfDay function.""" |
| 684 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 685 |
timestamp=1281643591987654321 |
| 686 |
) |
| 687 |
date_with_time_of_day_tuple = posix_time_object.GetDateWithTimeOfDay() |
| 688 |
self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) |
| 689 |
|
| 690 |
posix_time_object = posix_time.PosixTimeInNanoseconds() |
| 691 |
|
| 692 |
date_with_time_of_day_tuple = posix_time_object.GetDateWithTimeOfDay() |
| 693 |
self.assertEqual( |
| 694 |
date_with_time_of_day_tuple, (None, None, None, None, None, None) |
| 695 |
) |
| 696 |
|
| 697 |
def testGetTimeOfDay(self): |
| 698 |
"""Tests the GetTimeOfDay function.""" |
| 699 |
posix_time_object = posix_time.PosixTimeInNanoseconds( |
| 700 |
timestamp=1281643591987654321 |
| 701 |
) |
| 702 |
time_of_day_tuple = posix_time_object.GetTimeOfDay() |
| 703 |
self.assertEqual(time_of_day_tuple, (20, 6, 31)) |
| 704 |
|
| 705 |
posix_time_object = posix_time.PosixTimeInNanoseconds() |
| 706 |
|
| 707 |
time_of_day_tuple = posix_time_object.GetTimeOfDay() |
| 708 |
self.assertEqual(time_of_day_tuple, (None, None, None)) |
| 709 |
|
| 710 |
|
| 711 |
if __name__ == "__main__": |
| 712 |
unittest.main() |