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 specifying

  • leading_zeroes: set to True to keep the leading zeroes
  • trailing_zeroes: set to True to keep the trailing zeroes
  • infix_zeroes: set to True to 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
>>> delta = timedelta(hours=3, minutes=20)
>>> duration_string(delta)
'3h20m'

int is interpreted as number of seconds

>>> duration_string(200) # When an int is given, it is interpreted as a number of seconds
'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(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.9.0.tar.gz (4.0 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.9.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for nice_duration-0.9.0.tar.gz
Algorithm Hash digest
SHA256 0f634a638365aab54d84e7eebb84dc930240ab304296d8d4f97d4f36608467cc
MD5 1ddcad3c37f0a8130d202d7ee6d1d43a
BLAKE2b-256 b18933335debbe5bc32fc6a087187f36b2882ae21e01a4ac063ae88b4f401d34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nice_duration-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 15667e32b7327632ce86c92e932db2f8e0b9a8b9e58649f42f62d6543f002184
MD5 89a844b6b87a14884d407a7dd3fc2a66
BLAKE2b-256 a8d2c42b4275f7aae8bb7ee7be5290e9c2d127a01f4b4aafa36adc1c2907db1d

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