[Tutor] Processing unix style timestamp

Martin Walsh mwalsh at groktech.org
Fri Mar 7 10:09:14 CET 2008


Ravi Kondamuru wrote:
> Hi,
> 
> I have a log file that prints the date and time in the following format:
> Mon Feb 11 01:34:52 CST 2008
> I am expecting multiple timezone entries (eg: PST, PDT and GMT) on the
> system running in America/Los Angeles time zone.
> I am looking for a way to internally store all the different timezone
> entries in GMT.
> I looked at datetime, but it seems slightly complex to work with non GMT
> timestamps.
>
> Any pointers?
>

If you are not offended by a 3rd-party module, then the string parser
included in the egenix mxDateTime module[1] is hard to beat. You may
even have it installed already, as it appears to be a popular dependency
of other 3rd-party modules, especially db adapters.

In [1]: import mx.DateTime

In [2]: d = mx.DateTime.DateFrom('Mon Feb 11 01:34:52 CST 2008')

In [3]: d
Out[3]: <mx.DateTime.DateTime object for '2008-02-11 07:34:52.00' at
b788d138>

In [4]: d.strftime('%a %b %d %H:%M:%S %Y')
Out[4]: 'Mon Feb 11 07:34:52 2008'

In [5]: d = mx.DateTime.DateFrom('Mon Feb 11 01:34:52 EST 2008')

In [6]: d.strftime('%a %b %d %H:%M:%S %Y')
Out[6]: 'Mon Feb 11 06:34:52 2008'

In [7]: d = mx.DateTime.DateFrom('Mon Feb 11 01:34:52 UTC 2008')

In [8]: d.strftime('%a %b %d %H:%M:%S %Y')
Out[8]: 'Mon Feb 11 01:34:52 2008'

HTH,
Marty

[1] http://www.egenix.com/products/python/mxBase/mxDateTime/

> thanks,
> Ravi.



More information about the Tutor mailing list