Nice formatting of timedelta instances
Project description
nice_duration
This is a Python module that introduces a way of formatting a
timedelta instance like so: 3h12m. It can also format an int or
a float representing a number of seconds in the same way. A float
is just converted to an int, so 70.9 is converted to 70.
The separator parameter, which defaults to the empty string,
determines what the individual components are concatenated with.
Setting separator to a single space changes the above example to
3h 12m.
Components with value 0 are left out. That can be changed by specifying
leading_zeroes: set toTrueto keep the leading zeroestrailing_zeroes: set toTrueto keep the trailing zeroesinfix_zeroes: set toTrueto keep the zeroes between values
For example, by setting trailing_zeroes to True, the example duration_string is
changed to 3h12m0s.
For convenience, setting all_zeroes to True will set all three to True.
Limitations
- No sub-second precision
- Maximum unit is weeks (because bigger units have no fixed length)
Examples
>>> from datetime import timedelta
>>> from nice_duration import duration_string
>>> duration_string(timedelta(hours=3, minutes=20))
'3h20m'
int is interpreted as number of seconds
>>> duration_string(200)
'3h20m'
float is truncated to int and interpreted as a whole number of seconds
>>> duration_string(200.3)
'3h20m'
negative value
>>> duration_string(-13)
'-13s'
Setting separator:
>>> duration_string(timedelta(hours=3, minutes=20), separator=" ")
'3h 20m'
Setting all_zeroes:
>>> duration_string(timedelta(hours=3, minutes=20), all_zeroes=True)
'0w0d3h20m0s'
Setting trailing_zeroes:
>>> duration_string(timedelta(hours=3, minutes=20), trailing_zeroes=True)
'3h20m0s'
Setting leading_zeroes:
>>> duration_string(timedelta(hours=3, minutes=20), leading_zeroes=True)
'0w0d3h20m'
Setting infix_zeroes:
>>> duration_string(timedelta(hours=3, seconds=11), infix_zeroes=True)
'3h0m11s'
Return value when timedelta is 0:
>>> duration_string(timedelta())
'0s'
Project details
Release history Release notifications | RSS feed
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 nice_duration-0.9.1.tar.gz.
File metadata
- Download URL: nice_duration-0.9.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b9e7dad11cadb9e01632b0265448d9541f35198a4d2257b89e42ce9e9da4bef
|
|
| MD5 |
e70d1a2069966987850775e57dd539ee
|
|
| BLAKE2b-256 |
cc51293e61570557febe25c439e9d79111e16e2382c8c117d0060302a4ab9132
|
File details
Details for the file nice_duration-0.9.1-py3-none-any.whl.
File metadata
- Download URL: nice_duration-0.9.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b80d17de75fc50331818a57de828b66a2689c6e829e3e12c547a453d6bb5218
|
|
| MD5 |
c127287c640db98ea996fb10976204c6
|
|
| BLAKE2b-256 |
35d3d57baef1fc679b71acef5d4a512e1972499b7bcae84253dd1344323c26e7
|