Skip to main content

Parse and format human-readable duration strings like "2h30m" or "1 day, 3 hours"

Project description

philiprehberger-duration

Parse and format human-readable duration strings like "2h30m" or "1 day, 3 hours".

Install

pip install philiprehberger-duration

Usage

Parsing duration strings

from philiprehberger_duration import parse

parse("2h30m")              # 9000.0
parse("1 day, 3 hours")    # 97200.0
parse("500ms")             # 0.5
parse("1w 2d")             # 777600.0
parse("2.5h")              # 9000.0
parse("150m")              # 9000.0

Formatting seconds

from philiprehberger_duration import format

format(9000)                        # "2h 30m"
format(9000, style="long")         # "2 hours, 30 minutes"
format(9000, style="colon")        # "2:30:00"
format(9000, style="iso")          # "PT2H30M"
format(90061.5, style="short")    # "1d 1h 1m 1s 500ms"

Duration object

from philiprehberger_duration import Duration

d = Duration.from_seconds(9000)
d.hours    # 2
d.minutes  # 30

# Arithmetic
d2 = d + Duration(minutes=15)
d3 = d * 2

# Convert to timedelta
td = d.to_timedelta()

# String representation (short format)
str(d)  # "2h 30m"

Timedelta conversion

from philiprehberger_duration import Duration

d = Duration(hours=1, minutes=30, milliseconds=500)
td = d.to_timedelta()  # datetime.timedelta(seconds=5400, microseconds=500000)

API Reference

Function / Class Description
parse(s: str) -> float Parse a human-readable duration string to seconds.
format(seconds: float, *, style: str = "short") -> str Format seconds to a human-readable string. Styles: "short", "long", "colon", "iso".
Duration Dataclass with fields: weeks, days, hours, minutes, seconds, milliseconds.
Duration.total_seconds() -> float Return total duration in seconds.
Duration.to_timedelta() -> datetime.timedelta Convert to a timedelta object.
Duration.from_seconds(s: float) -> Duration Create a Duration from seconds.

Supported parse units

Unit Aliases
Weeks w, week, weeks
Days d, day, days
Hours h, hr, hrs, hour, hours
Minutes m, min, mins, minute, minutes
Seconds s, sec, secs, second, seconds
Milliseconds ms, millisecond, milliseconds

License

MIT

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

philiprehberger_duration-0.1.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_duration-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_duration-0.1.1.tar.gz.

File metadata

  • Download URL: philiprehberger_duration-0.1.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for philiprehberger_duration-0.1.1.tar.gz
Algorithm Hash digest
SHA256 513a8560a960b449dcf1677414ce33e3a0f61864a6a1e777a8d978befae1552e
MD5 7db410b0b5f85a35c60bff51a6586adb
BLAKE2b-256 3d158c4b5aca12640bd052f969f247a51c1e31da3878d66f60ac3d20fb5d5a25

See more details on using hashes here.

File details

Details for the file philiprehberger_duration-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_duration-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 587472e87c83ec081a3ea18e9bc9335fe9ac5faf4e754dc78e7dccf931b0bd65
MD5 fbd678def090cd71ee0316227e503601
BLAKE2b-256 9f92fb13fe5feed0e3563e09fd7018a2e71ffb13bfa8db216f1af06c1303361b

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