Skip to content

Commit

Permalink
Code review: 347740043: Removed FromPythonDatetime and FromPosixTime
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed May 9, 2018
1 parent e54ecfe commit 9275cc1
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 90 deletions.
2 changes: 1 addition & 1 deletion config/dpkg/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ plaso (20180509-1) unstable; urgency=low

* Auto-generated

-- Log2Timeline <log2timeline-dev@googlegroups.com> Wed, 09 May 2018 06:51:35 +0200
-- Log2Timeline <log2timeline-dev@googlegroups.com> Wed, 09 May 2018 07:02:31 +0200
6 changes: 3 additions & 3 deletions plaso/analysis/sessionize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from plaso.analysis import interface
from plaso.analysis import manager
from plaso.containers import reports
from plaso.lib import timelib
from plaso.lib import definitions


class SessionizeAnalysisPlugin(interface.AnalysisPlugin):
Expand All @@ -18,7 +18,7 @@ class SessionizeAnalysisPlugin(interface.AnalysisPlugin):

_EVENT_TAG_COMMENT = 'Tag applied by sessionize analysis plugin.'

_DEFAULT_MAXIMUM_PAUSE = 10 * timelib.Timestamp.MICROSECONDS_PER_MINUTE
_DEFAULT_MAXIMUM_PAUSE = 10 * definitions.MICROSECONDS_PER_MINUTE

def __init__(self):
"""Initializes a sessionize analysis plugin."""
Expand All @@ -37,7 +37,7 @@ def SetMaximumPause(self, maximum_pause_minutes):
the same session, in minutes.
"""
self._maximum_pause_microseconds = (
maximum_pause_minutes * timelib.Timestamp.MICROSECONDS_PER_MINUTE)
maximum_pause_minutes * definitions.MICROSECONDS_PER_MINUTE)

def CompileReport(self, mediator):
"""Compiles an analysis report.
Expand Down
1 change: 1 addition & 0 deletions plaso/lib/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


MICROSECONDS_PER_SECOND = 1000000
MICROSECONDS_PER_MINUTE = 60000000

COMPRESSION_FORMAT_NONE = 'none'
COMPRESSION_FORMAT_ZLIB = 'zlib'
Expand Down
8 changes: 5 additions & 3 deletions plaso/lib/pfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

from __future__ import unicode_literals

import calendar
import datetime
import logging
import re

from plaso.formatters import manager as formatters_manager
from plaso.formatters import mediator as formatters_mediator

# TODO: Changes this so it becomes an attribute instead of having backend
# load a front-end library.
from plaso.lib import definitions
from plaso.lib import errors
from plaso.lib import objectfilter
from plaso.lib import py2to3
Expand Down Expand Up @@ -293,7 +293,9 @@ def __init__(self, data):
self.text))

elif isinstance(data, datetime.datetime):
self.data = timelib.Timestamp.FromPythonDatetime(data)
posix_time = int(calendar.timegm(data.utctimetuple()))
self.data = (
posix_time * definitions.MICROSECONDS_PER_SECOND) + data.microsecond
self.text = '{0!s}'.format(data)

elif isinstance(data, DateCompareObject):
Expand Down
72 changes: 9 additions & 63 deletions plaso/lib/timelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import dateutil.parser
import pytz

from plaso.lib import definitions
from plaso.lib import errors


Expand All @@ -45,32 +46,11 @@ class Timestamp(object):
The timestamp is not necessarily in UTC.
"""
# The minimum timestamp in seconds.
TIMESTAMP_MIN_SECONDS = -(((1 << 63) - 1) / 1000000)

# The maximum timestamp in seconds.
TIMESTAMP_MAX_SECONDS = ((1 << 63) - 1) / 1000000

# The minimum timestamp in micro seconds.
TIMESTAMP_MIN_MICRO_SECONDS = -((1 << 63) - 1)

# The maximum timestamp in micro seconds.
TIMESTAMP_MAX_MICRO_SECONDS = (1 << 63) - 1

# Timestamp that represents the timestamp representing not
# a date and time value.
# TODO: replace this with a real None implementation.
NONE_TIMESTAMP = 0

# The number of micro seconds per second.
MICRO_SECONDS_PER_SECOND = 1000000

# The number of microseconds per minute.
MICROSECONDS_PER_MINUTE = (60 * MICRO_SECONDS_PER_SECOND)

# The multiplication factor to change milliseconds to micro seconds.
MILLI_SECONDS_TO_MICRO_SECONDS = 1000

@classmethod
def CopyFromString(cls, time_string):
"""Copies a timestamp from a string containing a date and time value.
Expand Down Expand Up @@ -294,43 +274,7 @@ def CopyToPosix(cls, timestamp):
The timestamp which is an integer containing the number of seconds
since January 1, 1970, 00:00:00 UTC.
"""
return timestamp // cls.MICRO_SECONDS_PER_SECOND

@classmethod
def FromPosixTime(cls, posix_time):
"""Converts a POSIX timestamp into a timestamp.
The POSIX time is a signed 32-bit or 64-bit value containing:
seconds since 1970-01-01 00:00:00
Args:
posix_time: The POSIX timestamp.
Returns:
The timestamp which is an integer containing the number of micro seconds
since January 1, 1970, 00:00:00 UTC or 0 on error.
"""
if (posix_time < cls.TIMESTAMP_MIN_SECONDS or
posix_time > cls.TIMESTAMP_MAX_SECONDS):
return 0
return int(posix_time) * cls.MICRO_SECONDS_PER_SECOND

@classmethod
def FromPythonDatetime(cls, datetime_object):
"""Converts a Python datetime object into a timestamp.
Args:
datetime_object: The datetime object (instance of datetime.datetime).
Returns:
The timestamp which is an integer containing the number of micro seconds
since January 1, 1970, 00:00:00 UTC or 0 on error.
"""
if not isinstance(datetime_object, datetime.datetime):
return 0

posix_time = int(calendar.timegm(datetime_object.utctimetuple()))
return cls.FromPosixTime(posix_time) + datetime_object.microsecond
return timestamp // definitions.MICROSECONDS_PER_SECOND

@classmethod
def FromTimeString(
Expand Down Expand Up @@ -377,7 +321,9 @@ def FromTimeString(
else:
datetime_object = timezone.localize(datetime_object)

return cls.FromPythonDatetime(datetime_object)
posix_time = int(calendar.timegm(datetime_object.utctimetuple()))
timestamp = posix_time * definitions.MICROSECONDS_PER_SECOND
return timestamp + datetime_object.microsecond

@classmethod
def GetNow(cls):
Expand Down Expand Up @@ -414,18 +360,18 @@ def LocaltimeToUTC(cls, timestamp, timezone, is_dst=False):
# for UTC and will raise.
datetime_delta = timezone.utcoffset(datetime_object, is_dst=is_dst)
seconds_delta = int(datetime_delta.total_seconds())
timestamp -= seconds_delta * cls.MICRO_SECONDS_PER_SECOND
timestamp -= seconds_delta * definitions.MICROSECONDS_PER_SECOND

return timestamp

@classmethod
def RoundToSeconds(cls, timestamp):
"""Takes a timestamp value and rounds it to a second precision."""
leftovers = timestamp % cls.MICRO_SECONDS_PER_SECOND
leftovers = timestamp % definitions.MICROSECONDS_PER_SECOND
scrubbed = timestamp - leftovers
rounded = round(float(leftovers) / cls.MICRO_SECONDS_PER_SECOND)
rounded = round(float(leftovers) / definitions.MICROSECONDS_PER_SECOND)

return int(scrubbed + rounded * cls.MICRO_SECONDS_PER_SECOND)
return int(scrubbed + rounded * definitions.MICROSECONDS_PER_SECOND)


def GetCurrentYear():
Expand Down
3 changes: 1 addition & 2 deletions plaso/parsers/bsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from plaso.containers import time_events
from plaso.lib import definitions
from plaso.lib import errors
from plaso.lib import timelib
from plaso.parsers import interface
from plaso.parsers import logger
from plaso.parsers import manager
Expand Down Expand Up @@ -1054,7 +1053,7 @@ def FormatToken(self, token_id, token, file_object):
# TODO: if this timestamp is useful, it must be extracted as a separate
# event object.
timestamp = token.microseconds + (
token.timestamp * timelib.Timestamp.MICRO_SECONDS_PER_SECOND)
token.timestamp * definitions.MICROSECONDS_PER_SECOND)
date_time = dfdatetime_posix_time.PosixTimeInMicroseconds(
timestamp=timestamp)
date_time_string = date_time.CopyToDateTimeString()
Expand Down
18 changes: 0 additions & 18 deletions tests/lib/timelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,6 @@ def testCopyFromString(self):
with self.assertRaises(ValueError):
_ = timelib.Timestamp.CopyFromString('2012-06-27 18:17:01Z')

def testTimestampFromPosixTime(self):
"""Test the POSIX time conversion."""
timestamp = timelib.Timestamp.FromPosixTime(1281647191)
expected_timestamp = timelib.Timestamp.CopyFromString(
'2010-08-12 21:06:31')
self.assertEqual(timestamp, expected_timestamp)

timestamp = timelib.Timestamp.FromPosixTime(-122557518)
expected_timestamp = timelib.Timestamp.FromTimeString(
'1966-02-12 1966 12:14:42 UTC')
self.assertEqual(timestamp, expected_timestamp)

# POSIX time that exceeds upper bound.
self.assertEqual(timelib.Timestamp.FromPosixTime(9223372036855), 0)

# POSIX time that exceeds lower bound.
self.assertEqual(timelib.Timestamp.FromPosixTime(-9223372036855), 0)

def testMonthDict(self):
"""Test the month dict, both inside and outside of scope."""
self.assertEqual(timelib.MONTH_DICT['nov'], 11)
Expand Down

0 comments on commit 9275cc1

Please sign in to comment.