1 """Utilities for parsing the request time field (``%t``).
3 The ``parse_date`` function is intended as a fast way to convert a log
4 date into something useful, without incurring a significant date
5 parsing overhead---good enough for basic stuff but will be a problem
6 if you need to deal with log from multiple servers in different
9 An alternative funtion, ``parse_time``, parses the data into
10 ``datetime.datetime`` instances, which may be slower, but it does take
11 the offset into account. It also makes it easy to calculate time
15 import datetime as _datetime
35 """Convert a date to a (`timestamp`, `offset`) tuple.
37 Takes a date in the format: [05/Dec/2006:10:51:44 +0000]
38 (including square brackets) and returns a two element
39 tuple containing first a timestamp of the form
40 YYYYMMDDHH24IISS e.g. 20061205105144 and second the
41 timezone offset as is e.g.;
43 >>> parse_date('[05/Dec/2006:10:51:44 +0000]')
44 ('20061205105144', '+0000')
46 It does not attempt to adjust the timestamp according
47 to the timezone---if you need this, use ``parse_time``.
49 date = date.strip('[]')
58 return (''.join(elems),date[21:])
61 class FixedOffset(_datetime.tzinfo):
62 """Fixed offset in minutes east from UTC.
64 >>> f = FixedOffset(name='-0500', hours=-5)
65 >>> f.utcoffset(dt=None)
66 datetime.timedelta(-1, 68400)
74 _ZERO = _datetime.timedelta(0)
76 def __init__(self, name, **kwargs):
77 self._offset = _datetime.timedelta(**kwargs)
80 def utcoffset(self, dt):
92 >>> dt = parse_time("[12/Feb/2012:09:55:33 -0500]")
94 '2012-02-12T09:55:33-05:00'
95 >>> time.mktime(dt.utctimetuple())
98 date = date.strip('[]')
99 tzdate = date[21:].strip()
100 soff = int(date[21:22] + '1')
101 hoff = int(date[22:24])
102 moff = int(date[24:])
103 tz = FixedOffset(tzdate, hours=soff*hoff, minutes=soff*moff)
104 return _datetime.datetime(
105 year=int(date[7:11]),
106 month=int(MONTHS[date[3:6]]),
108 hour=int(date[12:14]),
109 minute=int(date[15:17]),
110 second=int(date[18:20]),