[英]Parse date and time from string with time zone using Arrow
I have我有
import arrow
s = '2015/12/1 19:00:00'
tz = 'Asia/Hong_Kong'
How can I parse this with Arrow such that I get an Arrow object with the time zone tz
?我怎样才能用 Arrow 解析这个,这样我才能得到一个时区为
tz
的 Arrow 对象? The following defaults to UTC time.以下默认为 UTC 时间。
In [30]: arrow.get(s, 'YYYY/M/D HH:mm:ss')
Out[30]: <Arrow [2015-12-01T19:00:00+00:00]>
I know the .to
function but that converts a time zone and but doesn't allow me to change to time zone.我知道
.to
函数,但它会转换时区,但不允许我更改时区。
尝试这个:
arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo=dateutil.tz.gettz(tz))
I'm not qualified yet to add a comment and would just like to share a bit simpler version of the answer with timezone str expression.我还没有资格添加评论,只想用时区 str 表达式分享一个更简单的答案版本。
s = '2015/12/1 19:00:00'
tz = 'Asia/Hong_Kong'
arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo=tz)
or simply local timezone:或者只是本地时区:
arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo='local')
or specified ISO-8601 style:或指定的 ISO-8601 样式:
arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo='+08:00')
Per the current documentation , you can also provide a default timezone for arrow.get()
, eg:根据当前文档,您还可以为
arrow.get()
提供默认时区,例如:
s = '2015/12/1 19:00:00'
tz = 'Asia/Hong_Kong'
arrow.get(s, tzinfo=tz)
However, as of right now (version 0.12.1) there is a shortcoming where that doesn't work for string-based date parsing.但是,截至目前(版本 0.12.1)存在一个缺点,即它不适用于基于字符串的日期解析。 Fortunately, this has been fixed , so the next release of Arrow will integrate this fix.
幸运的是,这已经被修复了,所以下一个版本的 Arrow 将集成这个修复。
This is working for me as of 0.10.0:从 0.10.0 开始,这对我有用:
import arrow
s = '2015/12/1 19:00:00'
tz = 'Asia/Hong_Kong'
arrow.get(s, 'YYYY/M/D HH:mm:ss', tzinfo=tz)
# <Arrow [2015-12-01T19:00:00+08:00]>
However, arrow.get('2018-01-29 14:46', tzinfo='US/Central')
(ie without the format string) ignores the tzinfo
parameter.但是,
arrow.get('2018-01-29 14:46', tzinfo='US/Central')
(即没有格式字符串)忽略了tzinfo
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.