Skip to main content

durospan

Typed, zero-dependency round-trip between human durations and seconds, one grammar both ways.

Install

pip install durospan

Usage

import durospan

# Parse human text into seconds (a float) or a timedelta.
durospan.parse("1d 4h30m")  # 102600.0
durospan.parse("2h30m")  # 9000.0
durospan.parse("90s")  # 90.0
durospan.parse("1.5h")  # 5400.0
durospan.parse("28:30:00")  # 102600.0  (clock input is accepted too)
durospan.parse_timedelta("1h 30m")  # datetime.timedelta(seconds=5400)

# Format seconds (or a timedelta) back, in the same grammar.
durospan.format(102600)  # "1d 4h 30m"   (default token style)
durospan.format(102600, style="compact")  # "1d4h30m"
durospan.format(102600, style="clock")  # "28:30:00"
durospan.format(90.5, style="clock")  # "00:01:30.5"
durospan.format_timedelta(td, style="clock")

# The two directions share one grammar, so the trip is lossless:
durospan.parse(durospan.format(seconds)) == seconds

Bad input raises durospan.DurationError (a ValueError): unknown units, repeated units, trailing junk, negatives and non-finite values are all rejected.

Real formats, exactly

durospan also reads and writes the duration formats other tools use, exactly, through integer nanoseconds:

from datetime import date
import durospan

durospan.parse_ns("1h30m", dialect="go")  # 5400000000000
durospan.parse_ns("-2562047h47m16.854775808s", dialect="go")  # -9223372036854775808
durospan.parse_ns("PT1H30M", dialect="iso8601")  # 5400000000000
durospan.parse_ns("2 h 30 min", dialect="systemd")  # 9000000000000
durospan.parse_ns("1w2d", dialect="prometheus")  # 777600000000000
durospan.parse_ns("1 hour, 30 minutes")  # 5400000000000 (human, the default)
durospan.parse_seconds("300ms", dialect="go")  # Decimal('0.300000000')

durospan.format_duration(5_400_000_000_000, dialect="go")  # '1h30m0s'
durospan.convert("2h45m30.5s", source="go", target="iso8601")  # 'PT2H45M30.5S'

# ISO years and months have no fixed length: give a date or get an error, never a guess.
durospan.parse_ns("P1M", dialect="iso8601", reference=date(2024, 1, 31))  # 29 days

durospan.humanize(3725 * 10**9, precision=2)  # '1h 2m'
durospan.humanize(3725 * 10**9, long=True)  # '1 hour, 2 minutes, 5 seconds'

durospan.find_durations("retry in 5 minutes, give up after 2 h 30 min")
# [DurationMatch(start=9, end=18, text='5 minutes', ns=300000000000), ...]

Dialects: human, go (time.ParseDuration and Duration.String, bit for bit), iso8601, systemd (systemd.time(7)) and prometheus (model.ParseDuration). Errors are typed (DurationSyntaxError, DurationRangeError, AmbiguousDurationError) and carry the position of the bad token; explain() draws a caret under it.

Command line

durospan parse 1h30m --dialect go                    # 5400000000000
durospan convert "2 h 30 min" --from systemd --to prometheus   # 2h30m
durospan humanize 5400s --long                       # 1 hour, 30 minutes
durospan find app.log --dialect go

See GUIDE.md for every dialect, the exactness rules and the sources the test vectors come from.

Why

Python makes you choose one direction or a kitchen sink:

  • pytimeparse parses "1h30m" into seconds but cannot format a number back.
  • humanize formats a number into prose but cannot parse it back.
  • humanfriendly does both, but it is a broad grab-bag of terminal helpers whose last release was in 2021 and which is not typed.
  • pydura does both with nanosecond precision, but it requires Python 3.14+.

durospan is the small, focused piece in the middle: one shared unit table drives both the parser and the formatter, so a value survives the round trip, and it is fully typed, py.typed, zero-dependency and runs on Python 3.10+. Three output styles cover the common shapes: token (1d 4h 30m), compact (1d4h30m) and clock (HH:MM:SS), and clock text is accepted on input as well. On top of that it speaks the formats that Go, ISO 8601, systemd and Prometheus emit, exactly, so a value can move between a Go config, a systemd unit and a Prometheus rule without drifting.

License

MIT. See LICENSE.

Metadata

Release files for durospan 0.2.2

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

Source distribution (sdist)

Source distribution for durospan 0.2.2
File Size Uploaded Provenance Actions
durospan-0.2.2.tar.gz 35.1 kB Without provenance Details for durospan-0.2.2.tar.gz

Built distribution (wheel)

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

Total release size: 62.8 kB

Release files / durospan-0.2.2.tar.gz

Download URL durospan-0.2.2.tar.gz
Size 35.1 kB
Tags Source
SHA-256 checksum
How to use checksums
69b22033e48399b27285f8b27716c1029735c72b9ef5ec35be32567db6d63c0b
BLAKE2b-256 checksum
How to use checksums
3c24ae5c8fd3fabbb6a1df5cf73c5c22d697a94f097d45fd551b0e4183febfc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / durospan-0.2.2-py3-none-any.whl

Download URL durospan-0.2.2-py3-none-any.whl
Size 27.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ac32acd145417374bc033f7ec9f8c51228bbfadc7fecc3047575470c3e83a9f
BLAKE2b-256 checksum
How to use checksums
4003c1e466ef7e4cbfa5e75ccb5966462805c3304dd81fe97774d76c50b5640a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for durospan 0.2.2

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files No provenance

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