如何将 RFC822 转换为 python 日期时间对象?

发布于 2024-08-07 23:55:23 字数 118 浏览 6 评论 0原文

我知道如何以相反的方式做到这一点......那就是:

>>> dt.rfc822()
'Sun, 09 Mar 1997 13:45:00 -0500'

I know how to do this the other way around... it would be:

>>> dt.rfc822()
'Sun, 09 Mar 1997 13:45:00 -0500'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风来 2024-08-14 23:55:23

In [1]: import rfc822     # This only works for python 2 series

In [2]: rfc822.parsedate_tz('Sun, 09 Mar 1997 13:45:00 -0500')
Out[2]: (1997, 3, 9, 13, 45, 0, 0, 1, 0, -18000)

在 python3 中,parsedate_tz 已移至 email.utils


>>> import email.utils   # this works on Python2.5 and up
>>> email.utils.parsedate_tz('Sun, 09 Mar 1997 13:45:00 -0500')
(1997, 3, 9, 13, 45, 0, 0, 1, -1, -18000)

In [1]: import rfc822     # This only works for python 2 series

In [2]: rfc822.parsedate_tz('Sun, 09 Mar 1997 13:45:00 -0500')
Out[2]: (1997, 3, 9, 13, 45, 0, 0, 1, 0, -18000)

in python3 parsedate_tz has moved to email.utils


>>> import email.utils   # this works on Python2.5 and up
>>> email.utils.parsedate_tz('Sun, 09 Mar 1997 13:45:00 -0500')
(1997, 3, 9, 13, 45, 0, 0, 1, -1, -18000)
↙温凉少女 2024-08-14 23:55:23

从 python 3.3 开始,有 email.utils.parsedate_to_datetime(日期)

>>> from email.utils import parsedate_to_datetime
>>> datestr = 'Sun, 09 Mar 1997 13:45:00 -0500'
>>> parsedate_to_datetime(datestr)
datetime.datetime(1997, 3, 9, 13, 45, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))

As of python 3.3 there is email.utils.parsedate_to_datetime(date)

>>> from email.utils import parsedate_to_datetime
>>> datestr = 'Sun, 09 Mar 1997 13:45:00 -0500'
>>> parsedate_to_datetime(datestr)
datetime.datetime(1997, 3, 9, 13, 45, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))
还给你自由 2024-08-14 23:55:23

如果你去掉时区,你可以这样做:

datetime.datetime.strptime('Sun, 09 Mar 1997 13:45:00', '%a, %d %b %Y %H:%M:%S')

If you strip off the time zone, you can do it like this:

datetime.datetime.strptime('Sun, 09 Mar 1997 13:45:00', '%a, %d %b %Y %H:%M:%S')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文