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'

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 converted to int and interpreted as 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.8.3.tar.gz (3.9 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.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for nice_duration-0.8.3.tar.gz
Algorithm Hash digest
SHA256 614aee65077e5834f6e1b8bed2ce7b3eabede9ff5f6874cce10b611ddfc996c9
MD5 ca76aeae8355da565dcad3d1dc262568
BLAKE2b-256 fcb53812bba9a41def07402fcf8996b59b909cb27d9350003cc49c42d022b1f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nice_duration-0.8.3-py3-none-any.whl
Algorithm Hash digest
SHA256 409bc3f848d2e542ce935433725863571b3092efa67ae51e183a0972152ccf55
MD5 2fe53c44a8c7b81af6979d04e6aaf72f
BLAKE2b-256 7d20720727e79ceb41762f7ca6a633b9447635b92338a03e8e797eaff5b56908

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