Description: Remove dependency on arrow Origin: https://github.com/hackebrot/jinja2-time/pull/5 Author: Paul Ganssle Date: 2019-11-04 --- a/jinja2_time/jinja2_time.py +++ b/jinja2_time/jinja2_time.py @@ -1,6 +1,10 @@ # -*- coding: utf-8 -*- -import arrow +import re +from datetime import datetime + +from dateutil import tz +from dateutil.relativedelta import relativedelta from jinja2 import nodes from jinja2.ext import Extension @@ -16,14 +20,23 @@ environment.extend(datetime_format='%Y-%m-%d') def _datetime(self, timezone, operator, offset, datetime_format): - d = arrow.now(timezone) + tzi = _gettz(timezone) + d = datetime.now(tzi) # Parse replace kwargs from offset and include operator - replace_params = {} + rd_params = {} for param in offset.split(','): interval, value = param.split('=') - replace_params[interval.strip()] = float(operator + value.strip()) - d = d.replace(**replace_params) + rd_params[interval.strip()] = float(value.strip()) + + rd = relativedelta(**rd_params) + + if operator == '-': + d -= rd + elif operator == '+': + d += rd + else: + raise ValueError('Unknown operator: %s' % operator) if datetime_format is None: datetime_format = self.environment.datetime_format @@ -32,7 +45,10 @@ def _now(self, timezone, datetime_format): if datetime_format is None: datetime_format = self.environment.datetime_format - return arrow.now(timezone).strftime(datetime_format) + + tzi = _gettz(timezone) + return datetime.now(tzi).strftime(datetime_format) + def parse(self, parser): lineno = next(parser.stream).lineno @@ -63,3 +79,47 @@ lineno=lineno, ) return nodes.Output([call_method], lineno=lineno) + + +_TZOFFSET_MATCH = re.compile(r'(?P[+-])?(?P\d{2}):?(?P\d{2})?$', + flags=re.ASCII) + + +def _parse_isotz(tzstr): + m = _TZOFFSET_MATCH.match(tzstr) + if m: + sign = -1 if m.group('sgn') == '-' else 1 + + hh = int(m.group('hh')) + mm_g = mm.group('mm') + mm = int(mm_g) if mm_g else 0 + + total_seconds = sign * (hh * 3600 + (mm * 60)) + return tz.tzoffset(None, total_seconds) + + return None + + +def _gettz(tzstr): + """ Parse a time zone string into a time zone """ + + tzstr_l = tzstr.lower() + if not tzstr or tzstr_l == 'utc': + return tz.tzutc() + + if tzstr_l == 'local': + return tz.tzlocal() + + tzi = tz.gettz(tzstr) + + if tzi is None: + tzi = _parse_isotz(tzstr) + + if tzi is None: + raise TzParseError('Unknown time zone %s' % tzstr) + + return tzi + + +class TzParseError(ValueError): + pass --- a/README.rst +++ b/README.rst @@ -29,11 +29,10 @@ $ pip install jinja2-time -It will automatically install `jinja2`_ along with `arrow`_. +It will automatically install `jinja2`_ and its dependencies. .. _`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 @@ -42,14 +41,19 @@ Now Tag ~~~~~~~ -The extension comes with a ``now`` tag that provides convenient access to the -`arrow.now()`_ API from your templates. +The extension comes with a ``now`` tag that retrieves the current datetime +in a given time zone from your templates. Time zone specifiers supported are: + +- ``utc`` +- ``local`` +- ``[±]HH[:][MM]`` +- Formats supported by `dateutil.tz.gettz`_ (IANA zones, GNU tz strings, etc) 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 +.. _`dateutil.tz.gettz`: https://dateutil.readthedocs.io/en/latest/tz.html#dateutil.tz.gettz .. code-block:: python @@ -111,9 +115,9 @@ "{% 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`_. +`dateutil relativedelta docs`_. -.. _`arrow replace docs`: http://arrow.readthedocs.io/en/latest/#replace-shift +.. _`dateutil relativedelta docs`: https://dateutil.readthedocs.io/en/latest/relativedelta.html#module-dateutil.relativedelta Issues --- a/setup.py +++ b/setup.py @@ -31,8 +31,9 @@ zip_safe=False, install_requires=[ 'jinja2', - 'arrow' + 'python-dateutil>=2.3' ], + python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*", classifiers=[ 'Development Status :: 3 - Alpha', 'Intended Audience :: Developers', @@ -45,6 +46,7 @@ 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Programming Language :: Python',