Skip to content
Permalink
Browse files

Reformat elapsed time in processing to express time in hours, minutes…

… and seconds (#41573)

When appropriate, express the elapsed time in hours, minutes and seconds in addition to cumulative seconds.
  • Loading branch information
jfbourdon committed Feb 19, 2021
1 parent 51b3bfa commit 64d921fe53614ae29f04334e7b5129fefdd06d8f
Showing with 25 additions and 3 deletions.
  1. +25 −3 python/plugins/processing/gui/AlgorithmDialog.py
@@ -172,6 +172,28 @@ def runAlgorithm(self):
self.feedback.pushInfo('')
start_time = time.time()

def elapsed_time(start_time, result):
delta_t = time.time() - start_time
hours = int(delta_t / 3600)
minutes = int((delta_t % 3600) / 60)
seconds = delta_t - hours * 3600 - minutes * 60

str_hours = [self.tr("hour"), self.tr("hours")][hours > 1]
str_minutes = [self.tr("minute"), self.tr("minutes")][minutes > 1]
str_seconds = [self.tr("second"), self.tr("seconds")][seconds != 1]

if hours > 0:
elapsed = '{0} {1:0.2f} {2} ({3} {4} {5} {6} {7:0.0f} {2})'.format(
result, delta_t, str_seconds, hours, str_hours, minutes, str_minutes, seconds)
elif minutes > 0:
elapsed = '{0} {1:0.2f} {2} ({3} {4} {5:0.0f} {2})'.format(
result, delta_t, str_seconds, minutes, str_minutes, seconds)
else:
elapsed = '{0} {1:0.2f} {2}'.format(
result, delta_t, str_seconds)

return(elapsed)

if self.iterateParam:
# Make sure the Log tab is visible before executing the algorithm
try:
@@ -183,7 +205,7 @@ def runAlgorithm(self):
self.cancelButton().setEnabled(self.algorithm().flags() & QgsProcessingAlgorithm.FlagCanCancel)
if executeIterating(self.algorithm(), parameters, self.iterateParam, self.context, self.feedback):
self.feedback.pushInfo(
self.tr('Execution completed in {0:0.2f} seconds').format(time.time() - start_time))
self.tr(elapsed_time(start_time, 'Execution completed in')))
self.cancelButton().setEnabled(False)
self.finish(True, parameters, self.context, self.feedback)
else:
@@ -199,13 +221,13 @@ def runAlgorithm(self):
def on_complete(ok, results):
if ok:
self.feedback.pushInfo(
self.tr('Execution completed in {0:0.2f} seconds').format(time.time() - start_time))
self.tr(elapsed_time(start_time, 'Execution completed in')))
self.feedback.pushInfo(self.tr('Results:'))
r = {k: v for k, v in results.items() if k not in ('CHILD_RESULTS', 'CHILD_INPUTS')}
self.feedback.pushCommandInfo(pformat(r))
else:
self.feedback.reportError(
self.tr('Execution failed after {0:0.2f} seconds').format(time.time() - start_time))
self.tr(elapsed_time(start_time, 'Execution failed after')))
self.feedback.pushInfo('')

if self.feedback_dialog is not None:

0 comments on commit 64d921f

Please sign in to comment.