分页: 1 / 1

[求助]关于 python 时间格式的处理

发表于 : 2009-04-20 22:40
xfh1987
我想用python写一个用来收发和阅读邮件的程序,遇到个小问题。
就是如何实现下面的时间格式转换?
中间涉及时区的问题,还有不同的邮件 Date 的格式略有不同,如下:

代码: 全选

21 Mar 2009 13:52:39 -0400              --->  2009-03-22 01:52:39
Mon, 20 Apr 2009 21:08:25 +0800         --->  2009-04-20 21:08:25
Fri, 17 Apr 2009 23:04:17 +0200 (CEST)  --->  2009-04-18 05:04:17
Thu, 26 Mar 2009 22:10:57 +0800 (CST)   --->  2009-03-26 22:10:57
python 里有现成的方法可用吗?
谢谢赐教!

Re: [求助]关于 python 时间格式的处理

发表于 : 2009-04-21 3:11
libralibra
用time模块就可以了,其实time的localtime可以得到时间日期的所有信息,看下例,年月日,时分秒,周,星期几....然后有2种办法:

1,你自己拼接字符串也可以,
2,利用格式化字符串

个人建议第二种.利用strftime(format)方法即可.实例如下:

代码: 全选

>>> import time
>>> t1 = time.localtime()
>>> t1
time.struct_time(tm_year=2009, tm_mon=4, tm_mday=20, tm_hour=20, tm_min=8, tm_sec=47, tm_wday=0, tm_yday=110, tm_isdst=1)
>>> strFormat = "%Y-%m-%d %H:%M:%S"
>>> t2 = time.strftime(strFormat)
>>> t2
'2009-04-20 20:09:29'

Re: [求助]关于 python 时间格式的处理

发表于 : 2009-04-21 3:12
libralibra
再给一个格式化字符串查找

代码: 全选

%a  	Abbreviated weekday name
%A 	Full weekday name
%b 	Abbreviated month name
%B 	Full month name
%c 	Date and time representation appropriate for locale
%d 	Day of month as decimal number (01 - 31)
%H 	Hour in 24-hour format (00 - 23)
%I 	Hour in 12-hour format (01 - 12)
%j 	Day of year as decimal number (001 - 366)
%m 	Month as decimal number (01 - 12)
%M 	Minute as decimal number (00 - 59)
%p 	Current locale's A.M./P.M. indicator for 12-hour clock
%S 	Second as decimal number (00 - 59)
%U 	Week of year as decimal number, with Sunday as first day of week (00 - 51)
%w 	Weekday as decimal number (0 - 6; Sunday is 0)
%W 	Week of year as decimal number, with Monday as first day of week (00 - 51)
%x 	Date representation for current locale
%X 	Time representation for current locale
%y 	Year without century, as decimal number (00 - 99)
%Y 	Year with century, as decimal number
%z, %Z 	Time-zone name or abbreviation; no characters if time zone is unknown
%% 	Percent sign

Re: [求助]关于 python 时间格式的处理

发表于 : 2009-04-21 8:28
roylez
用time.strptime分析,然后用time.strftime打印

时间日期的读入,ruby的Time.parse还是强太多。

Re: [求助]关于 python 时间格式的处理

发表于 : 2009-04-21 10:34
xfh1987
我最后还是自己解析字符串,把时间转换成秒,然后根据时区加减,最后打印成本地时间。
代码显得很复杂,你们看看能不能简化啊。感觉为这么简单的一个目的写这么多不值得。

代码: 全选

import time

def get_time(date):
    if ', ' in date:
        rest,date = date.split(', ',1)
    if '(' in date:
        date,rest = date.split('(',1)
    if '+' in date:
        t = date[0:date.find('+')-1]
        tz = int(date[date.find('+'):])*36
    elif '-' in date:
        t = date[0:date.find('-')-1]
        tz = int(date[date.find('-'):])*36
    else:
        t = date
    t = time.strptime(t,"%d %b %Y %H:%M:%S")
    t = time.mktime(t) - tz - time.timezone
    t = time.localtime(t)
    return t

dates = ['21 Mar 2009 13:52:39 -0400',
         'Mon, 20 Apr 2009 21:08:25 +0800',
         'Fri, 17 Apr 2009 23:04:17 +0200 (CEST)',
         'Thu, 26 Mar 2009 22:10:57 +0800 (CST) ']

for date in dates:
    print time.strftime('%Y-%m-%d %H:%M:%S',get_time(date))


Re: [求助]关于 python 时间格式的处理

发表于 : 2009-07-15 20:05
znzhou
这个问题是可以解决的,3楼的同学已经很清楚的列出的所有的格式,楼主只需要使用time.strptime()函数做一下格式转换就行了。
举个例子,比如当前时间格式是这样的:“14/Aug/2009:17:58:22”,那么只需要用"%d/%b/%Y:%H:%M:%S"去解析就行。
date = time.strptime(“14/Aug/2009:17:58:22”,"%d/%b/%Y:%H:%M:%S" )