Registry / web-framework / jinja2-time

jinja2-time

JSON →
library0.2.0pypypi✓ verified 23d ago

Jinja2-time is a Jinja2 extension that provides a `now` tag for convenient access to date and time functionalities directly within Jinja2 templates. It allows retrieving the current time in various timezones, formatting it using Python's `strftime` patterns, and applying relative time offsets. The current version is 0.2.0, released in June 2016. It relies on Jinja2 and `arrow` for robust date and time handling.

pip install jinja2-time
INSTALL
IMPORT
SIG · JINJA2-TIME
J
jinja2-time
web-frameworkpythonv0.2.0
Install
2.1s avg
Import
217ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.234s · 23.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.1s · import 0.200s · 24MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TimeExtension
from jinja2_time import TimeExtension
from jinja2.ext import TimeExtension
The extension is provided by the `jinja2_time` package, not directly from `jinja2.ext`.

This quickstart demonstrates how to initialize a Jinja2 environment with `jinja2-time` and use the `now` tag to display the current time in various timezones, with custom formatting, and with relative time offsets. It also shows how to set a default datetime format for the environment.

from jinja2 import Environment # Configure Jinja2 environment with the TimeExtension env = Environment(extensions=['jinja2_time.TimeExtension']) # Example 1: Get current UTC time with default format template_utc_default = env.from_string("{% now 'utc' %}") print(f"UTC (default format): {template_utc_default.render()}") # Example 2: Get current UTC time with explicit strftime format template_utc_formatted = env.from_string("{% now 'utc', '%a, %d %b %Y %H:%M:%S' %}") print(f"UTC (formatted): {template_utc_formatted.render()}") # Example 3: Get local time with a relative offset (add 2 hours, 30 seconds) template_offset = env.from_string("{% now 'local' + 'hours=2,seconds=30' %}") print(f"Local (+2h30s): {template_offset.render()}") # Example 4: Set a default datetime format for the environment env.datetime_format = '%Y-%m-%d %H:%M:%S %Z' template_local_default_env = env.from_string("{% now 'local' %}") print(f"Local (env default format): {template_local_default_env.render()}")
Debug
Known issues
breakingThe `jinja2-time` library has not been updated since its 0.2.0 release in June 2016. It may have compatibility issues with newer major versions of Jinja2 (e.g., Jinja2 3.x), which introduced breaking changes such as the deprecation of `jinja2.Markup`.
fix
Pin your Jinja2 dependency to `<3.0` if you encounter compatibility issues, or consider implementing custom Jinja2 filters/extensions for date/time functionality if an upgrade to Jinja2 3.x is critical.
affects: All versions of jinja2-time when used with Jinja2 >= 3.0
gotchaTimezone handling can be tricky. The `{% now %}` tag defaults to the server's local timezone if no timezone is explicitly provided. For predictable and consistent results, always specify a timezone like `'utc'` or a valid IANA timezone string (e.g., `'Europe/Berlin'`).
fix
Explicitly define the desired timezone in the `{% now 'timezone' %}` tag, e.g., `{% now 'utc' %}` or `{% now 'America/New_York' %}`.
affects: 0.1.0, 0.2.0
gotchaThe relative time offset syntax uses a specific string format (e.g., `'hours=2,seconds=30'`). Incorrectly formatted strings will lead to parsing errors or the offset being ignored. Ensure the key-value pair syntax is followed precisely.
fix
Refer to the `arrow.Arrow.replace` documentation for valid arguments and ensure the offset string adheres to the `'key=value,key2=value2'` format (e.g., `{% now 'utc' + 'years=1,months=-3,days=15' %}`).
affects: 0.2.0
gotchaThe format string for the `now` tag relies on Python's `strftime` codes. If you're unfamiliar with these codes, formatting may not yield the expected output. There are many subtle differences from other formatting languages.
fix
Consult Python's official `strftime` documentation for a comprehensive list of format codes and their meanings to ensure correct date and time string representation.
affects: 0.1.0, 0.2.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'jinja2-time'
The `jinja2-time` library has not been installed in the Python environment.
fix
Install the library using pip: `pip install jinja2-time`
Encountered unknown tag 'now'
The `TimeExtension` from `jinja2-time` has not been properly loaded and registered with the Jinja2 environment.
fix
When creating your Jinja2 Environment, include 'jinja2_time.TimeExtension' in the `extensions` list: `env = Environment(extensions=['jinja2_time.TimeExtension'])`
Invalid template syntax
This error often occurs when the arguments for the `{% now %}` tag, especially time offsets or format strings, are incorrectly specified, leading to a parsing error in the Jinja2 template.
fix
Ensure that time offsets are correctly formatted (e.g., `'hours=2, seconds=30'`) and that format strings adhere to Python's `strftime` patterns, enclosed in quotes. For example: `{% now 'utc' + 'hours=2', '%Y-%m-%d %H:%M:%S' %}`
Incorrect date/time displayed (e.g., timezone issues)
By default, `jinja2-time`'s `now` tag outputs UTC time. Users often expect local time or a specific timezone, leading to discrepancies if not explicitly handled.
fix
Specify the desired timezone as the first argument to the `{% now %}` tag. For example, to display local time, use `{% now 'local' %}`, or for a specific timezone like New York, use `{% now 'America/New_York' %}`.
Upgrade
Version history
0.2.0latest on PyPI · released Jun 8, 2016
Audit
Dependencies
Jinja2requiredCore templating engine.
arrowrequiredUnderlying library for date and time manipulation.
python-dateutilrequiredUsed for timezone parsing by `arrow` and directly supported for timezone specifiers.
Agent activity
13 hits · last 30 days
node
10
Resources
jinja2-time — pip install jinja2-time · libregistry