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

  • keep_leading_zeroes
  • keep_trailing_zeroes
  • keep_infix_zeroes

For example, by setting keep_trailing_zeroes to True, the example duration_string is changed to 3h12m0s.

For convenience, setting keep_zeroes to True will set all three keep_*_zeroes 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 keep_zeroes:

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

Setting keep_trailing_zeroes:

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

Setting keep_leading_zeroes:

>>> duration_string(delta, keep_leading_zeroes=True)
'0w0d3h20m'

Setting keep_infix_zeroes:

>>> duration_string(timedelta(hours=3, seconds=11), keep_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.7.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.7.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nice_duration-0.7.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.7.0.tar.gz
Algorithm Hash digest
SHA256 e665beac964051d9bec7d1767e27f7c025a88172b71ef1bc14b36be72a35ef53
MD5 cb3dd1e2425de8092642282840cc51f7
BLAKE2b-256 c56f6f862e59bad4dbe400bdd7552a09556cbbfdc069575bdc0bd82abab24bc8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nice_duration-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 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.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 74620f80f33d40fbc5184f2700b5f1924ee1a9120caca9d489713b55f4d99b99
MD5 0049e7c6c978d96b20ddabed2f5ee393
BLAKE2b-256 e89879a51c0f97147924523b5a22daa5bf65e2e78e13e789e8d3b99b7d56ab72

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