Skip to main content

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 setting

  • leading_zeroes
  • trailing_zeroes
  • infix_zeroes

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.

Examples

>>> from datetime import timedelta
>>> from nice_duration import duration_string
>>> delta = timedelta(hours=3, minutes=20)
>>> duration_string(delta)
'3h20m'
>>> duration_string(200) # When an int is given, it is interpreted as a number of seconds
'3h20m'
>>> duration_string(200.3) # When a float is given, it is converted to an int and interpreted as a number of seconds
'3h20m'

Setting separator:

>>> duration_string(delta, separator=" ")
'3h 20m'

Setting all_zeroes:

>>> duration_string(delta, all_zeroes=True)
'0w0d3h20m0s'

Setting trailing_zeroes:

>>> duration_string(delta, trailing_zeroes=True)
'3h20m0s'

Setting leading_zeroes:

>>> duration_string(delta, 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


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-0.8.2.tar.gz (3.8 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-0.8.2-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nice_duration-0.8.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for nice_duration-0.8.2.tar.gz
Algorithm Hash digest
SHA256 1142ad69bd81c3f054f0ed393f7cc1ebfda98aa3c34e56c27e79547868f280c8
MD5 f46a7ffab64107a394a722b653f3a1f4
BLAKE2b-256 2c71f295e40e49d16316e5b4ffe45b810972477b8d52c1122fb5e4ae6a0292cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nice_duration-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aa4539a3ca0d6069a0e762fa74cd8163bf1a497deb9c9ba0f6f50b27246e1286
MD5 e824cd05c551c72bfb379343ce538a34
BLAKE2b-256 ea48d98ea64db426701224bfda392090b253c2cd8299d52f3476abf38879ea9c

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