File: README.rst

package info (click to toggle)
jinja2-time 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 132 kB
  • sloc: python: 143; makefile: 3
file content (145 lines) | stat: -rw-r--r-- 4,107 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
===========
Jinja2 Time
===========

|pypi| |pyversions| |license| |travis-ci|

Jinja2 Extension for Dates and Times

.. |pypi| image:: https://img.shields.io/pypi/v/jinja2-time.svg
   :target: https://pypi.python.org/pypi/jinja2-time
   :alt: PyPI Package

.. |pyversions| image:: https://img.shields.io/pypi/pyversions/jinja2-time.svg
   :target: https://pypi.python.org/pypi/jinja2-time/
   :alt: PyPI Python Versions

.. |license| image:: https://img.shields.io/pypi/l/jinja2-time.svg
   :target: https://pypi.python.org/pypi/jinja2-time
   :alt: PyPI Package License

.. |travis-ci| image:: https://travis-ci.org/hackebrot/jinja2-time.svg?branch=master
    :target: https://travis-ci.org/hackebrot/jinja2-time
    :alt: See Build Status on Travis CI

Installation
------------

**jinja2-time** is available for download from `PyPI`_ via `pip`_::

    $ pip install jinja2-time

It will automatically install `jinja2`_ along with `arrow`_.

.. _`jinja2`: https://github.com/mitsuhiko/jinja2
.. _`PyPI`: https://pypi.python.org/pypi
.. _`arrow`: https://github.com/crsmithdev/arrow
.. _`pip`: https://pypi.python.org/pypi/pip/

Usage
-----

Now Tag
~~~~~~~

The extension comes with a ``now`` tag that provides convenient access to the
`arrow.now()`_ API from your templates.

You can control the output by specifying a format, that will be passed to
Python's `strftime()`_:

.. _`arrow.now()`: http://crsmithdev.com/arrow/#arrow.factory.ArrowFactory.now
.. _`strftime()`: https://docs.python.org/3.5/library/datetime.html#strftime-and-strptime-behavior

.. code-block:: python

    from jinja2 import Environment

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

    # Timezone 'local', default format -> "2015-12-10"
    template = env.from_string("{% now 'local' %}")

    # Timezone 'utc', explicit format -> "Thu, 10 Dec 2015 15:49:01"
    template = env.from_string("{% now 'utc', '%a, %d %b %Y %H:%M:%S' %}")

    # Timezone 'Europe/Berlin', explicit format -> "CET +0100"
    template = env.from_string("{% now 'Europe/Berlin', '%Z %z' %}")

    # Timezone 'utc', explicit format -> "2015"
    template = env.from_string("{% now 'utc', '%Y' %}")

    template.render()

Default Datetime Format
~~~~~~~~~~~~~~~~~~~~~~~

**TimeExtension** extends the environment with a ``datetime_format`` attribute.

It is used as a fallback if you omit the format for ``now``.

.. code-block:: python

    from jinja2 import Environment

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

    env.datetime_format = '%a, %d %b %Y %H:%M:%S'

    # Timezone 'utc', default format -> "Thu, 10 Dec 2015 15:49:01"
    template = env.from_string("{% now 'utc' %}")

    template.render()

Time Offset
~~~~~~~~~~~

**jinja2-time** implements a convenient interface to modify ``now`` by a
relative time offset:

.. code-block:: python

    # Examples for now "2015-12-09 23:33:01"

    # "Thu, 10 Dec 2015 01:33:31"
    "{% now 'utc' + 'hours=2, seconds=30' %}"

    # "Wed, 09 Dec 2015 23:22:01"
    "{% now 'utc' - 'minutes=11' %}"

    # "07 Dec 2015 23:00:00"
    "{% now 'utc' - 'days=2, minutes=33, seconds=1', '%d %b %Y %H:%M:%S' %}"

Further documentation on the underlying functionality can be found in the
`arrow replace docs`_.

.. _`arrow replace docs`: http://arrow.readthedocs.io/en/latest/#replace-shift


Issues
------

If you encounter any problems, please `file an issue`_ along with a detailed description.

.. _`file an issue`: https://github.com/hackebrot/jinja2-time/issues


Code of Conduct
---------------

Everyone interacting in the jinja2-time project's codebases, issue trackers, chat
rooms, and mailing lists is expected to follow the `PyPA Code of Conduct`_.

.. _`PyPA Code of Conduct`: https://www.pypa.io/en/latest/code-of-conduct/

License
-------

Distributed under the terms of the `MIT`_ license, jinja2-time is free and open source software

.. image:: https://opensource.org/trademarks/osi-certified/web/osi-certified-120x100.png
   :align: left
   :alt: OSI certified
   :target: https://opensource.org/

.. _`MIT`: http://opensource.org/licenses/MIT