Parsing a date-time string?

Leeuw van der, Tim tim.leeuwvander at nl.unisys.com
Fri Dec 23 02:48:52 EST 2005


Hi Tim,

 

Thanks a lot! That sounds like quite a useful routine, that I can use
without going through extra documentation and adding extra libraries!

 

Cheers,

 

--Tim :-)

 

________________________________

From: Tim Williams (gmail) [mailto:tdwdotnet at gmail.com] 
Sent: donderdag 22 december 2005 16:38
To: Leeuw van der, Tim
Cc: python-list at python.org
Subject: Re: Parsing a date-time string?

 

 

On 21 Dec 2005 01:43:13 -0800, Tim N. van der Leeuw
<tim.leeuwvander at nl.unisys.com> wrote: 

Hi,

I want to parse strings containing date-time, which look like the
following:

"Mon Dec 19 11:06:12:333 CET 2005"
[snipped]
What I want to get is some sort of sortable date; either as a number or
(if nothing else) as a string in ISO8601 format.


Its slow in the office today, so: ......

######

from email import Utils
import time

zones = {'CET': '+0100', 'GMT': '0000', 'EST': '-0500', 'PST': '-0800'}

def get_sortable_time(a_date):
    split_date = a_date.split()

    split_time = split_date[3].split(':')          # split the time
    microsecs = float('0.'+ split_time[-1])   # get the microseconds
    split_date[3] = ':'.join(split_time[:-1])    # join time, without
microseconds 
    
    split_date[4] = zones[split_date[4]]     # convert the timezone to
'-0800' format
    split_date = ' '.join(split_date)              # eg "Mon Dec 19
11:06:12 +0100 2005"

    gmt_time_as_num = Utils.mktime_tz(Utils.parsedate_tz(split_date) )
# eg 1134993972.0

    return gmt_time_as_num + microsecs     # time in GMT,  eg
1134993972.33 
        
sortable_time = get_sortable_time( "Mon Dec 19 11:06:12:333 CET 2005" )

print sortable_time          # = 1134993972.33  (in time.time() format )

print time.ctime(sortable_time)          # Mon Dec 19 12:06:12 2005


######


You could remove the 2 "microsecs" references if you don't need that
level of granularity

 HTH :)

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051223/1fdd91b3/attachment.html>


More information about the Python-list mailing list