繁体   English   中英

Python 将 datetime.time 转换为箭头

[英]Python convert datetime.time to arrow

我需要将 python 对象datetime.time转换为箭头对象。

y = datetime.time()
>>> y
datetime.time(0, 0)
>>> arrow.get(y)

TypeError: Can't parse single argument type of '<type 'datetime.time'>'

Arrow 遵循其文档中指定的特定格式:

arrow.get('2013-05-11T21:23:58.970460+00:00')  

您需要将日期时间对象转换为箭头可理解的格式,以便能够将其转换为箭头对象。 下面的代码块应该可以工作:

from datetime import datetime
import arrow

arrow.get(datetime.now())

您可以使用strptime的类方法arrow.Arrow类,并应用适当的formattting:

y = datetime.time()
print(arrow.Arrow.strptime(y.isoformat(), '%H:%M:%S'))
# 1900-01-01T00:00:00+00:00

但是,您会注意到日期值是默认值,因此最好解析datetime time对象而不是time对象。

datetime.time对象包含“一个理想化的时间,独立于任何特定的一天”。 箭头对象不能表示这种部分信息,因此您必须先用今天的日期或其他日期“填写”。

from datetime import time, date, datetime
t = time(12, 34)
a = arrow.get(datetime.combine(date.today(), t))  # <Arrow [2019-11-14T12:34:00+00:00]>
a = arrow.get(datetime.combine(date(1970, 1, 1), t))  # <Arrow [1970-01-01T12:34:00+00:00]>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM