官术网_书友最值得收藏!

datetime objects

Investigators are often asked to determine when a file was deleted, when a text message was read, or the correct order for a sequence of events. Consequently, a great deal of analysis revolves around timestamps and other temporal artifacts. Understanding time can help us piece together the puzzle and further understand the context surrounding an artifact. For this, and many other reasons, let's practice handling timestamps using the datetime module.

Python's datetime module supports the interpretation and formatting of timestamps. This module has many features, most notably getting the current time, determining the change (or delta) between two timestamps, and converting common timestamp formats into a human readable date. The datetime.datetime() method creates a datetime object and accepts the year, month, day, and optionally hour, minute, second, millisecond, and time zone arguments. The timedelta() method shows the difference between two datetime objects by storing the difference in days, seconds, and microseconds.

First, we need to import the datetime library so that we can use functions from the module. We can see the current date with the datetime.now() method. This creates a datetime object, which we then manipulate. For instance, let's create a timedelta object by subtracting two datetime objects, separated by a few seconds. We can add or subtract the timedelta object to or from our right_now variable to generate another datetime object:

>>> import datetime
>>> right_now = datetime.datetime.now()
>>> right_now
datetime.datetime(2018, 6, 30, 7, 48, 31, 576151)

>>> # Subtract time
>>> delta = datetime.datetime.now() - right_now
>>> delta
datetime.timedelta(0, 16, 303831)

>>> # Add datetime to time delta to produce second time
>>> right_now + delta
datetime.datetime(2018, 6, 30, 7, 48, 47, 879982)

The output may vary, as you are running these commands at a different time than when they were for this book.

Another highly used application of the datetime module is strftime(), which allows datetime objects to be converted into custom-formatted strings. This function takes a string format as its input. This format string is made up of special characters beginning with the percentage sign. The following table illustrates examples of the formatters we can use with the strftime() function:

You can find additional timestamp formatting information at  http://strftime.org/ or via the official documentation:  https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior.

In addition, the strptime() function, which we do not showcase here, can be used for the reverse process. The strptime() function will take a string containing a date and time and convert it into a datetime object using the formatting string. We can also interpret epoch time (also called Unix or POSIX time), represented as an integer, into a UTC datetime object:

>>> epoch_timestamp = 874281600
>>> datetime_timestamp = datetime.datetime.utcfromtimestamp(epoch_timestamp)

We can print this new object and it will be automatically converted into a string representing the datetime object. However, let's pretend that we do not like to separate our date by hyphens. Instead, we can use the strftime() method to display the date with forward slashes or using any of the defined formatters. Lastly, the datetime library has a few pre-built formatters such as isoformat(), which we can use to easily produce a standard timestamp format:

>>> from __future__ import print_function
>>> print(datetime_timestamp)
1997-09-15 00:00:00
>>> print(datetime_timestamp.strftime('%m/%d/%Y %H:%M:%S'))
09/15/1997 00:00:00
>>> print(datetime_timestamp.strftime('%A %B %d, %Y at %I:%M:%S %p'))
Monday September 15, 1997 at 12:00:00 AM
>>> print(datetime_timestamp.isoformat())
1997-09-15T00:00:00
As a note, we have imported  print_function into our interpreter to allow us to print these date values in both Python 2 and Python 3.

The datetime library alleviates a great deal of stress involved in handling date and time values in Python. This module is also well-suited for processing time formats that are often encountered during investigations.

主站蜘蛛池模板: 台前县| 百色市| 罗平县| 子洲县| 石景山区| 镇平县| 平塘县| 巴彦淖尔市| 海原县| 江阴市| 广元市| 兰州市| 车险| 呼和浩特市| 江山市| 施秉县| 武乡县| 宜宾市| 昌吉市| 涟水县| 任丘市| 五常市| 五河县| 弋阳县| 哈尔滨市| 巍山| 邵阳县| 柘荣县| 商南县| 新绛县| 曲靖市| 山西省| 河南省| 吴旗县| 那坡县| 哈巴河县| 北海市| 西乌珠穆沁旗| 都兰县| 鸡西市| 唐河县|