datetime.strptime()的Python时区'%z'指令不可用 [英] Python timezone '%z' directive for datetime.strptime() not available

查看:3268
本文介绍了datetime.strptime()的Python时区'%z'指令不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用datetime.strptime()

的'%z'模式

我有一个表示日期的字符串文本,我完全能够解析它并转换它变成一个干净的datetime对象:

  date =[24 / Aug / 2014:17:57:26
dt = datetime.strptime(date,[%d /%b /%Y:%H:%M:%S)

除了我无法使用时区捕获整个日期字符串,使用%z pattern 指定这里

  date_tz = 24 / Aug / 2014:17:57:26 +0200 
dt = datetime.strptime(date,[%d /%b /%Y:%H:%M:%S%z] )
>>> ValueError:'z'是格式为'[%d /%b /%Y:%H:%M:%S%z]'


由于此错误报告


strftime()实现每个平台


我确切地说,没有这样一个问题,天真的tzinfo指令'%Z'



解决方法:将tzinfo字符串转换为tzinfo对象



我可以通过使用 dateutil 模块
,然后将 tzinfo插入到datetime对象



问题:让%z可用于我的平板?



但是,我显然需要%z模式我想进一步的项目一个解决方案,以避免这种解决方法,并使用外部模块进行这个简单的任务。你可以建议我一些阅读吗?我认为较新版本的python(我在2.7版本)可以处理它,但我宁愿不改变我的版本,现在这个小但关键的细节。





嗯,看到评论让我重新设定了我的问题,如何解析电子邮件时区指标使用strptime()而不知道本地时间?

解决方案

strptime()在纯Python中实现。不像 strftime();它支持哪些指令不依赖于平台。自从Python 3.2起支持%z

 >> ; from datetime import datetime 
>>>> datetime.strptime('24 / Aug / 2014:17:57:26 +0200','%d /%b /%Y:%H:%M:%S%z')
datetime.datetime 2014,8,24,17,57,26,tzinfo = datetime.timezone(datetime.timedelta(0,7200)))




如何使用strptime()解析电子邮件时区指标,而不知道本地时间?


Python 2.7中没有具体的时区实现。您可以轻松实施UTC偏移解析,请参阅如何使用python中的-0400时区字符串解析日期?


Using '%z' pattern of datetime.strptime()

I have a string text that represent a date and I'm perfectly able to parse it and transform it into a clean datetime object:

date = "[24/Aug/2014:17:57:26"
dt = datetime.strptime(date, "[%d/%b/%Y:%H:%M:%S")  

Except that I can't catch the entire date string with the timezone using the %z pattern as specified here

date_tz = 24/Aug/2014:17:57:26 +0200
dt = datetime.strptime(date, "[%d/%b/%Y:%H:%M:%S %z]")
>>> ValueError: 'z' is a bad directive in format '[%d/%b/%Y:%H:%M:%S %z]'

Because as this bug report says

strftime() is implemented per platform

I precise that there is no such a problem with the naive tzinfo directive '%Z'

Workaround : Casting tzinfo string into tzinfo object

I can perfectly make the following workaround by transforming the GST time format string into a tzinfo object [as suggested here][4] using dateutil module and then insert tzinfo into datetime object

Question: Make %z available for my plateform?

But as I will obviously need %z pattern for further project I would like to find a solution to avoid this workaround and using external module for this simple task. Can you suggest me some reading on it? I supposed that newer version of python (I'm on 2.7) can handle it but I'd rather not changing my version now for this little but crucial detail.

[EDIT]

Well, seeing comments make me reformulated my question how to parse Email time zone indicator using strptime() without being aware of locale time?

解决方案

strptime() is implemented in pure Python. Unlike strftime(); it [which directives are supported] doesn't depend on platform. %z is supported since Python 3.2:

>>> from datetime import datetime
>>> datetime.strptime('24/Aug/2014:17:57:26 +0200', '%d/%b/%Y:%H:%M:%S %z')
datetime.datetime(2014, 8, 24, 17, 57, 26, tzinfo=datetime.timezone(datetime.timedelta(0, 7200)))

how to parse Email time zone indicator using strptime() without being aware of locale time?

There is no concrete timezone implementation in Python 2.7. You could easily implement the UTC offset parsing, see How to parse dates with -0400 timezone string in python?

这篇关于datetime.strptime()的Python时区'%z'指令不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆