| 1 |
=========== |
| 2 |
Jinja2 Time |
| 3 |
=========== |
| 4 |
|
| 5 |
|pypi| |pyversions| |license| |travis-ci| |
| 6 |
|
| 7 |
Jinja2 Extension for Dates and Times |
| 8 |
|
| 9 |
.. |pypi| image:: https://img.shields.io/pypi/v/jinja2-time.svg |
| 10 |
:target: https://pypi.python.org/pypi/jinja2-time |
| 11 |
:alt: PyPI Package |
| 12 |
|
| 13 |
.. |pyversions| image:: https://img.shields.io/pypi/pyversions/jinja2-time.svg |
| 14 |
:target: https://pypi.python.org/pypi/jinja2-time/ |
| 15 |
:alt: PyPI Python Versions |
| 16 |
|
| 17 |
.. |license| image:: https://img.shields.io/pypi/l/jinja2-time.svg |
| 18 |
:target: https://pypi.python.org/pypi/jinja2-time |
| 19 |
:alt: PyPI Package License |
| 20 |
|
| 21 |
.. |travis-ci| image:: https://travis-ci.org/hackebrot/jinja2-time.svg?branch=master |
| 22 |
:target: https://travis-ci.org/hackebrot/jinja2-time |
| 23 |
:alt: See Build Status on Travis CI |
| 24 |
|
| 25 |
Installation |
| 26 |
------------ |
| 27 |
|
| 28 |
**jinja2-time** is available for download from `PyPI`_ via `pip`_:: |
| 29 |
|
| 30 |
$ pip install jinja2-time |
| 31 |
|
| •32 |
It will automatically install `jinja2`_ and its dependencies. |
| 33 |
|
| 34 |
.. _`jinja2`: https://github.com/mitsuhiko/jinja2 |
| 35 |
.. _`PyPI`: https://pypi.python.org/pypi |
| 36 |
.. _`pip`: https://pypi.python.org/pypi/pip/ |
| 37 |
|
| 38 |
Usage |
| 39 |
----- |
| 40 |
|
| 41 |
Now Tag |
| 42 |
~~~~~~~ |
| 43 |
|
| •44 |
The extension comes with a ``now`` tag that retrieves the current datetime |
| 45 |
in a given time zone from your templates. Time zone specifiers supported are: |
| 46 |
|
| 47 |
- ``utc`` |
| 48 |
- ``local`` |
| 49 |
- ``[±]HH[:][MM]`` |
| 50 |
- Formats supported by `dateutil.tz.gettz`_ (IANA zones, GNU tz strings, etc) |
| 51 |
|
| 52 |
You can control the output by specifying a format, that will be passed to |
| 53 |
Python's `strftime()`_: |
| 54 |
|
| 55 |
.. _`strftime()`: https://docs.python.org/3.5/library/datetime.html#strftime-and-strptime-behavior |
| •56 |
.. _`dateutil.tz.gettz`: https://dateutil.readthedocs.io/en/latest/tz.html#dateutil.tz.gettz |
| 57 |
|
| 58 |
.. code-block:: python |
| 59 |
|
| 60 |
from jinja2 import Environment |
| 61 |
|
| 62 |
env = Environment(extensions=['jinja2_time.TimeExtension']) |
| 63 |
|
| 64 |
# Timezone 'local', default format -> "2015-12-10" |
| 65 |
template = env.from_string("{% now 'local' %}") |
| 66 |
|
| 67 |
# Timezone 'utc', explicit format -> "Thu, 10 Dec 2015 15:49:01" |
| 68 |
template = env.from_string("{% now 'utc', '%a, %d %b %Y %H:%M:%S' %}") |
| 69 |
|
| 70 |
# Timezone 'Europe/Berlin', explicit format -> "CET +0100" |
| 71 |
template = env.from_string("{% now 'Europe/Berlin', '%Z %z' %}") |
| 72 |
|
| 73 |
# Timezone 'utc', explicit format -> "2015" |
| 74 |
template = env.from_string("{% now 'utc', '%Y' %}") |
| 75 |
|
| 76 |
template.render() |
| 77 |
|
| 78 |
Default Datetime Format |
| 79 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 80 |
|
| 81 |
**TimeExtension** extends the environment with a ``datetime_format`` attribute. |
| 82 |
|
| 83 |
It is used as a fallback if you omit the format for ``now``. |
| 84 |
|
| 85 |
.. code-block:: python |
| 86 |
|
| 87 |
from jinja2 import Environment |
| 88 |
|
| 89 |
env = Environment(extensions=['jinja2_time.TimeExtension']) |
| 90 |
|
| 91 |
env.datetime_format = '%a, %d %b %Y %H:%M:%S' |
| 92 |
|
| 93 |
# Timezone 'utc', default format -> "Thu, 10 Dec 2015 15:49:01" |
| 94 |
template = env.from_string("{% now 'utc' %}") |
| 95 |
|
| 96 |
template.render() |
| 97 |
|
| 98 |
Time Offset |
| 99 |
~~~~~~~~~~~ |
| 100 |
|
| 101 |
**jinja2-time** implements a convenient interface to modify ``now`` by a |
| 102 |
relative time offset: |
| 103 |
|
| 104 |
.. code-block:: python |
| 105 |
|
| 106 |
# Examples for now "2015-12-09 23:33:01" |
| 107 |
|
| 108 |
# "Thu, 10 Dec 2015 01:33:31" |
| 109 |
"{% now 'utc' + 'hours=2, seconds=30' %}" |
| 110 |
|
| 111 |
# "Wed, 09 Dec 2015 23:22:01" |
| 112 |
"{% now 'utc' - 'minutes=11' %}" |
| 113 |
|
| 114 |
# "07 Dec 2015 23:00:00" |
| 115 |
"{% now 'utc' - 'days=2, minutes=33, seconds=1', '%d %b %Y %H:%M:%S' %}" |
| 116 |
|
| 117 |
Further documentation on the underlying functionality can be found in the |
| •118 |
`dateutil relativedelta docs`_. |
| 119 |
|
| •120 |
.. _`dateutil relativedelta docs`: https://dateutil.readthedocs.io/en/latest/relativedelta.html#module-dateutil.relativedelta |
| 121 |
|
| 122 |
|
| 123 |
Issues |
| 124 |
------ |
| 125 |
|
| 126 |
If you encounter any problems, please `file an issue`_ along with a detailed description. |
| 127 |
|
| 128 |
.. _`file an issue`: https://github.com/hackebrot/jinja2-time/issues |
| 129 |
|
| 130 |
|
| 131 |
Code of Conduct |
| 132 |
--------------- |
| 133 |
|
| 134 |
Everyone interacting in the jinja2-time project's codebases, issue trackers, chat |
| 135 |
rooms, and mailing lists is expected to follow the `PyPA Code of Conduct`_. |
| 136 |
|
| 137 |
.. _`PyPA Code of Conduct`: https://www.pypa.io/en/latest/code-of-conduct/ |
| 138 |
|
| 139 |
License |
| 140 |
------- |
| 141 |
|
| 142 |
Distributed under the terms of the `MIT`_ license, jinja2-time is free and open source software |
| 143 |
|
| 144 |
.. image:: https://opensource.org/trademarks/osi-certified/web/osi-certified-120x100.png |
| 145 |
:align: left |
| 146 |
:alt: OSI certified |
| 147 |
:target: https://opensource.org/ |
| 148 |
|
| 149 |
.. _`MIT`: http://opensource.org/licenses/MIT |