Refactor the timeSincePrefix and timeUntilPrefix functions

Instead of having two separate functions to get the prefix
and the formatted time interval the new functions return
both the prefix and the time interval.

refs #5981
This commit is contained in:
Gunnar Beutner 2014-05-29 13:03:10 +02:00
parent 7b0a48bef5
commit a66a1a4729
7 changed files with 30 additions and 43 deletions

View File

@ -101,32 +101,20 @@ class Format
return self::showHourMin($duration);
}
protected static function showHourMin($sec, $returnPrefix = false)
protected static function showHourMin($sec, $includePrefix = false)
{
$min = floor($sec / 60);
if ($min < 60) {
if ($returnPrefix) {
return 'For';
} else {
return $min . 'm ' . ($sec % 60) . 's';
}
return ($includePrefix ? t('for') . ' ' : '') . $min . 'm ' . ($sec % 60) . 's';
}
$hour = floor($min / 60);
if ($hour < 24) {
if ($returnPrefix) {
return 'Since';
} else {
return date('H:i', time() - $sec);
}
}
if ($returnPrefix) {
return 'For';
} else {
return floor($hour / 24) . 'd ' . ($hour % 24) . 'h';
return ($includePrefix ? t('since') . ' ' : '') . date('H:i', time() - $sec);
}
return ($includePrefix ? t('for') . ' ' : '') . floor($hour / 24) . 'd ' . ($hour % 24) . 'h';
}
protected static function smartTimeDiff($diff, $timestamp, $returnPrefix = false)
protected static function smartTimeDiff($diff, $timestamp, $includePrefix = false)
{
if ($timestamp === null || $timestamp === false) {
return '-';
@ -140,19 +128,11 @@ class Format
}
if (abs($diff) > 3600 * 24 * 3) {
if (date('Y') === date('Y', $timestamp)) {
if ($returnPrefix) {
return 'Since';
} else {
return date('d.m.', $timestamp);
return ($includePrefix ? t('since') . ' ' : '') . date('d.m.', $timestamp);
}
return ($includePrefix ? t('since') . ' ' : '') . date('m.Y', $timestamp);
}
if ($returnPrefix) {
return 'Since';
} else {
return date('m.Y', $timestamp);
}
}
return $prefix . self::showHourMin(abs($diff), $returnPrefix);
return $prefix . self::showHourMin(abs($diff), $includePrefix);
}
public static function timeSince($timestamp)
@ -160,9 +140,13 @@ class Format
return self::smartTimeDiff(time() - $timestamp, $timestamp);
}
public static function timeSincePrefix($timestamp)
public static function prefixedTimeSince($timestamp, $ucfirst = false)
{
return self::smartTimeDiff(time() - $timestamp, $timestamp, true);
$result = self::smartTimeDiff(time() - $timestamp, $timestamp, true);
if ($ucfirst) {
$result = ucfirst($result);
}
return $result;
}
public static function timeUntil($timestamp)
@ -170,9 +154,13 @@ class Format
return self::smartTimeDiff($timestamp - time(), $timestamp);
}
public static function timeUntilPrefix($timestamp)
public static function prefixedTimeUntil($timestamp, $ucfirst)
{
return self::smartTimeDiff($timestamp - time(), $timestamp, true);
$result = self::smartTimeDiff($timestamp - time(), $timestamp, true);
if ($ucfirst) {
$result = ucfirst($result);
}
return $result;
}
protected static function formatForUnits($value, & $units, $base)

View File

@ -15,9 +15,9 @@ $this->addHelperFunction('timeSince', function ($timestamp) {
. '</span>';
});
$this->addHelperFunction('timeSincePrefix', function ($timestamp) {
$this->addHelperFunction('prefixedTimeSince', function ($timestamp, $ucfirst = false) {
return '<span class="timesince">'
. $this->translate(Format::timeSincePrefix($timestamp))
. Format::prefixedTimeSince($timestamp, $ucfirst)
. ' </span>';
});
@ -28,9 +28,9 @@ $this->addHelperFunction('timeUntil', function ($timestamp) {
. '</span>';
});
$this->addHelperFunction('timeUntilPrefix', function ($timestamp) {
$this->addHelperFunction('prefixedTimeUntil', function ($timestamp, $ucfirst = false) {
if (! $timestamp) return '';
return '<span class="timeuntil">'
. $this->translate(Format::timeUntilPrefix($timestamp))
. Format::prefixedTimeUntil($timestamp, $ucfirst)
. '</span>';
});

View File

@ -295,8 +295,7 @@ class ListCommand extends Command
$leaf,
$screen->underline($row->service_description),
$screen->colorize($utils->objectStateFlags('service', $row) . $perf, 'lightblue'),
strtolower(Format::timeSincePrefix($row->service_last_state_change)),
Format::timeSince($row->service_last_state_change)
Format::prefixedTimeSince($row->service_last_state_change, true)
);
if ($this->isVerbose) {
$out .= $emptyLine . preg_replace(

View File

@ -78,7 +78,7 @@ if ($hosts->count() === 0) {
<strong><?= ucfirst($helper->monitoringState($host, 'host')); ?></strong><br />
<div class="small-row">
<?= $this->timeSincePrefix($host->host_last_state_change) ?> <?= $this->timeSince($host->host_last_state_change); ?>
<?= $this->prefixedTimeSince($host->host_last_state_change, true) ?>
<?php if ($host->host_state > 0): ?>
<br />
<strong><?= ($host->host_state_type === '1') ? $this->translate('Hard') : $this->translate('Soft') ?> </strong> <?= $host->host_current_check_attempt; ?>/<?= $host->host_max_check_attempts; ?>

View File

@ -41,7 +41,7 @@ foreach ($services as $service):
<td class="state" title="<?= $helper->getStateTitle($service, 'service'); ?>">
<strong><?= $this->translate(strtoupper($helper->monitoringState($service, 'service'))) ?></strong><br />
<?php if (!$this->compact): ?><?= $this->timeSincePrefix($service->service_last_state_change) ?> <?php endif ?><?= $this->timeSince($service->service_last_state_change); ?>
<?php if (!$this->compact): ?><?= $this->prefixedTimeSince($service->service_last_state_change); ?><?php else: ?><?= $this->timeSince($service->service_last_state_change); ?><?php endif ?>
<?php if ($service->service_state > 0 && (int) $service->service_state_type === 0): ?>
<br />
<strong>Soft <?= $service->service_attempt ?></strong>

View File

@ -10,7 +10,7 @@ $cf = $this->getHelper('CommandForm');
<div class="content processinfo">
<p>Backend <strong><?= $this->backendName; ?></strong>
<?= $ps->is_currently_running === '1' ? sprintf('has been running with PID %d %s ', $ps->process_id, strtolower($this->timeSincePrefix($ps->program_start_time))) . $this->timeSince($ps->program_start_time) : 'is not running'; ?>.
<?= $ps->is_currently_running === '1' ? sprintf('has been running with PID %d ', $ps->process_id) . $this->prefixedTimeSince($ps->program_start_time) : 'is not running'; ?>.
<table class="avp">
<tbody>

View File

@ -6,7 +6,7 @@
<tr class="state <?= $this->monitoringState($object, 'host') ?><?= $object->host_handled ? ' handled' : '' ?>">
<td class="state"<?= $showService ? '' : ' rowspan="2"' ?>>
<?= $this->translate($this->util()->getHostStateName($object->host_state)) ?><br />
<?= $this->timeSincePrefix($object->host_last_state_change) ?> <?= $this->timeSince($object->host_last_state_change) ?>
<?= $this->prefixedTimeSince($object->host_last_state_change, true) ?>
</td>
<td><b><?= $this->escape($object->host_name) ?></b><?php
if ($object->host_address && $object->host_address !== $object->host_name): ?>
@ -18,7 +18,7 @@
<tr class="state <?= $this->monitoringState($object, 'service') ?><?= $object->service_handled ? ' handled' : '' ?>">
<td class="state">
<?= $this->translate($this->util()->getServiceStateName($object->service_state)) ?><br />
<?= $this->timeSincePrefix($object->service_last_state_change) ?> <?= $this->timeSince($object->service_last_state_change) ?>
<?= $this->prefixedTimeSince($object->service_last_state_change, true) ?>
</td>
<td><b><?= $this->translate('Service') ?>: <?= $this->escape($object->service_description) ?></b>