Skip to main content

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

Project description

philiprehberger-duration

Tests PyPI version Last updated

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

Installation

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

ISO 8601 parsing

from philiprehberger_duration import parse

parse("PT2H30M")           # 9000.0
parse("P1DT12H")           # 129600.0
parse("PT1M30S")           # 90.0
parse("PT0.5S")            # 0.5

Colon format parsing

from philiprehberger_duration import parse

parse("1:30:00")           # 5400.0
parse("1:05")              # 65.0
parse("1:05:30.500")       # 3930.5

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
d4 = d - Duration(minutes=10)
d5 = d / 2
d6 = d // 3

# Comparisons
Duration.from_seconds(60) < Duration.from_seconds(120)   # True
Duration.from_seconds(60) == Duration(minutes=1)          # True
Duration(hours=1) >= Duration(minutes=30)                 # True

# 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".
parse(s: str) -> float Also accepts ISO 8601 ("PT2H30M") and colon format ("1:30:00").
Duration Dataclass with fields: weeks, days, hours, minutes, seconds, milliseconds, microseconds.
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
Microseconds us, μs, microsecond, microseconds

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

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.2.2.tar.gz (7.3 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.2.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_duration-0.2.2.tar.gz
Algorithm Hash digest
SHA256 55ff1ee14a22098954b40a3791b7a38b15a1e2b721227027349a066e66bde54c
MD5 e90a06632456d080a233df6b5e7b7a89
BLAKE2b-256 c966241b3870c0938c44ae8eff34d239968d3db3cc7c47ec4d154677fb9b5213

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_duration-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 53de87c21adea5f23bfa5248fa5d6e57c13bb04e339bd8a5220b9251e7727d25
MD5 8d79ac638d437033dae28c893ba16c67
BLAKE2b-256 a803b410752e1b53871ea9bcafc2739d7c19f9a3be90653e7c23c9abfad0e902

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