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.1.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.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for nice_duration-0.8.1.tar.gz
Algorithm Hash digest
SHA256 c3640c438b399616345c6ab158fca7df648c3fb5669b9f88aaf7de7cb35abf97
MD5 e9bfa5ed62ac29a7c83f14759108d42d
BLAKE2b-256 43fd8d7b4b4d213e05e3c7df94d9353d2a1d25dd904f459c5c1214db08614764

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nice_duration-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4394c66eaf36a79e54c8f27b13f2efc243a23a2f459fe58fd75b80e57834f27f
MD5 3ec318d97322adf5188d94f199382138
BLAKE2b-256 f3dc5bf4e70e093f3be59fca6205e8935d6c2073114c3e70258deb9dbf648ff3

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