Skip to main content

Backport of Python 3.11's datetime.fromisoformat

Project description

https://github.com/movermeyer/backports.datetime_fromisoformat/actions/workflows/test.yml/badge.svg

A backport of Python 3.11’s datetime.fromisoformat methods to earlier versions of Python 3. Tested against Python 3.6, 3.7, 3.8, 3.9, 3.10 and 3.11

Current Status

Development of backports.datetime_fromisoformat is “complete”. Outside of potential minor bug fixes, do not expect new development here.

Version 2 changes

In version 1, backports.datetime_fromisoformat was a backport of the Python 3.7 version of the fromisoformat methods. This meant that it was limited in being able to parse only timestamps that were in the format produced by datetime.isoformat.

As of version 2, backports.datetime_fromisoformat is a backport of the Python 3.11 version of the fromisoformat methods, which can parse (almost) the entire ISO 8601 specification. There are no changes required when upgrading from v1 to v2. The parser is simply able to parse a wider portion of the ISO 8601 specification.

However, starting in version 2, backports.datetime_fromisoformat will apply its changes to Python < 3.11, whereas v1 only applied changes to Python < 3.7. If you happened to be using backports.datetime_fromisoformat v1 on Python 3.7 through Python 3.10 and then upgrade to v2, it will patch the fromisoformat methods, whereas in v1 it did not. The result is that the fromisoformat methods will suddenly be able to parse timestamps from a wider portion of the ISO 8601 specification.

Quick Start

Installation:

pip install backports-datetime-fromisoformat

Usage:

>>> from datetime import date, datetime, time
>>> from backports.datetime_fromisoformat import MonkeyPatch
>>> MonkeyPatch.patch_fromisoformat()

>>> datetime.fromisoformat("2014-01-09T21:48:00-05:30")
datetime.datetime(2014, 1, 9, 21, 48, tzinfo=-05:30)

>>> date.fromisoformat("2014-01-09")
datetime.date(2014, 1, 9)

>>> time.fromisoformat("21:48:00-05:30")
datetime.time(21, 48, tzinfo=-05:30)

Explanation

In Python 3.7, datetime.fromisoformat was added. It is the inverse of datetime.isoformat. Similar methods were added to the date and time types as well.

In Python 3.11, datetime.fromisoformat was extended to cover (almost) all of the ISO 8601 specification, making it generally useful.

For those who need to support earlier versions of Python, a backport of these methods was needed.

backports.datetime_fromisoformat is a C implementation of fromisoformat based on the upstream cPython 3.11 code. For timezone objects, it uses a custom timezone C implementation (originally from Pendulum).

Usage in Python 3.11+

NOTE: in Python 3.11 and later, compatible versions of fromisoformat methods exist in the stdlib, and installing this package has NO EFFECT.

Goal / Project Scope

The purpose of this project is to provide a perfect backport of the fromisoformat methods to earlier versions of Python, while still providing comparable performance.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

backports-datetime-fromisoformat-2.0.1.tar.gz (23.2 kB view hashes)

Uploaded Source

Built Distributions

backports_datetime_fromisoformat-2.0.1-cp310-cp310-win_amd64.whl (28.7 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

backports_datetime_fromisoformat-2.0.1-cp310-cp310-musllinux_1_1_x86_64.whl (58.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

backports_datetime_fromisoformat-2.0.1-cp310-cp310-musllinux_1_1_i686.whl (57.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

backports_datetime_fromisoformat-2.0.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (51.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

backports_datetime_fromisoformat-2.0.1-cp310-cp310-macosx_11_0_arm64.whl (26.7 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

backports_datetime_fromisoformat-2.0.1-cp310-cp310-macosx_10_9_x86_64.whl (26.3 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

backports_datetime_fromisoformat-2.0.1-cp310-cp310-macosx_10_9_universal2.whl (33.8 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

backports_datetime_fromisoformat-2.0.1-cp39-cp39-win_amd64.whl (28.7 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

backports_datetime_fromisoformat-2.0.1-cp39-cp39-musllinux_1_1_x86_64.whl (58.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

backports_datetime_fromisoformat-2.0.1-cp39-cp39-musllinux_1_1_i686.whl (57.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

backports_datetime_fromisoformat-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (51.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

backports_datetime_fromisoformat-2.0.1-cp39-cp39-macosx_11_0_arm64.whl (26.7 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

backports_datetime_fromisoformat-2.0.1-cp39-cp39-macosx_10_9_x86_64.whl (26.3 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

backports_datetime_fromisoformat-2.0.1-cp39-cp39-macosx_10_9_universal2.whl (33.8 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

backports_datetime_fromisoformat-2.0.1-cp38-cp38-win_amd64.whl (28.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

backports_datetime_fromisoformat-2.0.1-cp38-cp38-musllinux_1_1_x86_64.whl (57.6 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

backports_datetime_fromisoformat-2.0.1-cp38-cp38-musllinux_1_1_i686.whl (56.7 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

backports_datetime_fromisoformat-2.0.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (51.5 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

backports_datetime_fromisoformat-2.0.1-cp38-cp38-macosx_11_0_arm64.whl (26.7 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

backports_datetime_fromisoformat-2.0.1-cp38-cp38-macosx_10_9_x86_64.whl (26.3 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

backports_datetime_fromisoformat-2.0.1-cp38-cp38-macosx_10_9_universal2.whl (33.8 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

backports_datetime_fromisoformat-2.0.1-cp37-cp37m-win_amd64.whl (28.7 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

backports_datetime_fromisoformat-2.0.1-cp37-cp37m-musllinux_1_1_x86_64.whl (58.2 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

backports_datetime_fromisoformat-2.0.1-cp37-cp37m-musllinux_1_1_i686.whl (57.2 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

backports_datetime_fromisoformat-2.0.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (50.0 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

backports_datetime_fromisoformat-2.0.1-cp37-cp37m-macosx_10_9_x86_64.whl (26.2 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page