Skip to main content

Convert seconds to units of time.

Project description

Strfseconds is a simple function that convert seconds to units of time. Units of time are supplied in the formatstring parameter, they range from week to microsecond. All but the smallest time units are floored. The smallest time unit has decimals the size of the ndecimals parameter. Functionality looks like timeit.default_timer but this function allows for choosing which time unit to be displayed and in what format.

Function parameters are:

  • :param float or int seconds: The seconds to format
  • :param str formatstring: The formatstring containing the format specifiers. Time units are:
    • %w for weeks
    • %d for days
    • %h for hours
    • %m for minutes
    • %s for seconds
    • %l for milliseconds (0.001 second)
    • %f for microseconds (0.000001 second)
    • %o for the unchanged seconds value
  • :param int ndecimal: The number of decimals applied to the smallest unit
  • :return: str: The formatted formatstring with all format specifiers replaced
  • :rtype: str

Quick start

from strfseconds import strfseconds
>>> print( strfseconds(seconds=1948))
00:32:28.000

Display same number of seconds but only in minutes

>>> print( strfseconds(seconds=1948, formatstring='%m', ndecimal=3))
32.466

Or in hours

>>> print( strfseconds(seconds=1948, formatstring='%h', ndecimal=3))
0.541

Only the smallest time unit gets decimals, see example below with both days an hours

>>> print( strfseconds(seconds=1948, formatstring='%d days, %h hours'))
0 days, 0.541 hours
>>> 

Format masks

Units can be given a format mask of zeros before the decimal sign by up to 9. Add a number between 1 and 9 after the format specifier, '%s3' will have format mask '000'

>>> print(strfseconds(seconds=5, formatstring='%s3', ndecimal=0))
005

Rounding

In order to prevent time units from incrementing this module does not round. All decimal values are truncated after the decimal sign, '0.99' with 'ndecimal' '0' will return '0'

>>> print(strfseconds(seconds=0.999, formatstring='%s3', ndecimal=0))
000
>>> print(strfseconds(seconds=0.999, formatstring='%s3', ndecimal=2))
000.99





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

strfseconds-0.0.0b1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strfseconds-0.0.0b1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file strfseconds-0.0.0b1.tar.gz.

File metadata

  • Download URL: strfseconds-0.0.0b1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for strfseconds-0.0.0b1.tar.gz
Algorithm Hash digest
SHA256 09b5e1c4379f4997a751a6ac40f9deacc076afeb47c7cac941591e15123c840b
MD5 050f1b5d89536964f115ac388d8c3d34
BLAKE2b-256 9743eb4cd61a0c85dbad303d72fa8a57aef32beb3f7b72bbd69f25d74d07e7a3

See more details on using hashes here.

File details

Details for the file strfseconds-0.0.0b1-py3-none-any.whl.

File metadata

  • Download URL: strfseconds-0.0.0b1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for strfseconds-0.0.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 ee32ef8a3c81642df7607c7a7f8a334bf036c1935f111357a17f1cbba2c99559
MD5 66528084a6672a3a7d83807f06a56378
BLAKE2b-256 4e5bee76c17e4b93e1d7df4e24ff742004ce7ac0a51fbbc8298d2907d0238039

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