Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing date and timestamps in Python with time.strptime format

My cloud server logs time in this format:

[17/Dec/2011:09:48:49 -0600]

To read it into Python variables, I can say:

>>>str = '17/Dec/2011:09:48:49 -0600'
>>>import time
>>>print  time.strptime(str,"%d/%b/%Y:%H:%M:%S -0600")

Result:

time.struct_time(tm_year=2011, tm-mon=12, tm=mday=17, tm_hour=9, tm_min=48, tm_sec=49, tm_wday=5, tm_yday=351, tm_isdst=-1)

or I can try

>>>mytime = time.strptime(str,"%d/%b/%Y:%H:%M:%S -0600")
>>>print mytime.tm_hour

Result:

9

What does the -0600 do? I expected it to adjust the hour value in the date time object? Is there a wildcard to use instead of hard-coding the -0600?

like image 273
TGanoe Avatar asked Apr 24 '12 12:04

TGanoe


1 Answers

The -0600 is the offset from Greenwich Mean Time (GMT). As another SO question already says, time.strptime cannot read timezone offsets, though datetime.strftime can generate them.

As explained at the start of the datetime module's documentation, there are two ways of approaching "time" in python, naive or aware. When all you care about is time inside your system, dealing with naive time/datetime objects is fine (in which case you can strip out the offset as alan suggested). When you need to compare the values inside your system with the real world's notion of time, you have to start dealing with that offset.

The easy way to deal with this is just to use python-dateutil. It has a parse function that will do its best to fuzzily match the date string you pass in to multiple formats and return a workable datetime instance that represents its best guess as to what you meant.

>>> from dateutil.parser import parse
>>> parse('17/Dec/2011:09:48:49 -0600', fuzzy=True)
datetime.datetime(2011, 12, 17, 9, 48, 49, tzinfo=tzoffset(None, -21600))

Normally, having software give its "best guess" is a Bad Thing. In this case, it seems justified if your input formats are stable. Dealing with time in software development is hard, just go shopping.

like image 170
Don Spaulding Avatar answered Oct 26 '22 06:10

Don Spaulding