[Tutor] Processing unix style timestamp

Ravi Kondamuru ravikondamuru at gmail.com
Fri Mar 7 04:14:45 CET 2008


Thanks for the replies.

time.strptime() is able to parse the string if the timezone is local or GMT
or UTC. Is there a file of timezones and corresponding time offsets that I
can use to build the dictionary?

thanks
Ravi.

>>> import time
>>> time.strptime("Mon Feb 11 01:34:52 CST 2008", "%a %b %d %H:%M:%S %Z %Y")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/_strptime.py", line 293, in strptime
    raise ValueError("time data did not match format:  data=%s  fmt=%s" %
ValueError: time data did not match format:  data=Mon Feb 11 01:34:52 CST
2008  fmt=%a %b %d %H:%M:%S %Z %Y
>>> time.tzname
('EST', 'EDT')
>>> time.strptime("Mon Feb 11 01:34:52 EST 2008", "%a %b %d %H:%M:%S %Z %Y")
(2008, 2, 11, 1, 34, 52, 0, 42, 0)
>>> time.strptime("Mon Feb 11 01:34:52 UTC 2008", "%a %b %d %H:%M:%S %Z %Y")
(2008, 2, 11, 1, 34, 52, 0, 42, 0)


On Thu, Mar 6, 2008 at 4:20 PM, John Fouhy <john at fouhy.net> wrote:

> On 07/03/2008, Ravi Kondamuru <ravikondamuru at gmail.com> wrote:
> > 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.
>
> Maybe you could split out the information.. e.g.:
>
> >>> s = 'Mon Feb 11 01:34:52 CST 2008'
> >>> s[4:19] + s[23:], s[20:23]
> ('Feb 11 01:34:52 2008', 'CST')
>
> You should be able to parse the former with strptime().  You could
> then build a dictionary mapping timezones to offsets which you could
> add to the parsed time to produce a time in GMT.
>
> --
> John.
>



-- 
రవిచంద్ర కొండమూరు
(Ravi Kondamuru)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080306/c3e2b3f7/attachment.htm 


More information about the Tutor mailing list