Home > GAE和编程 > Datetime--Python的基本日期时间类型

Datetime--Python的基本日期时间类型

July 5th, 2010 发表评论 阅读评论

最近在学习Google App Engine日期时间属性时,尝试对Micolog这个基于Google App Engine的Blog后台的评论做了一个简单的修改页面,这样就可以使我们省下了去App Engine的后台去搜寻相关数据库再做修改的时间。编辑代码的过程倒是基本简单,新建一个编辑评论的页面(comment.html),再到admin.py中加入相关代码就可以了。不过,这个过程中倒是遇到一个小问题,python提交时间更新时会抛出一个异常来:

BadValueError: Property date must be a datetime

解决办法倒是很简单,将时间格式化成Datetime类型即可。

比如说:我在模板中对时间格式化为{{comment.date|datetz:"Y-m-d H:i:s"}}(如:2010-07-05-20:00:12这样的形式),那么在程序代码中可以这样写做:

comment.date=datetime.strptime(commentdate,"%Y-%m-%d %H:%M:%S")

当然其他类型的格式化都是可取的。由于Python中对时间进行格式化的相关字符与Django模板中time过滤器所用的格式字符串略有区别,下面就分别对其进行简单介绍,希望对你有所帮助。

表1  可用的日期格式字符串
格式字符串

描述

示例输出
a 'a.m.' 或者 'p.m.' 。(这与PHP中的输出略有不同,因为为了匹配美联社风格,它包含了句点。 'a.m.'
A 'AM' 或者 'PM' 'AM'
b 月份,文字式的,三个字母,小写。 'jan'
d 一月的第几天,两位数字,带前导零。 '01''31'
D 一周的第几天,文字式的,三个字母。 'Fri'
f 时间,12小时制的小时和分钟数,如果分钟数为零则不显示。 '1' , '1:30'
F 月份,文字式的,全名。 'January'
g 小时,12小时制,没有前导零。 '1''12'
G 小时,24小时制,没有前导零。 '0''23'
h 小时,12小时制。 '01''12'
H 小时,24小时制。 '00''23'
i 分钟。 '00''59'
j 一月的第几天,不带前导零。 '1''31'
l 一周的第几天,文字式的,全名。 'Friday'
L 是否为闰年的布尔值。 TrueFalse
m 月份,两位数字,带前导零。 '01''12'
M 月份,文字式的,三个字母。 'Jan'
n 月份,没有前导零。 '1''12'
N 美联社风格的月份缩写。 'Jan.' , 'Feb.' , 'March' , 'May'
O 与格林威治标准时间的时间差(以小时计)。 '+0200'
P 时间,12小时制的小时分钟数以及a.m./p.m.,分钟数如果为零则不显示,用字符串表示特殊时间点,如 'midnight''noon' '1 a.m.' , '1:30 p.m.' , 'midnight' , 'noon' , '12:30 p.m.'
r RFC 822 格式的日期。 'Thu, 21 Dec 2000 16:01:07 +0200'
s 秒数,两位数字,带前导零。 '00''59'
S 英语序数后缀,用于表示一个月的第几天,两个字母。 'st' , 'nd' , 'rd''th'
t 指定月份的天数。 2831
T 本机的时区。 'EST' , 'MDT'
w 一周的第几天,数字,带前导零。 '0' (Sunday) 到 '6' (Saturday)
W ISO-8601 一年中的第几周,一周从星期一开始。 1 , 23
y 年份,两位数字。 '99'
Y 年份,四位数字。 '1999'
z 一年的第几天。 0365
Z 以秒计的时区偏移量,这个偏移量对于UTC西部时区总是负数,对于UTC东部时区总是正数。 -4320043200

strftime() and strptime() Behavior

date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string. Broadly speaking, d.strftime(fmt) acts like the time module’s time.strftime(fmt, d.timetuple()) although not all objects support a timetuple() method.

Conversely, the datetime.strptime() class method creates a datetime object from a string representing a date and time and a corresponding format string. datetime.strptime(date_string, format) is equivalent to datetime(*(time.strptime(date_string, format)[0:6])).

For time objects, the format codes for year, month, and day should not be used, as time objects have no such values. If they’re used anyway, 1900 is substituted for the year, and 1 for the month and day.

For date objects, the format codes for hours, minutes, seconds, and microseconds should not be used, as date objects have no such values. If they’re used anyway, 0 is substituted for them.

New in version 2.6: time and datetime objects support a %f format code which expands to the number of microseconds in the object, zero-padded on the left to six places.

For a naive object, the %z and %Z format codes are replaced by empty strings.

For an aware object:

%z
utcoffset() is transformed into a 5-character string of the form +HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and MM is a 2-digit string giving the number of UTC offset minutes. For example, if utcoffset() returns timedelta(hours=-3, minutes=-30), %z is replaced with the string '-0330'.
%Z
If tzname() returns None, %Z is replaced by an empty string. Otherwise %Z is replaced by the returned value, which must be a string.

The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common.

The following is a list of all the format codes that the C standard (1989 version) requires, and these work on all platforms with a standard C implementation. Note that the 1999 version of the C standard added additional format codes.

The exact range of years for which strftime() works also varies across platforms. Regardless of platform, years before 1900 cannot be used.

表2  Python datetime中可用的日期格式字符串
Directive

Meaning

Notes
%a Locale’s abbreviated weekday name.  
%A Locale’s full weekday name.  
%b Locale’s abbreviated month name.  
%B Locale’s full month name.  
%c Locale’s appropriate date and time representation.  
%d Day of the month as a decimal number [01,31].  
%f Microsecond as a decimal number [0,999999], zero-padded on the left (1)
%H Hour (24-hour clock) as a decimal number [00,23].  
%I Hour (12-hour clock) as a decimal number [01,12].  
%j Day of the year as a decimal number [001,366].  
%m Month as a decimal number [01,12].  
%M Minute as a decimal number [00,59].  
%p Locale’s equivalent of either AM or PM. (2)
%S Second as a decimal number [00,61]. (3)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (4)
%w Weekday as a decimal number [0(Sunday),6].  
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (4)
%x Locale’s appropriate date representation.  
%X Locale’s appropriate time representation.  
%y Year without century as a decimal number [00,99].  
%Y Year with century as a decimal number.  
%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). (5)
%Z Time zone name (empty string if the object is naive).  
%% A literal '%' character.  

 

Notes:

  1. When used with the strptime() method, the %f directive accepts from one to six digits and zero pads on the right. %f is an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available).
  2. When used with the strptime() method, the %p directive only affects the output hour field if the %I directive is used to parse the hour.
  3. The range really is 0 to 61; according to the Posix standard this accounts for leap seconds and the (very rare) double leap seconds. The time module may produce and does accept leap seconds since it is based on the Posix standard, but the datetime module does not accept leap seconds in strptime() input nor will it produce them in strftime() output.
  4. When used with the strptime() method, %U and %W are only used in calculations when the day of the week and the year are specified.
  5. For example, if utcoffset() returns timedelta(hours=-3, minutes=-30), %z is replaced with the string '-0330'.
转载请以超链接注明来自  云在天边看世界
本文永久链接  http://www.tangblog.info/2010/07/5/python-datetime-basic-date-and-time-types.html

分类: GAE和编程 标签: ,

  1. avatar
    the5fire 2012-12-19 at 16:54 |#45

    r RFC 822 格式的日期。 这个格式要怎么用?
  2. avatar
    Ted 2011-09-06 at 08:57 |#43

    @admin
    谢谢了,这个在我的机器上是可以。
    但是换一个环境,系统的默认日期时间格式会变,就错了。
    这个字符串是从别的程序中读取多来的。
  3. avatar
    admin 2011-09-05 at 21:27 |#42

    @Ted

    from datetime import datetime
    a="9/5/2011 6:00 PM"
    origdate=datetime.strptime(a,'%m/%d/%Y %I:%M %p')
    print origdate
  4. avatar
    Ted 2011-09-05 at 18:01 |#41

    问仁兄一个问题:
    我在文本文件中读取了一个本地时间的格式化字符串,例如“9/5/2011 6:00 PM”,因为这个脚本需要在任意一台机器上去跑,所以想知道在Python中如何将这段文本转成一个datetime对象。

    谢谢。
  5. avatar
    绣十字绣 2011-07-17 at 00:41 |#40

    很不错,学习了哦
  6. avatar
    云在天边 2010-07-22 at 09:37 |#39

    @Leyond
    呵呵,没事就折腾啊
  7. avatar
    Leyond 2010-07-21 at 22:24 |#38

    好久没过来,变样子了,越折腾越好啊
  8. avatar
    等待思索 2010-07-20 at 23:57 |#37

    我那个是flash做的。。只是一种装饰,有机会以后在改进下。
  9. avatar
    云在天边 2010-07-20 at 23:23 |#36

    @Mucid
    是倒是阿,可我也不太清楚邮件发送情况哦,后台记录忘加递送状态信息了……
  10. avatar
    Mucid 2010-07-20 at 23:17 |#35

    @云在天边
    呃,GAE和Micolog的邮局不是很好整啊 邮件里面的链接还是改回来了。。
  11. avatar
    云在天边 2010-07-20 at 23:07 |#34

    @Mucid
    发送邮件是系统自动发送的,我也不清楚是咋回事 我在后台也看到了发出很多邮件,不知道是为什么
  12. avatar
    Mucid 2010-07-20 at 23:03 |#33

    大哥最近发了好多重复的邮件,但是邮件里面的链接还是ru米米,还木有改过来啊
  13. avatar
    云在天边 2010-07-18 at 09:10 |#32

    @dengmin
    使用的是CSS3效果IE8以下的浏览器是不支持的,虽然我平时使用的最多的是IE8,但毕竟有人使用其他浏览器,所以就把这个加进来了
  14. avatar
    dengmin 2010-07-18 at 08:58 |#31

    @云在天边
    我是在pps上看的,你这回复的圆角边框在ie下怎么看不出来
  15. avatar
    云在天边 2010-07-18 at 08:54 |#30

    @dengm
    我感觉那个还不错啊,前天我才把720P高清全集下载下来,现在我都看完19集了
  16. avatar
    dengm 2010-07-18 at 08:01 |#29

    蒲松龄刚刚于昨天晚上看看,哈哈
  17. avatar
    云在天边 2010-07-17 at 21:12 |#28

    @Mucid
    呵呵,我只是加上了CSS3圆角效果
  18. avatar
    Mucid 2010-07-17 at 15:33 |#27

    大哥最近泡MM去了啊 ,这个新的评论框好好看,圆圆的很有爱
  19. avatar
    等待思索 2010-07-17 at 13:39 |#26

    @云在天边杯具IE,没事,回来在我那上写明,不能用IE浏览器看我博客。娃哈哈!
  20. avatar
    云在天边 2010-07-17 at 09:23 |#25

    @等待思索
    现在倒是好了,不过你的主题在IE下都跑到最左边了
Comment pages 123»
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
/static/smilies/icon_twisted.gif /static/smilies/icon_smile.gif /static/smilies/icon_cry.gif /static/smilies/icon_question.gif /static/smilies/icon_razz.gif /static/smilies/icon_mrgreen.gif /static/smilies/icon_sad.gif /static/smilies/icon_evil.gif /static/smilies/icon_exclaim.gif /static/smilies/icon_redface.gif /static/smilies/icon_biggrin.gif /static/smilies/icon_surprised.gif /static/smilies/icon_eek.gif /static/smilies/icon_confused.gif /static/smilies/icon_cool.gif /static/smilies/icon_lol.gif /static/smilies/icon_mad.gif /static/smilies/icon_rolleyes.gif /static/smilies/icon_wink.gif /static/smilies/icon_idea.gif /static/smilies/icon_arrow.gif /static/smilies/icon_neutral.gif
capacha 请输入验证码(不区分大小写)