Timer

Timer displays the elapsed time since the bar started.

Reach for it whenever “how long has this taken so far” matters more than an estimate of what remains. Unlike the ETA family, it needs no max_value and works just as well for indeterminate work. format controls the text (default Elapsed Time: %(elapsed)s) and the readout has whole-second resolution.

class progressbar.widgets.Timer(format='Elapsed Time: %(elapsed)s', **kwargs: Any)[source]

Bases: FormatLabel, TimeSensitiveWidgetBase

WidgetBase which displays the elapsed seconds.

Create a Timer, rewriting a legacy bare %s placeholder.

Very old configs used a bare %s placeholder for the elapsed time. It is silently rewritten here to the named %(elapsed)s form this widget actually formats with.

static format_time(timestamp: timedelta | date | datetime | str | int | float | None, precision: timedelta = datetime.timedelta(seconds=1)) str

Formats timedelta/datetime/seconds.

>>> format_time('1')
'0:00:01'
>>> format_time(1.234)
'0:00:01'
>>> format_time(1)
'0:00:01'
>>> format_time(datetime.datetime(2000, 1, 2, 3, 4, 5, 6))
'2000-01-02 03:04:05'
>>> format_time(datetime.date(2000, 1, 2))
'2000-01-02'
>>> format_time(datetime.timedelta(seconds=3661))
'1:01:01'
>>> format_time(None)
'--:--:--'
>>> format_time(format_time)
Traceback (most recent call last):
    ...
TypeError: Unknown type ...

Example

Timer
"""``Timer`` displays the elapsed time since the bar started.

Reach for it whenever "how long has this taken so far" matters more
than an estimate of what remains -- unlike the ``ETA`` family, it needs
no ``max_value`` and works just as well for indeterminate work. Compare
``CurrentTime`` for the wall-clock time of day instead of a duration.

This example runs longer than most in this set: elapsed time is only shown
to whole-second resolution, so a bar that finishes in a few milliseconds
would read "Elapsed Time: 0:00:00" for its entire run -- the one thing this
widget exists to show would never move.
"""

import time

import progressbar

STEPS = 24


def main() -> None:
    widgets = [progressbar.Timer()]
    with progressbar.ProgressBar(max_value=STEPS, widgets=widgets) as bar:
        for step in range(STEPS):
            bar.update(step + 1)
            time.sleep(0.2)


if __name__ == '__main__':
    main()

See also

  • ETA: a remaining-time estimate, which does need a known max_value.

  • CurrentTime: the time of day instead of a duration.