datetime iso8601字符串输入 [英] datetime iso8601 string input

查看:124
本文介绍了datetime iso8601字符串输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近发现

我有点意外,因为datetime无法输入字符串

值。 PEP 321出现并没有传达太多的信息,但是一对夫妇在两年前发表的一篇帖子澄清了一些事情:

http://tinyurl.com/epjqc

你可以停止寻找:datetime不支持从字符串进行任何类型的转换。
任何日期时间无底洞的数量都是无限制的,而且Guido声明这个特定的坑 - 一开始就有这样的结果
有机会获得
*任何*为2.3完成。




我能理解为什么datetime无法处理

任意字符串输入,但为什么不只是

简单的iso8601格式 - 即datetime的默认

输出格式?


给定日期时间生成的字符串:

now = STR(datet ime.datetime.now())
立即打印



''2006-02-23 11:03:36.762172''


为什么我们不能接受它的功能

作为字符串输入并返回一个datetime对象?


datetime.parse_iso8601(现在)


Jeff Bauer

Rubicon,Inc。

解决方案

Rubic写道:

我能理解为什么datetime无法处理任意字符串输入,但为什么不只是简单的iso8601格式 - 即datetime的默认输出格式?




您是否真的阅读过ISO 8601标准?

如果有,请你会知道解析有效

ISO 8601非常接近处理任意

字符串...


我怀疑这个缺席是很有意思。


有很多可能的字符串格式,使用

和在不同情况下优先选择,

和带时区的各种循环漏洞以及DST

等等,它可能更好地让

程序员对这个

负全部责任,而不是提供10%的解决方案。


ater all,这并不是很难用/>
time.strptime()具有一些适当的格式,

并从中构建日期时间对象。然后

程序员掌控。


Magnus,


感谢您的回复。我之前的帖子中我不清楚我的

。而不是支持整个

范围的iso8601格式,而* date *

格式的日期时间如何?称之为

parse_datetime()函数。然后将

作为日期时间

对象的输出字符串,并将其读回以往返

数据。

now = str(datetime.datetime.now())
现在
' '2006-02-24 06:58:23.737586''datetime.parse_datetime(now)



datetime.datetime(2006,2,24,6) ,58,23,737586)


Jeff Bauer

Rubicon,Inc。


< blockquote> Rubic写道:

感谢您的回复。我之前的帖子里并不清楚。而不是支持整个
范围的iso8601格式,如何*只是* datetime发出的格式?称之为
parse_datetime()函数。然后可以获取日期时间
对象的输出字符串并将其读回以往返
数据。

>>> now = str(datetime.datetime.now())
>>>现在''2006-02-24 06:58:23.737586''>>> datetime.parse_datetime(now)


datetime.datetime(2006,2,24,6,58,23,737586)

Jeff Bauer
Rubicon,Inc。




如果这真的是你想要的全部,那么它就近乎琐碎的功能了。

写。其中一个扔掉的在每个项目中,你倾向于砸几个函数,几乎没有注意到,因为你使用的是
Python,这很容易做到。 br />

-Peter


I was a little surprised to recently discover
that datetime has no method to input a string
value. PEP 321 appears does not convey much
information, but a timbot post from a couple
years ago clarifies things:

http://tinyurl.com/epjqc

You can stop looking: datetime doesn''t
support any kind of conversion from string.
The number of bottomless pits in any datetime
module is unbounded, and Guido declared this
particular pit out-of-bounds at the start so
that there was a fighting chance to get
*anything* done for 2.3.



I can understand why datetime can''t handle
arbitrary string inputs, but why not just
simple iso8601 format -- i.e. the default
output format for datetime?

Given a datetime-generated string:

now = str(datetime.datetime.now())
print now


''2006-02-23 11:03:36.762172''

Why can''t we have a function to accept it
as string input and return a datetime object?

datetime.parse_iso8601(now)

Jeff Bauer
Rubicon, Inc.

解决方案

Rubic wrote:

I can understand why datetime can''t handle
arbitrary string inputs, but why not just
simple iso8601 format -- i.e. the default
output format for datetime?



Have you actually read the ISO 8601 standard?
If you have, you would know that parsing valid
ISO 8601 is fairly close to handling arbitrary
strings...

I suspect this absence is quite deliberate.

There are so many possible string formats that
are used and prefered in different situations,
and various loop holes with time zones and DST
etc, that it''s probably better to let the
programmer take full responsibility over this
instead of providing a 10% solution.

ater all, it isn''t very hard to use for instance
time.strptime() with some appropriate format,
and build datetime objects from that. Then
the programmer is in control.


Magnus,

Thanks for your reply. I wasn''t clear in my
prior post. Rather than support the entire
range of iso8601 formats, how about *just* the
format that datetime emits? Call it the
parse_datetime() function. Then it would be
possible to take the output string of a datetime
object and read it back in to round-trip the
data.

now = str(datetime.datetime.now())
now ''2006-02-24 06:58:23.737586'' datetime.parse_datetime(now)


datetime.datetime(2006, 2, 24, 6, 58, 23, 737586)

Jeff Bauer
Rubicon, Inc.


Rubic wrote:

Thanks for your reply. I wasn''t clear in my
prior post. Rather than support the entire
range of iso8601 formats, how about *just* the
format that datetime emits? Call it the
parse_datetime() function. Then it would be
possible to take the output string of a datetime
object and read it back in to round-trip the
data.

>>> now = str(datetime.datetime.now())
>>> now ''2006-02-24 06:58:23.737586'' >>> datetime.parse_datetime(now)


datetime.datetime(2006, 2, 24, 6, 58, 23, 737586)

Jeff Bauer
Rubicon, Inc.



If that''s truly all you want, then it''s a near trivial function to
write. One of those "throw-away" functions you tend to whip up a few
dozen times on every project, hardly noticing because you''re using
Python and it''s so easy to do.

-Peter


这篇关于datetime iso8601字符串输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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