首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能加strptime,只做减法呢?

为什么我不能加strptime,只做减法呢?
EN

Stack Overflow用户
提问于 2018-05-20 21:17:40
回答 1查看 131关注 0票数 2

这可以很好地工作:

代码语言:javascript
复制
{{ strptime(states('input_datetime.music_alarm'), "%H:%M:%S") - strptime("10", "%M") }} 

但这会抛出一个错误:

代码语言:javascript
复制
{{ strptime(states('input_datetime.music_alarm'), "%H:%M:%S") + strptime("10", "%M") }} 

states('input_datetime.music_alarm')等于一个时间,如08:00:00

我在用jinja2做家政助理。这就是错误。

代码语言:javascript
复制
Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/automation/__init__.py", line 336, in async_trigger
    yield from self._async_action(self.entity_id, variables)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/automation/__init__.py", line 425, in action
    yield from script_obj.async_run(variables)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/script.py", line 158, in async_run
    await self._async_call_service(action, variables)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/script.py", line 187, in _async_call_service
    self.hass, action, True, variables, validate_config=False)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/service.py", line 72, in async_call_from_config
    config[CONF_SERVICE_DATA_TEMPLATE], variables))
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py", line 56, in render_complex
    for key, item in value.items()}
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py", line 56, in <dictcomp>
    for key, item in value.items()}
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py", line 57, in render_complex
    return value.async_render(variables)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/template.py", line 132, in async_render
    return self._compiled.render(kwargs).strip()
  File "/srv/homeassistant/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/srv/homeassistant/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: bad operand type for unary -: 'datetime.datetime'
EN

回答 1

Stack Overflow用户

发布于 2018-05-21 19:08:58

您必须使用timedelta,并且您的代码中存在多个问题。

1/错误使用datetime.datetime.strptime()

代码语言:javascript
复制
>>> import datetime
>>> print(datetime.datetime.strptime("10", "%M"))
1900-01-01 00:10:00
>>> print(datetime.datetime.strptime('08:00:00', "%H:%M:%S"))
1900-01-01 08:00:00

您必须解析完整的datetime才能拥有正确的行为。

2/不能将两个日期时间相加

基本上,为了避免错误,+是被禁止的,你只需要-,因为你只需要颠倒表达式中的变量,就可以得到负的时间增量。

代码语言:javascript
复制
>>> print(type(datetime.datetime.strptime("10", "%M") - datetime.datetime.strptime('08:00:00', "%H:%M:%S")))
<class 'datetime.timedelta'>
>>> print(datetime.datetime.strptime("10", "%M") - datetime.datetime.strptime('08:00:00', "%H:%M:%S"))
-1 day, 16:10:00
>>> print(datetime.datetime.strptime('08:00:00', "%H:%M:%S") - datetime.datetime.strptime("10", "%M"))
7:50:00

你可以看到相反的顺序,这会给你不正确的-1 day, 16:10:00,因为这不能没有错误地处理。

3/您可以将timedelta注册到您的模板

在Jinja2中,strptime()在默认情况下是不可用的,所以请使用TimeDelta...

大概是这样的:

代码语言:javascript
复制
import datetime
from jinja2 import Template

jinga = Template('{{ strptime(states("input_datetime.music_alarm"), "%H:%M:%S") - timedelta(minutes=10) }} ')
jinga.globals['timedelta'] = datetime.timedelta
print(jinga.render())
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50435234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档