/usr/share/php/Icinga/Date/DateFormatter.php is in php-icinga 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Date;
/**
* Date formatting
*/
class DateFormatter
{
/**
* Format relative
*
* @var int
*/
const RELATIVE = 0;
/**
* Format time
*
* @var int
*/
const TIME = 1;
/**
* Format date
*
* @var int
*/
const DATE = 2;
/**
* Format date and time
*
* @var int
*/
const DATETIME = 4;
/**
* Get the diff between the given time and the current time
*
* @param int|float $time
*
* @return array
*/
protected static function diff($time)
{
$invert = false;
$now = time();
$time = (float) $time;
$diff = $time - $now;
if ($diff < 0) {
$diff = abs($diff);
$invert = true;
}
if ($diff > 3600 * 24 * 3) {
$type = static::DATE;
if (date('Y') === date('Y', $time)) {
$formatted = date('M j', $time);
} else {
$formatted = date('Y-m', $time);
}
} else {
$minutes = floor($diff / 60);
if ($minutes < 60) {
$type = static::RELATIVE;
$formatted = sprintf('%dm %ds', $minutes, $diff % 60);
} else {
$hours = floor($minutes / 60);
if ($hours < 24) {
if (date('d') === date('d', $time)) {
$type = static::TIME;
$formatted = date('H:i', $time);
} else {
$type = static::DATE;
$formatted = date('M j H:i', $time);
}
} else {
$type = static::RELATIVE;
$formatted = sprintf('%dd %dh', floor($hours / 24), $hours % 24);
}
}
}
return array($type, $formatted, $invert);
}
/**
* Format date
*
* @param int|float $date
*
* @return string
*/
public static function formatDate($date)
{
return date('Y-m-d', (float) $date);
}
/**
* Format date and time
*
* @param int|float $dateTime
*
* @return string
*/
public static function formatDateTime($dateTime)
{
return date('Y-m-d H:i:s', (float) $dateTime);
}
/**
* Format a duration
*
* @param int|float $seconds Duration in seconds
*
* @return string
*/
public static function formatDuration($seconds)
{
$minutes = floor((float) $seconds / 60);
if ($minutes < 60) {
$formatted = sprintf('%dm %ds', $minutes, $seconds % 60);
} else {
$hours = floor($minutes / 60);
if ($hours < 24) {
$formatted = sprintf('%dh %dm', $hours, $minutes % 60);
} else {
$formatted = sprintf('%dd %dh', floor($hours / 24), $hours % 24);
}
}
return $formatted;
}
/**
* Format time
*
* @param int|float $time
*
* @return string
*/
public static function formatTime($time)
{
return date('H:i:s', (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) {
case static::DATE:
// Move to next case
case static::DATETIME:
$formatted = sprintf(
t('on %s', 'An event happened on the given date or date and time'),
$ago
);
break;
case static::RELATIVE:
$formatted = sprintf(
t('%s ago', 'An event that happened the given time interval ago'),
$ago
);
break;
case static::TIME:
$formatted = sprintf(t('at %s', 'An event happened at the given time'), $ago);
break;
}
return $formatted;
}
/**
* Format time as time since
*
* @param int|float $time
* @param bool $timeOnly
*
* @return string
*/
public static function timeSince($time, $timeOnly = false)
{
list($type, $since, $invert) = static::diff($time);
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:
$formatted = sprintf(
t('since %s', 'A status is lasting since the given time, date or date and time'),
$since
);
break;
}
return $formatted;
}
/**
* Format time as time until
*
* @param int|float $time
* @param bool $timeOnly
*
* @return string
*/
public static function timeUntil($time, $timeOnly = false)
{
list($type, $until, $invert) = static::diff($time);
if ($invert && $type === static::RELATIVE) {
$until = '-' . $until;
}
if ($timeOnly) {
return $until;
}
switch ($type) {
case static::DATE:
// Move to next case
case static::DATETIME:
$formatted = sprintf(
t('on %s', 'An event will happen on the given date or date and time'),
$until
);
break;
case static::RELATIVE:
$formatted = sprintf(
t('in %s', 'An event will happen after the given time interval has elapsed'),
$until
);
break;
case static::TIME:
$formatted = sprintf(t('at %s', 'An event will happen at the given time'), $until);
break;
}
return $formatted;
}
}
|