Replace DateFormatter format constants w/ format functions

refs #6778
This commit is contained in:
Eric Lippmann 2015-05-19 09:55:21 +02:00
parent aaa8e4bf6a
commit bf8c4f1e9d
1 changed files with 139 additions and 150 deletions

View File

@ -4,7 +4,6 @@
namespace Icinga\Date; namespace Icinga\Date;
use IntlDateFormatter; use IntlDateFormatter;
use InvalidArgumentException;
/** /**
* ICU date formatting * ICU date formatting
@ -12,11 +11,11 @@ use InvalidArgumentException;
class DateFormatter class DateFormatter
{ {
/** /**
* Internal constant for relative time diffs * Format relative
* *
* @var int * @var int
*/ */
protected static $relative = 0; const RELATIVE = 0;
/** /**
* Format time * Format time
@ -40,67 +39,17 @@ class DateFormatter
const DATETIME = 4; const DATETIME = 4;
/** /**
* Format time diff as time ago * Get the diff between the given time and the current time
*
* @var int
*/
const AGO = 8;
/**
* Format time diff as time until
*
* @var int
*/
const UNTIL = 16;
/**
* Format time diff as time since
*
* @var int
*/
const SINCE = 32;
/**
* Format used for the DateFormatter
*
* @var int
*/
protected $format;
/**
* Create a date formatter
*
* @param int $format
*/
public function __construct($format)
{
$this->format = $format;
}
/**
* Create a date formatter
*
* @param int $format
*
* @return static
*/
public static function create($format)
{
return new static($format);
}
/**
* Format the diff between two date/time values as a string
* *
* @param int|float $time * @param int|float $time
* @param int|float $now
* *
* @return string The formatted string * @return array
*/ */
protected function diff($time, $now = null) protected static function diff($time)
{ {
$now = $now === null ? time() : (float) $now;
$invert = false; $invert = false;
$now = time();
$time = (float) $time;
$diff = $time - $now; $diff = $time - $now;
if ($diff < 0) { if ($diff < 0) {
$diff = abs($diff); $diff = abs($diff);
@ -113,7 +62,7 @@ class DateFormatter
} else { } else {
$minutes = floor($diff / 60); $minutes = floor($diff / 60);
if ($minutes < 60) { if ($minutes < 60) {
$type = static::$relative; $type = static::RELATIVE;
$formatted = sprintf('%dm %ds', $minutes, $diff % 60); $formatted = sprintf('%dm %ds', $minutes, $diff % 60);
} else { } else {
$hours = floor($minutes / 60); $hours = floor($minutes / 60);
@ -122,116 +71,156 @@ class DateFormatter
$fmt = new IntlDateFormatter(null, IntlDateFormatter::NONE, IntlDateFormatter::SHORT); $fmt = new IntlDateFormatter(null, IntlDateFormatter::NONE, IntlDateFormatter::SHORT);
$formatted = $fmt->format($time); $formatted = $fmt->format($time);
} else { } else {
$type = static::$relative; $type = static::RELATIVE;
$formatted = sprintf('%dd %dh', floor($hours / 24), $hours % 24); $formatted = sprintf('%dd %dh', floor($hours / 24), $hours % 24);
} }
} }
} }
switch ($this->format) { return array($type, $formatted, $invert);
case static::AGO: }
/**
* Format date
*
* @param int|float time
*
* @return string
*/
public static function formatDate($time)
{
$fmt = new IntlDateFormatter(null, IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
return $fmt->format((float) $time);
}
/**
* Format date and time
*
* @param int|float time
*
* @return string
*/
public static function formatDateTime($time)
{
$fmt = new IntlDateFormatter(null, IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);
return $fmt->format((float) $time);
}
/**
* Format time
*
* @param int|float time
*
* @return string
*/
public static function formatTime($time)
{
$fmt = new IntlDateFormatter(null, IntlDateFormatter::NONE, IntlDateFormatter::SHORT);
return $fmt->format((float) $time);
}
/**
* Format time as time ago
*
* @param int|float $time
* @param bool $timeOnly
*
* @return string
*/
public static function timeAgo($time, $timeOnly = false)
{
list($type, $ago, $invert) = static::diff($time);
if ($timeOnly) {
return $ago;
}
switch ($type) { switch ($type) {
case static::$relative:
$formatted = sprintf(
t('%s ago', 'An event that happened the given time interval ago'),
$formatted
);
break;
case static::TIME:
$formatted = sprintf(t('at %s', 'An event happened at the given time'), $formatted);
break;
case static::DATE: case static::DATE:
// Move to next case // Move to next case
case static::DATETIME: case static::DATETIME:
// TODO(el): Use own format string for date?
$formatted = sprintf( $formatted = sprintf(
t('on %s', 'An event happened on the given date or date and time'), t('on %s', 'An event happened on the given date or date and time'),
$formatted $ago
); );
break; break;
} case static::RELATIVE:
break;
case static::UNTIL:
switch ($type) {
case static::$relative:
$formatted = sprintf( $formatted = sprintf(
t('in %s', 'An event will happen after the given time interval has elapsed'), t('%s ago', 'An event that happened the given time interval ago'),
$invert ? ('-' . $formatted) : $formatted $ago
); );
break; break;
case static::TIME: case static::TIME:
$formatted = sprintf(t('at %s', 'An event will happen at the given time'), $formatted); $formatted = sprintf(t('at %s', 'An event happened at the given time'), $ago);
break;
case static::DATE:
// Move to next case
case static::DATETIME:
// TODO(el): Use own format string for date?
$formatted = sprintf(
t('on %s', 'An event will happen on the given date or date and time'),
$formatted
);
break;
}
break;
case static::SINCE;
switch ($type) {
case static::$relative:
$formatted = sprintf(
t('for %s', 'A status is lasting for the given time interval'),
$formatted
);
break;
case static::TIME:
// Move to next case
case static::DATE:
// Move to next case
case static::DATETIME:
// TODO(el): Use own format strings for time and date?
$formatted = sprintf(
t('since %s', 'A status is lasting since the given time, date or date and time'),
$formatted
);
break;
}
break;
default:
break; break;
} }
return $formatted; return $formatted;
} }
/** /**
* Format a date/time value as a string * Format time as time since
* *
* @param int|float $time Date/time to format * @param int|float $time
* @param int|float $now * @param bool $timeOnly
* *
* @return string The formatted string * @return string
*/ */
public function format($time, $now = null) public static function timeSince($time, $timeOnly = false)
{ {
$time = (float) $time; list($type, $since, $invert) = static::diff($time);
switch ($this->format) { if ($timeOnly) {
return $since;
}
switch ($type) {
case static::RELATIVE:
$formatted = sprintf(
t('for %s', 'A status is lasting for the given time interval'),
$since
);
break;
case static::DATE:
// Move to next case
case static::DATETIME:
// Move to next case
case static::TIME: case static::TIME:
$fmt = new IntlDateFormatter(null, IntlDateFormatter::NONE, IntlDateFormatter::SHORT); $formatted = sprintf(
$formatted = $fmt->format($time); t('since %s', 'A status is lasting since the given time, date or date and time'),
$since
);
break; break;
case static::DATE; }
$fmt = new IntlDateFormatter(null, IntlDateFormatter::SHORT, IntlDateFormatter::NONE); return $formatted;
$formatted = $fmt->format($time); }
break;
case static::DATETIME; /**
$fmt = new IntlDateFormatter(null, IntlDateFormatter::SHORT, IntlDateFormatter::SHORT); * Format time as time until
$formatted = $fmt->format($time); *
break; * @param int|float $time
case static::AGO: * @param bool $timeOnly
*
* @return string
*/
public static function timeUntil($time, $timeOnly = false)
{
list($type, $until, $invert) = static::diff($time);
if ($timeOnly) {
return $until;
}
switch ($type) {
case static::DATE:
// Move to next case // Move to next case
case static::UNTIL: case static::DATETIME:
// Move to next case $formatted = sprintf(
case static::SINCE: t('on %s', 'An event will happen on the given date or date and time'),
$formatted = $this->diff($time, $now); $until
);
break;
case static::RELATIVE:
$formatted = sprintf(
t('in %s', 'An event will happen after the given time interval has elapsed'),
$invert ? ('-' . $until) : $until
);
break;
case static::TIME:
$formatted = sprintf(t('at %s', 'An event will happen at the given time'), $until);
break; break;
default:
throw new InvalidArgumentException('Invalid format');
} }
return $formatted; return $formatted;
} }