Skip to content

Relative time offset does not work #18

Open
@dangrazh

Description

@dangrazh

I tried to use the following expression:
{% now 'local' - 'days=1', '%d %b %Y %H:%M:%S' %} which leads to the following error: ValueError: Unknown attribute: 'days'

I am working with:

Package         Version
--------------- -------
arrow           1.2.1
Jinja2          3.0.3
jinja2-time     0.2.0

In more detail this is the script:

import jinja2
from jinja2 import Environment, BaseLoader

env = Environment(loader=BaseLoader(), extensions=['jinja2_time.TimeExtension'])

myString = "{% now 'local' - 'days=1', '%d %b %Y %H:%M:%S' %}"
rtemplate = env.from_string(myString)
data = rtemplate.render()
print(data)

and this is the output:

Traceback (most recent call last):
  File "P:\Programming\Python\temp\test_jinja2_time.py", line 9, in <module>
    data = rtemplate.render()
  File "P:\Programming\Python\temp\sandbox\lib\site-packages\jinja2\environment.py", line 1291, in render
    self.environment.handle_exception()
  File "P:\Programming\Python\temp\sandbox\lib\site-packages\jinja2\environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "P:\Programming\Python\temp\sandbox\lib\site-packages\jinja2_time\jinja2_time.py", line 26, in _datetime
    d = d.replace(**replace_params)
  File "P:\Programming\Python\temp\sandbox\lib\site-packages\arrow\arrow.py", line 974, in replace
    raise ValueError(f"Unknown attribute: {key!r}.")
ValueError: Unknown attribute: 'days'.

Looking at the source code, it looks like the d = d.replace(**replace_params) should be switched to d = d.shift(**replace_params) to achieve relative time offset. I did quickly test this against this specific expression and it worked as expected (for this expression).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions