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

软件和网站开发以及相关技术探讨
回复
头像
xfh1987
帖子: 12
注册时间: 2008-04-22 17:41

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

#1

帖子 xfh1987 » 2009-04-20 22:40

我想用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 里有现成的方法可用吗?
谢谢赐教!
头像
libralibra
帖子: 401
注册时间: 2008-02-23 17:31
联系:

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

#2

帖子 libralibra » 2009-04-21 3:11

用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'
My Blog: matlab, ubuntu, python
http://goo.gl/GDIO
头像
libralibra
帖子: 401
注册时间: 2008-02-23 17:31
联系:

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

#3

帖子 libralibra » 2009-04-21 3:12

再给一个格式化字符串查找

代码: 全选

%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
My Blog: matlab, ubuntu, python
http://goo.gl/GDIO
头像
roylez
帖子: 1928
注册时间: 2005-10-04 10:59
来自: 上海

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

#4

帖子 roylez » 2009-04-21 8:28

用time.strptime分析,然后用time.strftime打印

时间日期的读入,ruby的Time.parse还是强太多。
弄个dropbox空间来备份文件或者做私人代码服务器
配置:[url]git://github.com/roylez/dotfiles.git[/url]
主页:http://roylez.heroku.com
各种稀奇玩意儿:http://dooloo.info
头像
xfh1987
帖子: 12
注册时间: 2008-04-22 17:41

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

#5

帖子 xfh1987 » 2009-04-21 10:34

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

代码: 全选

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))

znzhou
帖子: 1
注册时间: 2009-07-15 19:46

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

#6

帖子 znzhou » 2009-07-15 20:05

这个问题是可以解决的,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" )
回复