CurrentTime¶
CurrentTime displays the current date and time, updated live.
Reach for it to stamp log-like output with a wall clock, independent of
the bar’s own progress. Unlike Timer, which reports time elapsed
since the bar started, it shows the actual time of day, updated each
redraw at whole-second resolution.
- class progressbar.widgets.CurrentTime(format='Current Time: %(current_time)s', microseconds=False, **kwargs)[source]
Bases:
FormatWidgetMixin,TimeSensitiveWidgetBaseWidget which displays the current (date)time with seconds resolution.
Create a CurrentTime.
- Parameters:
format – Template string. Adds current_time/ current_datetime keys on top of the usual data() set.
microseconds – Keep microsecond resolution instead of truncating to whole seconds.
**kwargs – Forwarded to the next class in the cooperative __init__ chain.
- current_datetime()[source]
Return datetime.now(), seconds-truncated unless microseconds.
- current_time()[source]
Return self.current_datetime()’s time-of-day component.
Example¶
"""``CurrentTime`` displays the current date and time, updated live.
Reach for it to stamp log-like output with a wall clock, independent of
the bar's own progress -- unlike ``Timer``, which reports time elapsed
*since the bar started* rather than the actual time of day.
This example runs longer than most in this set: the clock is only shown
to whole-second resolution, so a bar that finishes in a fraction of a
second would show the same, unmoving reading for its entire run --
"updated live" would never actually be visible.
"""
import time
import progressbar
STEPS = 24
def main() -> None:
widgets = [progressbar.CurrentTime(), ' ', progressbar.Bar()]
with progressbar.ProgressBar(max_value=STEPS, widgets=widgets) as bar:
for step in range(STEPS):
bar.update(step + 1)
time.sleep(0.3)
if __name__ == '__main__':
main()
See also¶
Timer: elapsed time since the bar started, instead of the time of day.