Skip to content

Commit 9ac0dde

Browse files
Uses JavaScript to update "time elapsed" counter in maintenance tools
Signed-off-by: Jon Stovell <jonstovell@gmail.com>
1 parent c1b6c4e commit 9ac0dde

3 files changed

Lines changed: 29 additions & 14 deletions

File tree

Languages/en_US/Maintenance.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,7 @@
6565
$txt['maintenance_step'] = 'Step';
6666
$txt['maintenance_overall_progress'] = 'Overall Progress';
6767
$txt['maintenance_substep_progress'] = 'Step Progress';
68-
$txt['maintenance_time_elasped_ms'] = 'Time Elapsed {m, plural,
69-
one {# minute}
70-
other {# minutes}
71-
} and {s, plural,
72-
one {# second}
73-
other {# seconds}
74-
}';
68+
$txt['maintenance_time_elapsed'] = 'Time Elapsed: ';
7569

7670
// File Permissions.
7771
$txt['chmod_linux_info'] = 'If you have a shell account, the following command can automatically correct permissions on these files';

Sources/Maintenance/Maintenance.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -744,12 +744,17 @@ public static function loginWithDatabasePassword(
744744
*/
745745
public static function getTimeElapsed(): string
746746
{
747-
// How long have we been running this?
748-
$elapsed = time() - (int) self::$context['started'];
749-
$mins = (int) ($elapsed / 60);
750-
$seconds = $elapsed - $mins * 60;
747+
$duration = (new \DateTime('@' . self::$context['started']))->diff(new \DateTime());
751748

752-
return Lang::getTxt('maintenance_time_elasped_ms', ['m' => $mins, 's' => $seconds]);
749+
if ((int) $duration->format('%a') > 0) {
750+
return \strval((int) $duration->format('%h') + ((int) $duration->format('%a') * 24)) . $duration->format(':%I:%S');
751+
}
752+
753+
if ((int) $duration->format('%h') > 0) {
754+
return $duration->format('%h:%I:%S');
755+
}
756+
757+
return $duration->format('%i:%S');
753758
}
754759

755760
/**

Themes/default/MaintenanceTemplate.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static function header(): void
4646
const smf_charset = \'UTF-8\';
4747
const allow_xhjr_credentials = true;
4848
const isDebug = ', Maintenance::$tool->isDebug() ? 'true' : 'false', ';
49+
const timeStarted = ', Maintenance::$context['started'], ';
4950
let startPercent = ', Maintenance::$overall_percent, ';
5051
</script>
5152
</head>
@@ -124,8 +125,9 @@ public static function header(): void
124125
<span id="substep_text">', Maintenance::getItemsProgress(), '%</span>
125126
</div>
126127
127-
<div id="time_elapsed" class="smalltext time_elapsed">
128-
', Maintenance::getTimeElapsed(), '
128+
<div class="smalltext time_elapsed">
129+
', Lang::getTxt('maintenance_time_elapsed', file: 'Maintenance'), '
130+
<time id="time_elapsed">', Maintenance::getTimeElapsed(), '</time>
129131
</div>
130132
</div>
131133
<div id="main_screen" class="clear">
@@ -183,6 +185,20 @@ function updateItemsProgress(current, max)
183185
setInnerHTML(document.getElementById("item_text"), width + "%");
184186
}
185187
}
188+
189+
// This function dynamically updates the "time elapsed" counter.
190+
function updateTimeElapsed()
191+
{
192+
const elapsed = (Date.now() / 1000) - timeStarted;
193+
194+
const s = elapsed % 60;
195+
const m = Math.floor(elapsed / 60) % 60;
196+
const h = Math.floor(elapsed / 3600);
197+
198+
if (document.getElementById("time_elapsed")) {
199+
document.getElementById("time_elapsed").textContent = ((h > 0) ? ((h < 10) ? "0" + h.toString() : h.toString()) + ":" : "") + ((m < 10) ? "0" + m.toString() : m.toString()) + ":" + ((s < 10) ? "0" + s.toString() : s.toString());
200+
}
201+
}
186202
</script>
187203
</body>
188204
</html>';

0 commit comments

Comments
 (0)