Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

ISO 8601 date/time parser

PyPI version Supported Python versions PyPI downloads GitHub Actions status Codecov Licence

isodate2 is a fork of isodate

This module implements ISO 8601 date, time and duration parsing. The implementation follows ISO8601:2004 standard, and implements only date/time representations mentioned in the standard. If something is not mentioned there, then it is treated as non-existent, and not as an allowed option.

For instance, ISO8601:2004 never mentions 2 digit years. So, it is not intended by this module to support 2 digit years. (while it may still be valid as ISO date, because it is not explicitly forbidden.) Another example is, when no time zone information is given for a time, then it should be interpreted as local time, and not UTC.

As this module maps ISO 8601 dates/times to standard Python data types, like date, time, datetime and timedelta, it is not possible to convert all possible ISO 8601 dates/times. For instance, dates before 0001-01-01 are not allowed by the Python date and datetime classes. Additionally fractional seconds are limited to microseconds. That means if the parser finds for instance nanoseconds it will round it to microseconds.

Documentation

There are five parsing methods available.

  • parse_time: parses an ISO 8601 time string into a time object
  • parse_date: parses an ISO 8601 date string into a date object
  • parse_datetime: parses an ISO 8601 date-time string into a datetime object
  • parse_duration: parses an ISO 8601 duration string into a timedelta or Duration object.
  • parse_tzinfo: parses the time zone info part of an ISO 8601 string into a tzinfo object.

As ISO 8601 allows to define durations in years and months, and timedelta does not handle years and months, this module provides a Duration class, which can be used almost like a timedelta object (with some limitations). However, a Duration object can be converted into a timedelta object.

There are also ISO formatting methods for all supported data types. Each xxx_isoformat method accepts a format parameter. The default format is always the ISO 8601 expanded format. This is the same format used by datetime.isoformat:

  • time_isoformat: Intended to create ISO time strings with default format hh:mm:ssZ.
  • date_isoformat: Intended to create ISO date strings with default format yyyy-mm-dd.
  • datetime_isoformat: Intended to create ISO date-time strings with default format yyyy-mm-ddThh:mm:ssZ.
  • duration_isoformat: Intended to create ISO duration strings with default format PnnYnnMnnDTnnHnnMnnS.
  • tz_isoformat: Intended to create ISO time zone strings with default format hh:mm.
  • strftime: A re-implementation mostly compatible with Python's strftime, but supports only those format strings, which can also be used for dates prior 1900. This method also understands how to format datetime and Duration instances.

Installation

python -m pip install isodate2

Limitations

  • The parser accepts several date/time representation which should be invalid according to ISO 8601 standard.
  1. for date and time together, this parser accepts a mixture of basic and extended format. e.g. the date could be in basic format, while the time is accepted in extended format. It also allows short dates and times in date-time strings.
  2. For incomplete dates, the first day is chosen. e.g. 19th century results in a date of 1901-01-01.
  3. negative Duration and timedelta value are not fully supported yet.

Further information

The doc strings and unit tests should provide rather detailed information about the methods and their limitations.

The source release provides a setup.py script, which can be used to run the unit tests included.

Source code is available at https://github.com/isodate/isodate.

See also

isoduration is a well-maintained library which aims to improve upon isodate:

The state of the art of ISO 8601 duration handling in Python is more or less limited to what's offered by isodate. What we are trying to achieve here is to address the shortcomings of isodate (as described in their own Limitations section), and a few of our own annoyances with their interface, such as the lack of uniformity in their handling of types, and the use of regular expressions for parsing.

Metadata

Release files for isodate2 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for isodate2 0.9.0
File Size Uploaded Provenance
isodate2-0.9.0.tar.gz 31.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for isodate2 0.9.0
File Interpreter ABI Platform
isodate2-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 58.2 kB

Release files / isodate2-0.9.0.tar.gz

Download URL isodate2-0.9.0.tar.gz
Size 31.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5b0aa4b267cf1e6d2a16fbd62528b556cc7a3422ac81cb5464031172ba618dbf
BLAKE2b-256 checksum
How to use checksums
da7c08297948c8b7e56ab0f24100eb23facfc7913fce2ea4ff264308c6f989b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release files / isodate2-0.9.0-py3-none-any.whl

Download URL isodate2-0.9.0-py3-none-any.whl
Size 26.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1fdf6b33b1118c8e5995dd9ea1b48b54a39bfaa0527abc15e5379d31b3aff0fc
BLAKE2b-256 checksum
How to use checksums
726a2184c48b48a2adbad501b7134c82bac39c48760a8589a44971e05ec14450
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Security for isodate2 0.9.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page