python - 如何从Python中的日期解析时区

标签 python timezone date

这是我到目前为止所尝试过的。我只是不太确定为什么它输出为 PST 而不是 GMT。我不确定这是否不是解析它或输出它的正确方法。好像有什么地方不对劲。

>>> x = time.strptime('Wed, 27 Oct 2010 22:17:00 GMT', '%a, %d %b %Y %H:%M:%S %Z')

>>> time.strftime('%a, %d %b %Y %H:%M:%S %Z', x)

'Wed, 27 Oct 2010 22:17:00 PST'

感谢任何帮助,

最佳答案

首先我建议您使用 dateutil

>>> import dateutil.parser

>>> x = dateutil.parser.parse('Wed, 27 Oct 2010 22:17:00 GMT')
datetime.datetime(2010, 10, 27, 22, 17, tzinfo=tzutc())
>>> str(x)
'2010-10-27 22:17:00+00:00'
>>> x.strftime('%a, %d %b %Y %H:%M:%S %Z')
'Wed, 27 Oct 2010 22:17:00 UTC'

关于python - 如何从Python中的日期解析时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4038797/

相关文章:

python - VS Code 在我的 Python 脚本中随机导入库

用于检索假负值的 Python 编程

python - 为什么我们需要在 Huggingface Transformers 的 BERT 预训练模型中使用 init_weight 函数?

ruby-on-rails - 从 Rails 日期时间列中查询一天中的时间(带时区)

php - 获取给定日期的日期范围

java - 需要根据当前日期列出前几个季度的列表

python - Unicode 与 Unicode 表示法

timezone - 如何更改 Frontaccounting 中的时区?

c# - .NET 中的 "US Eastern Standard Time"与 "Eastern Standard Time"

java - jackson @JsonFormat 意外的时区输出