| 1 |
Description: Remove dependency on arrow |
| 2 |
Origin: https://github.com/hackebrot/jinja2-time/pull/5 |
| 3 |
Author: Paul Ganssle <[email protected]> |
| 4 |
Date: 2019-11-04 |
| 5 |
|
| 6 |
--- a/jinja2_time/jinja2_time.py |
| 7 |
+++ b/jinja2_time/jinja2_time.py |
| 8 |
@@ -1,6 +1,10 @@ |
| 9 |
# -*- coding: utf-8 -*- |
| 10 |
|
| 11 |
|
| 12 |
+import re |
| 13 |
+from datetime import datetime |
| 14 |
+ |
| 15 |
+from dateutil import tz |
| 16 |
+from dateutil.relativedelta import relativedelta |
| 17 |
|
| 18 |
from jinja2 import nodes |
| 19 |
from jinja2.ext import Extension |
| 20 |
@@ -16,14 +20,23 @@ |
| 21 |
environment.extend(datetime_format='%Y-%m-%d') |
| 22 |
|
| 23 |
def _datetime(self, timezone, operator, offset, datetime_format): |
| 24 |
|
| 25 |
+ tzi = _gettz(timezone) |
| 26 |
+ d = datetime.now(tzi) |
| 27 |
|
| 28 |
# Parse replace kwargs from offset and include operator |
| 29 |
|
| 30 |
+ rd_params = {} |
| 31 |
for param in offset.split(','): |
| 32 |
interval, value = param.split('=') |
| 33 |
|
| 34 |
|
| 35 |
+ rd_params[interval.strip()] = float(value.strip()) |
| 36 |
+ |
| 37 |
+ rd = relativedelta(**rd_params) |
| 38 |
+ |
| 39 |
+ if operator == '-': |
| 40 |
+ d -= rd |
| 41 |
+ elif operator == '+': |
| 42 |
+ d += rd |
| 43 |
+ else: |
| 44 |
+ raise ValueError('Unknown operator: %s' % operator) |
| 45 |
|
| 46 |
if datetime_format is None: |
| 47 |
datetime_format = self.environment.datetime_format |
| 48 |
@@ -32,7 +45,10 @@ |
| 49 |
def _now(self, timezone, datetime_format): |
| 50 |
if datetime_format is None: |
| 51 |
datetime_format = self.environment.datetime_format |
| 52 |
|
| 53 |
+ |
| 54 |
+ tzi = _gettz(timezone) |
| 55 |
+ return datetime.now(tzi).strftime(datetime_format) |
| 56 |
+ |
| 57 |
|
| 58 |
def parse(self, parser): |
| 59 |
lineno = next(parser.stream).lineno |
| 60 |
@@ -63,3 +79,47 @@ |
| 61 |
lineno=lineno, |
| 62 |
) |
| 63 |
return nodes.Output([call_method], lineno=lineno) |
| 64 |
+ |
| 65 |
+ |
| 66 |
+_TZOFFSET_MATCH = re.compile(r'(?P<sgn>[+-])?(?P<hh>\d{2}):?(?P<mm>\d{2})?$', |
| 67 |
+ flags=re.ASCII) |
| 68 |
+ |
| 69 |
+ |
| 70 |
+def _parse_isotz(tzstr): |
| 71 |
+ m = _TZOFFSET_MATCH.match(tzstr) |
| 72 |
+ if m: |
| 73 |
+ sign = -1 if m.group('sgn') == '-' else 1 |
| 74 |
+ |
| 75 |
+ hh = int(m.group('hh')) |
| 76 |
+ mm_g = mm.group('mm') |
| 77 |
+ mm = int(mm_g) if mm_g else 0 |
| 78 |
+ |
| 79 |
+ total_seconds = sign * (hh * 3600 + (mm * 60)) |
| 80 |
+ return tz.tzoffset(None, total_seconds) |
| 81 |
+ |
| 82 |
+ return None |
| 83 |
+ |
| 84 |
+ |
| 85 |
+def _gettz(tzstr): |
| 86 |
+ """ Parse a time zone string into a time zone """ |
| 87 |
+ |
| 88 |
+ tzstr_l = tzstr.lower() |
| 89 |
+ if not tzstr or tzstr_l == 'utc': |
| 90 |
+ return tz.tzutc() |
| 91 |
+ |
| 92 |
+ if tzstr_l == 'local': |
| 93 |
+ return tz.tzlocal() |
| 94 |
+ |
| 95 |
+ tzi = tz.gettz(tzstr) |
| 96 |
+ |
| 97 |
+ if tzi is None: |
| 98 |
+ tzi = _parse_isotz(tzstr) |
| 99 |
+ |
| 100 |
+ if tzi is None: |
| 101 |
+ raise TzParseError('Unknown time zone %s' % tzstr) |
| 102 |
+ |
| 103 |
+ return tzi |
| 104 |
+ |
| 105 |
+ |
| 106 |
+class TzParseError(ValueError): |
| 107 |
+ pass |
| 108 |
--- a/README.rst |
| 109 |
+++ b/README.rst |
| 110 |
@@ -29,11 +29,10 @@ |
| 111 |
|
| 112 |
$ pip install jinja2-time |
| 113 |
|
| 114 |
|
| 115 |
+It will automatically install `jinja2`_ and its dependencies. |
| 116 |
|
| 117 |
.. _`jinja2`: https://github.com/mitsuhiko/jinja2 |
| 118 |
.. _`PyPI`: https://pypi.python.org/pypi |
| 119 |
|
| 120 |
.. _`pip`: https://pypi.python.org/pypi/pip/ |
| 121 |
|
| 122 |
Usage |
| 123 |
@@ -42,14 +41,19 @@ |
| 124 |
Now Tag |
| 125 |
~~~~~~~ |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
+The extension comes with a ``now`` tag that retrieves the current datetime |
| 130 |
+in a given time zone from your templates. Time zone specifiers supported are: |
| 131 |
+ |
| 132 |
+- ``utc`` |
| 133 |
+- ``local`` |
| 134 |
+- ``[±]HH[:][MM]`` |
| 135 |
+- Formats supported by `dateutil.tz.gettz`_ (IANA zones, GNU tz strings, etc) |
| 136 |
|
| 137 |
You can control the output by specifying a format, that will be passed to |
| 138 |
Python's `strftime()`_: |
| 139 |
|
| 140 |
|
| 141 |
.. _`strftime()`: https://docs.python.org/3.5/library/datetime.html#strftime-and-strptime-behavior |
| 142 |
+.. _`dateutil.tz.gettz`: https://dateutil.readthedocs.io/en/latest/tz.html#dateutil.tz.gettz |
| 143 |
|
| 144 |
.. code-block:: python |
| 145 |
|
| 146 |
@@ -111,9 +115,9 @@ |
| 147 |
"{% now 'utc' - 'days=2, minutes=33, seconds=1', '%d %b %Y %H:%M:%S' %}" |
| 148 |
|
| 149 |
Further documentation on the underlying functionality can be found in the |
| 150 |
|
| 151 |
+`dateutil relativedelta docs`_. |
| 152 |
|
| 153 |
|
| 154 |
+.. _`dateutil relativedelta docs`: https://dateutil.readthedocs.io/en/latest/relativedelta.html#module-dateutil.relativedelta |
| 155 |
|
| 156 |
|
| 157 |
Issues |
| 158 |
--- a/setup.py |
| 159 |
+++ b/setup.py |
| 160 |
@@ -31,8 +31,9 @@ |
| 161 |
zip_safe=False, |
| 162 |
install_requires=[ |
| 163 |
'jinja2', |
| 164 |
|
| 165 |
+ 'python-dateutil>=2.3' |
| 166 |
], |
| 167 |
+ python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*", |
| 168 |
classifiers=[ |
| 169 |
'Development Status :: 3 - Alpha', |
| 170 |
'Intended Audience :: Developers', |
| 171 |
@@ -45,6 +46,7 @@ |
| 172 |
'Programming Language :: Python :: 3.3', |
| 173 |
'Programming Language :: Python :: 3.4', |
| 174 |
'Programming Language :: Python :: 3.5', |
| 175 |
+ 'Programming Language :: Python :: 3.6', |
| 176 |
'Programming Language :: Python :: Implementation :: CPython', |
| 177 |
'Programming Language :: Python :: Implementation :: PyPy', |
| 178 |
'Programming Language :: Python', |