LEFT | RIGHT |
(no file at all) | |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """Time manipulation functions and variables. | 2 """Time manipulation functions and variables. |
3 | 3 |
4 This module contain common methods that can be used to convert timestamps | 4 This module contain common methods that can be used to convert timestamps |
5 from various formats into number of micro seconds since January 1, 1970, | 5 from various formats into number of micro seconds since January 1, 1970, |
6 00:00:00 UTC that is used internally to store timestamps. | 6 00:00:00 UTC that is used internally to store timestamps. |
7 | 7 |
8 It also contains various functions to represent timestamps in a more | 8 It also contains various functions to represent timestamps in a more |
9 human readable form. | 9 human readable form. |
10 """ | 10 """ |
11 | 11 |
12 from __future__ import unicode_literals | 12 from __future__ import unicode_literals |
13 | 13 |
14 import calendar | 14 import calendar |
15 import datetime | 15 import datetime |
16 import logging | 16 import logging |
17 import time | 17 import time |
18 | 18 |
19 import dateutil.parser | 19 import dateutil.parser |
20 import pytz | 20 import pytz |
21 | 21 |
22 from plaso.lib import errors | 22 from plaso.lib import errors |
23 from plaso.lib import py2to3 | |
24 | 23 |
25 | 24 |
26 MONTH_DICT = { | 25 MONTH_DICT = { |
27 'jan': 1, | 26 'jan': 1, |
28 'feb': 2, | 27 'feb': 2, |
29 'mar': 3, | 28 'mar': 3, |
30 'apr': 4, | 29 'apr': 4, |
31 'may': 5, | 30 'may': 5, |
32 'jun': 6, | 31 'jun': 6, |
33 'jul': 7, | 32 'jul': 7, |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 The timestamp which is an integer containing the number of micro seconds | 326 The timestamp which is an integer containing the number of micro seconds |
328 since January 1, 1970, 00:00:00 UTC or 0 on error. | 327 since January 1, 1970, 00:00:00 UTC or 0 on error. |
329 """ | 328 """ |
330 if not isinstance(datetime_object, datetime.datetime): | 329 if not isinstance(datetime_object, datetime.datetime): |
331 return 0 | 330 return 0 |
332 | 331 |
333 posix_time = int(calendar.timegm(datetime_object.utctimetuple())) | 332 posix_time = int(calendar.timegm(datetime_object.utctimetuple())) |
334 return cls.FromPosixTime(posix_time) + datetime_object.microsecond | 333 return cls.FromPosixTime(posix_time) + datetime_object.microsecond |
335 | 334 |
336 @classmethod | 335 @classmethod |
337 def FromTimeParts( | |
338 cls, year, month, day, hour, minutes, seconds, microseconds=0, | |
339 timezone=pytz.UTC): | |
340 """Converts a list of time entries to a timestamp. | |
341 | |
342 Args: | |
343 year: An integer representing the year. | |
344 month: An integer between 1 and 12. | |
345 day: An integer representing the number of day in the month. | |
346 hour: An integer representing the hour, 0 <= hour < 24. | |
347 minutes: An integer, 0 <= minute < 60. | |
348 seconds: An integer, 0 <= second < 60. | |
349 microseconds: Optional number of microseconds ranging from: | |
350 0 <= microsecond < 1000000. | |
351 timezone: Optional timezone (instance of pytz.timezone). | |
352 | |
353 Returns: | |
354 The timestamp which is an integer containing the number of micro seconds | |
355 since January 1, 1970, 00:00:00 UTC or 0 on error. | |
356 | |
357 Raises: | |
358 TimestampError: if the timestamp cannot be created from the time parts. | |
359 """ | |
360 try: | |
361 date = datetime.datetime( | |
362 year, month, day, hour, minutes, seconds, microseconds) | |
363 except ValueError as exception: | |
364 raise errors.TimestampError(( | |
365 'Unable to create timestamp from {0:04d}-{1:02d}-{2:02d} ' | |
366 '{3:02d}:{4:02d}:{5:02d}.{6:06d} with error: {7!s}').format( | |
367 year, month, day, hour, minutes, seconds, microseconds, | |
368 exception)) | |
369 | |
370 if isinstance(timezone, py2to3.STRING_TYPES): | |
371 timezone = pytz.timezone(timezone) | |
372 | |
373 date_use = timezone.localize(date) | |
374 posix_time = int(calendar.timegm(date_use.utctimetuple())) | |
375 | |
376 return cls.FromPosixTime(posix_time) + microseconds | |
377 | |
378 @classmethod | |
379 def FromTimeString( | 336 def FromTimeString( |
380 cls, time_string, dayfirst=False, gmt_as_timezone=True, | 337 cls, time_string, dayfirst=False, gmt_as_timezone=True, |
381 timezone=pytz.UTC): | 338 timezone=pytz.UTC): |
382 """Converts a string containing a date and time value into a timestamp. | 339 """Converts a string containing a date and time value into a timestamp. |
383 | 340 |
384 Args: | 341 Args: |
385 time_string: String that contains a date and time value. | 342 time_string: String that contains a date and time value. |
386 dayfirst: An optional boolean argument. If set to true then the | 343 dayfirst: An optional boolean argument. If set to true then the |
387 parser will change the precedence in which it parses timestamps | 344 parser will change the precedence in which it parses timestamps |
388 from MM-DD-YYYY to DD-MM-YYYY (and YYYY-MM-DD will be | 345 from MM-DD-YYYY to DD-MM-YYYY (and YYYY-MM-DD will be |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 timezone: Optional timezone of the POSIX timestamp. | 445 timezone: Optional timezone of the POSIX timestamp. |
489 | 446 |
490 Returns: | 447 Returns: |
491 The year of the POSIX timestamp. | 448 The year of the POSIX timestamp. |
492 | 449 |
493 Raises: | 450 Raises: |
494 ValueError: If the posix timestamp is out of the range of supported values. | 451 ValueError: If the posix timestamp is out of the range of supported values. |
495 """ | 452 """ |
496 datetime_object = datetime.datetime.fromtimestamp(posix_time, tz=timezone) | 453 datetime_object = datetime.datetime.fromtimestamp(posix_time, tz=timezone) |
497 return datetime_object.year | 454 return datetime_object.year |
LEFT | RIGHT |