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 representing a number of seconds in the same way.

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'

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: nice_duration-0.8.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for nice_duration-0.8.0.tar.gz
Algorithm Hash digest
SHA256 53654ce46de4641c60f2be576c6ccb656c85e8fcd01875ede7bcb1b280ac9ea5
MD5 c83c7a26708813019ba444f0caa28a5a
BLAKE2b-256 b59dbe07d48e7c8f10e9fe0b70f335284c7afdb857f97c81274450ae560f25d1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nice_duration-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for nice_duration-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 311a33b6787a3e7f22575ef7457934641f2599fd54e61419889c6c4f55a66622
MD5 071f19db05e9d1f92d3a560b4b0db094
BLAKE2b-256 f2e5cc4ca6739bb0ef90f5fe5f24bbc2dca6340a190dc600f813b8fa4ece9153

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