Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switched to use Pendulum's C timezone implementation #58

Merged
merged 4 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
# Unreleased

* Added Python 3.9 support
* Switched to using a C implementation of `timezone` objects.
* Much faster parse times for timestamps with timezone information
* ~2.5x faster on Python 2.7, ~10% faster on Python 3.9
* Thanks to [`pendulum`](https://github.com/sdispater/pendulum) and @sdispater for the code.
* Python 2.7 users no longer need to install `pytz` dependency :smiley:

# 2.x.x

Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include LICENSE
include README.rst
include CHANGELOG.md
include timezone.h
75 changes: 26 additions & 49 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Parsing a timestamp with no time zone information (ex. ``2014-01-09T21:48:00``):

.. <include:benchmark_with_no_time_zone.rst>

.. table::
.. table::

+---------------+----------+----------+----------+----------+----------+-------------------------------+-----------------------------------------------+
| Module |Python 3.8|Python 3.7|Python 3.6|Python 3.5|Python 3.4| Python 2.7 |Relative Slowdown (versus ciso8601, Python 3.8)|
Expand Down Expand Up @@ -118,7 +118,7 @@ Parsing a timestamp with time zone information (ex. ``2014-01-09T21:48:00-05:30`

.. <include:benchmark_with_time_zone.rst>

.. table::
.. table::

+---------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+----------+-------------------------------+-----------------------------------------------+
| Module | Python 3.8 | Python 3.7 | Python 3.6 | Python 3.5 |Python 3.4| Python 2.7 |Relative Slowdown (versus ciso8601, Python 3.8)|
Expand Down Expand Up @@ -185,29 +185,6 @@ For full benchmarking details (or to run the benchmark yourself), see `benchmark

.. _`benchmarking/README.rst`: https://github.com/closeio/ciso8601/blob/master/benchmarking/README.rst

Dependency on pytz (Python 2)
-----------------------------

In Python 2, ``ciso8601`` uses the `pytz`_ library while parsing timestamps with time zone information. This means that if you wish to parse such timestamps, you must first install ``pytz``:

.. _pytz: http://pytz.sourceforge.net/

.. code:: python

pip install pytz

Otherwise, ``ciso8601`` will raise an exception when you try to parse a timestamp with time zone information:

.. code:: python

In [2]: ciso8601.parse_datetime('2014-12-05T12:30:45.123456-05:30')
Out[2]: ImportError: Cannot parse a timestamp with time zone information without the pytz dependency. Install it with `pip install pytz`.

``pytz`` is intentionally not an explicit dependency of ``ciso8601``. This is because many users use ``ciso8601`` to parse only naive timestamps, and therefore don't need this extra dependency.
In Python 3, ``ciso8601`` makes use of the built-in `datetime.timezone`_ class instead, so ``pytz`` is not necessary.

.. _datetime.timezone: https://docs.python.org/3/library/datetime.html#timezone-objects

Supported Subset of ISO 8601
----------------------------

Expand All @@ -227,11 +204,11 @@ The following date formats are supported:
``YYYY-MM-DD`` ``2018-04-29`` ✅
``YYYY-MM`` ``2018-04`` ✅
``YYYYMMDD`` ``2018-04`` ✅
``--MM-DD`` (omitted year) ``--04-29`` ❌
``--MM-DD`` (omitted year) ``--04-29`` ❌
``--MMDD`` (omitted year) ``--0429`` ❌
``±YYYYY-MM`` (>4 digit year) ``+10000-04`` ❌
``+YYYY-MM`` (leading +) ``+2018-04`` ❌
``-YYYY-MM`` (negative -) ``-2018-04`` ❌
``±YYYYY-MM`` (>4 digit year) ``+10000-04`` ❌
``+YYYY-MM`` (leading +) ``+2018-04`` ❌
``-YYYY-MM`` (negative -) ``-2018-04`` ❌
============================= ============== ==================

Week dates or ordinal dates are not currently supported.
Expand All @@ -247,7 +224,7 @@ Week dates or ordinal dates are not currently supported.
``YYYY-Www-D`` (week date) ``2009-W01-1`` ❌
``YYYYWwwD`` (week date) ``2009-W01-1`` ❌
``YYYY-DDD`` (ordinal date) ``1981-095`` ❌
``YYYYDDD`` (ordinal date) ``1981095`` ❌
``YYYYDDD`` (ordinal date) ``1981095`` ❌
============================= ============== ==================

Time Formats
Expand All @@ -264,22 +241,22 @@ The following time formats are supported:
.. table::
:widths: auto

=================================== =================== ==============
Format Example Supported
=================================== =================== ==============
``hh`` ``11`` ✅
``hhmm`` ``1130`` ✅
``hh:mm`` ``11:30`` ✅
``hhmmss`` ``113059`` ✅
``hh:mm:ss`` ``11:30:59`` ✅
``hhmmss.ssssss`` ``113059.123456`` ✅
``hh:mm:ss.ssssss`` ``11:30:59.123456`` ✅
``hhmmss,ssssss`` ``113059,123456`` ✅
``hh:mm:ss,ssssss`` ``11:30:59,123456`` ✅
Midnight (special case) ``24:00:00`` ✅
``hh.hhh`` (fractional hours) ``11.5`` ❌
``hh:mm.mmm`` (fractional minutes) ``11:30.5`` ❌
=================================== =================== ==============
=================================== =================== ==============
Format Example Supported
=================================== =================== ==============
``hh`` ``11`` ✅
``hhmm`` ``1130`` ✅
``hh:mm`` ``11:30`` ✅
``hhmmss`` ``113059`` ✅
``hh:mm:ss`` ``11:30:59`` ✅
``hhmmss.ssssss`` ``113059.123456`` ✅
``hh:mm:ss.ssssss`` ``11:30:59.123456`` ✅
``hhmmss,ssssss`` ``113059,123456`` ✅
``hh:mm:ss,ssssss`` ``11:30:59,123456`` ✅
Midnight (special case) ``24:00:00`` ✅
``hh.hhh`` (fractional hours) ``11.5`` ❌
``hh:mm.mmm`` (fractional minutes) ``11:30.5`` ❌
=================================== =================== ==============

**Note:** Python datetime objects only have microsecond precision (6 digits). Any additional precision will be truncated.

Expand All @@ -291,9 +268,9 @@ Time zone information may be provided in one of the following formats:
.. table::
:widths: auto

========== ========== ===========
Format Example Supported
========== ========== ===========
========== ========== ===========
Format Example Supported
========== ========== ===========
``Z`` ``Z`` ✅
``z`` ``z`` ✅
``±hh`` ``+11`` ✅
Expand Down
94 changes: 46 additions & 48 deletions module.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
#include <Python.h>
#include <ctype.h>
#include <datetime.h>
#include "timezone.h"

#define STRINGIZE(x) #x
#define EXPAND_AND_STRINGIZE(x) STRINGIZE(x)

#define PY_VERSION_AT_LEAST_32 \
((PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || PY_MAJOR_VERSION > 3)
#define PY_VERSION_AT_LEAST_33 \
((PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 3) || PY_MAJOR_VERSION > 3)
#define PY_VERSION_AT_LEAST_36 \
((PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 6) || PY_MAJOR_VERSION > 3)
#define PY_VERSION_AT_LEAST_37 \
movermeyer marked this conversation as resolved.
Show resolved Hide resolved
((PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 7) || PY_MAJOR_VERSION > 3)

#if !PY_VERSION_AT_LEAST_37
static PyObject *fixed_offset;
// PyPy compatibility for cPython 3.7's Timezone API was added to PyPy 7.3.6
// https://foss.heptapod.net/pypy/pypy/-/merge_requests/826
#ifdef PYPY_VERSION
#define SUPPORTS_37_TIMEZONE_API \
(PYPY_VERSION_NUM >= 0x07030600)
#else
#define SUPPORTS_37_TIMEZONE_API \
PY_VERSION_AT_LEAST_37
#endif

static PyObject *utc;
Expand Down Expand Up @@ -427,32 +432,34 @@ _parse(PyObject *self, PyObject *args, int parse_any_tzinfo, int rfc3339_only)
tzminute += 60 * tzhour;
tzminute *= tzsign;

#if !PY_VERSION_AT_LEAST_32
if (fixed_offset == NULL || utc == NULL) {
PyErr_SetString(PyExc_ImportError,
"Cannot parse a timestamp with time zone "
"information without the pytz dependency. "
"Install it with `pip install pytz`.");
return NULL;
}
#endif

if (tzminute == 0) {
tzinfo = utc;
}
else {
#if PY_VERSION_AT_LEAST_37
delta = PyDelta_FromDSU(0, 60 * tzminute, 0);
tzinfo = PyTimeZone_FromOffset(delta);
else if (abs(tzminute) >= 1440) {
/* Format the error message as if we were still using pytz
* for Python 2 and datetime.timezone for Python 3.
* This is done to maintain complete backwards
* compatibility with ciso8601 2.0.x. Perhaps change to a
* simpler message in ciso8601 v3.0.0.
*/
#if PY_MAJOR_VERSION >= 3
delta = PyDelta_FromDSU(0, tzminute * 60, 0);
PyErr_Format(PyExc_ValueError,
"offset must be a timedelta"
" strictly between -timedelta(hours=24) and"
" timedelta(hours=24),"
" not %R.",
delta);
Py_DECREF(delta);
#elif PY_VERSION_AT_LEAST_32
tzinfo = PyObject_CallFunction(
fixed_offset, "N",
PyDelta_FromDSU(0, 60 * tzminute, 0));
#else
tzinfo =
PyObject_CallFunction(fixed_offset, "i", tzminute);
PyErr_Format(PyExc_ValueError,
"('absolute offset is too large', %d)",
tzminute);
#endif
return NULL;
}
else {
tzinfo = new_fixed_offset(60 * tzminute);
movermeyer marked this conversation as resolved.
Show resolved Hide resolved
if (tzinfo == NULL) /* ie. PyErr_Occurred() */
return NULL;
}
Expand All @@ -473,6 +480,8 @@ _parse(PyObject *self, PyObject *args, int parse_any_tzinfo, int rfc3339_only)
/* Make sure that there is no more to parse. */
if (*c != '\0') {
PyErr_Format(PyExc_ValueError, "unconverted data remains: '%s'", c);
if (tzinfo != Py_None && tzinfo != utc)
Py_DECREF(tzinfo);
return NULL;
}

Expand Down Expand Up @@ -542,12 +551,6 @@ PyInit_ciso8601(void)
initciso8601(void)
#endif
{
#if !PY_VERSION_AT_LEAST_32
PyObject *pytz;
#elif !PY_VERSION_AT_LEAST_37
PyObject *datetime;
#endif

#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
Expand All @@ -558,28 +561,23 @@ initciso8601(void)
EXPAND_AND_STRINGIZE(CISO8601_VERSION));

PyDateTime_IMPORT;
#if PY_VERSION_AT_LEAST_37
utc = PyDateTime_TimeZone_UTC;
#elif PY_VERSION_AT_LEAST_32
datetime = PyImport_ImportModule("datetime");
if (datetime == NULL)
return NULL;
fixed_offset = PyObject_GetAttrString(datetime, "timezone");
if (fixed_offset == NULL)
return NULL;
utc = PyObject_GetAttrString(fixed_offset, "utc");
if (utc == NULL)

// PyMODINIT_FUNC is void in Python 2, returns PyObject* in Python 3
if (initialize_timezone_code(module) < 0) {
#if PY_MAJOR_VERSION >= 3
return NULL;
#else
pytz = PyImport_ImportModule("pytz");
if (pytz == NULL) {
PyErr_Clear();
}
else {
fixed_offset = PyObject_GetAttrString(pytz, "FixedOffset");
utc = PyObject_GetAttrString(pytz, "UTC");
return;
#endif
}

#if SUPPORTS_37_TIMEZONE_API
utc = PyDateTime_TimeZone_UTC;
#else
utc = new_fixed_offset(0);
movermeyer marked this conversation as resolved.
Show resolved Hide resolved
#endif

// PyMODINIT_FUNC is void in Python 2, returns PyObject* in Python 3
#if PY_MAJOR_VERSION >= 3
return module;
#endif
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
ext_modules=[
Extension(
"ciso8601",
sources=["module.c"],
sources=["module.c", "timezone.c"],
define_macros=[("CISO8601_VERSION", VERSION)],
)
],
Expand Down
Empty file added tests/__init__.py
Empty file.
68 changes: 68 additions & 0 deletions tests/test_timezone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-

import sys

from datetime import datetime, timedelta

from ciso8601 import FixedOffset

if sys.version_info.major == 2:
# We use unittest2 since it has a backport of the `unittest.TestCase.assertRaisesRegex` method,
# which is called `assertRaisesRegexp` in Python 2. This saves us the hassle of monkey-patching
# the class ourselves.
import unittest2 as unittest
else:
import unittest


class TimezoneTestCase(unittest.TestCase):
def test_utcoffset(self):
if sys.version_info >= (3, 2):
from datetime import timezone
for minutes in range(-1439, 1440):
td = timedelta(minutes=minutes)
tz = timezone(td)
built_in_dt = datetime(2014, 2, 3, 10, 35, 27, 234567, tzinfo=tz)
our_dt = datetime(2014, 2, 3, 10, 35, 27, 234567, tzinfo=FixedOffset(minutes * 60))
self.assertEqual(built_in_dt.utcoffset(), our_dt.utcoffset(), "`utcoffset` output did not match for offset: {minutes}".format(minutes=minutes))
else:
self.assertEqual(FixedOffset(0).utcoffset(), timedelta(minutes=0))
self.assertEqual(FixedOffset(+0).utcoffset(), timedelta(minutes=0))
self.assertEqual(FixedOffset(-0).utcoffset(), timedelta(minutes=0))
self.assertEqual(FixedOffset(-4980).utcoffset(), timedelta(hours=-1, minutes=-23))
self.assertEqual(FixedOffset(+45240).utcoffset(), timedelta(hours=12, minutes=34))

def test_dst(self):
if sys.version_info >= (3, 2):
from datetime import timezone
for minutes in range(-1439, 1440):
td = timedelta(minutes=minutes)
tz = timezone(td)
built_in_dt = datetime(2014, 2, 3, 10, 35, 27, 234567, tzinfo=tz)
our_dt = datetime(2014, 2, 3, 10, 35, 27, 234567, tzinfo=FixedOffset(minutes * 60))
self.assertEqual(built_in_dt.dst(), our_dt.dst(), "`dst` output did not match for offset: {minutes}".format(minutes=minutes))
else:
self.assertIsNone(FixedOffset(0).dst(), "UTC")
self.assertIsNone(FixedOffset(+0).dst(), "UTC")
self.assertIsNone(FixedOffset(-0).dst(), "UTC")
self.assertIsNone(FixedOffset(-4980).dst(), "UTC-01:23")
self.assertIsNone(FixedOffset(+45240).dst(), "UTC+12:34")

def test_tzname(self):
if sys.version_info >= (3, 2):
from datetime import timezone
for minutes in range(-1439, 1440):
td = timedelta(minutes=minutes)
tz = timezone(td)
built_in_dt = datetime(2014, 2, 3, 10, 35, 27, 234567, tzinfo=tz)
our_dt = datetime(2014, 2, 3, 10, 35, 27, 234567, tzinfo=FixedOffset(minutes * 60))
self.assertEqual(built_in_dt.tzname(), our_dt.tzname(), "`tzname` output did not match for offset: {minutes}".format(minutes=minutes))
else:
self.assertEqual(FixedOffset(0).tzname(), "UTC+00:00")
self.assertEqual(FixedOffset(+0).tzname(), "UTC+00:00")
self.assertEqual(FixedOffset(-0).tzname(), "UTC+00:00")
self.assertEqual(FixedOffset(-4980).tzname(), "UTC-01:23")
self.assertEqual(FixedOffset(+45240).tzname(), "UTC+12:34")

if __name__ == '__main__':
unittest.main()
Loading