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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_duration-0.1.3.tar.gz.
File metadata
- Download URL: philiprehberger_duration-0.1.3.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d59b1114316d2216052d89e8e384afdadd3b47d654d75859034a43de952b723
|
|
| MD5 |
4bfa92cd12d694fbbfcc8ca1336cda40
|
|
| BLAKE2b-256 |
67a713260edbc7749450db95c6440d0ea1677b59629d3aef6d80300c82523974
|
File details
Details for the file philiprehberger_duration-0.1.3-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_duration-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3fdaac7fb47efbd3e15090fa5a86878f65fbfeedbf35891e9f07f8508f564f0e
|
|
| MD5 |
d1d4b540c77489123152bbd9e9e2f12d
|
|
| BLAKE2b-256 |
93fddeac91bc7140cd37034f46dff8d001b3db9ac1f73ca4710c6c762492d690
|