Skip to main content

Supplemental ISO8601 duration format support for datetime.timedelta

Project description

The timedelta-isoformat library provides supplemental ISO 8601 duration support to the datetime.timedelta class.

The library is pure-Python, and does not depend upon regular expressions.

Functionality is provided in a subclass of datetime.timedelta that implements additional isoformat() and fromisoformat(duration_string) methods.

Usage

>>> from timedelta_isoformat import timedelta
>>> from datetime import datetime
>>>
>>> first = datetime(year=2022, month=10, day=2)
>>> second = datetime(year=2022, month=11, day=27, hour=14)
>>>
>>> td = timedelta(seconds=(second - first).total_seconds())
>>> td.isoformat()
'PT1358H'
>>>
>>> first + timedelta.fromisoformat('PT1358H')
datetime.datetime(2022, 11, 27, 14, 0)

Design decisions

A variety of ISO 8601 duration parsers exist across a range of programming languages, and many of them have made slightly different design decisions.

Some of the significant design decisions made within this library are:

  • Values in parsed duration strings must be zero-or-greater (PT1H is considered valid; P-2D is not)

  • Empty time segments at the end of duration strings are allowed (P1DT is considered valid)

  • Measurement limits are checked within date/time segments (PT20:59:01 is within limits; PT20:60:01 is not)

  • Measurement values are parsed into floating-point values (at the time of writing, precise procedural algorithms to parse base-ten strings into integers for large inputs are not practical – or not widely known)

  • When inputs are reliably known to be of correct type and format, assertions should be safe to remove (for example, by including the -O command-line flag when invoking the Python interpreter) to improve runtime 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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

timedelta_isoformat-0.6.2.12-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file timedelta_isoformat-0.6.2.12-py3-none-any.whl.

File metadata

File hashes

Hashes for timedelta_isoformat-0.6.2.12-py3-none-any.whl
Algorithm Hash digest
SHA256 99c25420af61382b492d131bc31f6ff242a329c4a4738e2f2034473b87456ad0
MD5 8d449daa03c416571e3c7eca6484bf98
BLAKE2b-256 7b8d71bb448f48eda5c9895f4daac3ddafcd364eb93a2c1c94dccafab9e7fdc2

See more details on using hashes here.

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