Skip to main content

Nice formatting of timedelta instances

Project description

nice_duration

A Python library for formatting durations in a human-readable way. Convert timedelta objects or numeric time values into readable strings like 3h20m or 2w5d. This module features optional micro-second precision, configurable separators and flexible handling of zero-values.

Quick Start

from datetime import timedelta
from nice_duration import duration_string

# From timedelta objects
duration_string(timedelta=timedelta(hours=3, minutes=20))  # "3h20m"

# From seconds (int or float)
duration_string(seconds=200)    # "3m20s"
duration_string(seconds=200.9)  # "3h20m" (truncated)

# From milliseconds or microseconds
duration_string(milliseconds=1500)  # "1s" (with default precision)
duration_string(microseconds=1500000)  # "1s"

Precision Control

Control the smallest unit displayed. The precision parameter can be one of weeks, days, hours, minutes, seconds, milliseconds or microseconds.

delta = timedelta(weeks=2, days=3, hours=4, minutes=5, seconds=30, microseconds=500000)

duration_string(timedelta=delta, precision="weeks")        # "2w"
duration_string(timedelta=delta, precision="minutes")      # "2w3d4h5m"
duration_string(timedelta=delta, precision="seconds")      # "2w3d4h5m30s"
duration_string(timedelta=delta, precision="milliseconds") # "2w3d4h5m30s500ms"

Flexible Zero Handling

By default, zero values are omitted, but zeroes can be left alone when leading, trailing or infix.

delta = timedelta(days=3, minutes=12)  # Note: no hours

duration_string(timedelta=delta)                          # "3d12m"
duration_string(timedelta=delta, infix_zeroes=True)       # "3d0h12m"
duration_string(timedelta=delta, leading_zeroes=True)     # "0w3d12m"
duration_string(timedelta=delta, trailing_zeroes=True)    # "3d12m0s"
duration_string(timedelta=delta, all_zeroes=True)         # "0w3d0h12m0s"

Custom Separators

Add spacing or custom separators between units:

delta = timedelta(hours=3, minutes=20)

duration_string(timedelta=delta, separator=" ")     # "3h 20m"

Limitations

  • Maximum unit is weeks (because bigger units have no fixed length)
  • Float values are truncated

License

This project is licensed under the MIT License.

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

nice_duration-1.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nice_duration-1.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file nice_duration-1.1.0.tar.gz.

File metadata

  • Download URL: nice_duration-1.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for nice_duration-1.1.0.tar.gz
Algorithm Hash digest
SHA256 fb941e875ea305a6dea7a6c7c009e00c0db40788826068a14af0f364e3ea9cda
MD5 5026d6a703b2c845f43e1a923f130c10
BLAKE2b-256 a62e14dc9eb269b2061fb920fe8f1011f35a1a68a4b03847be511a2b70f6c2a1

See more details on using hashes here.

File details

Details for the file nice_duration-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: nice_duration-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for nice_duration-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a9b8ef8d64e9fc91bf8885bb79ef7ad08d651164d996448aa0b7c0d9037b9246
MD5 72b78f8f0d9fcc0710a0c536d6850bfd
BLAKE2b-256 598ddd9aa18bd7c72e7f868a2b726752992e5ee22c1f4f65877a8b61ca114afe

See more details on using hashes here.

Supported by

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