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: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271:
<?php
namespace Apptus\ESales\Connector\Time;
/**
* Constants and static methods for time units.
*/
class Unit {
const SECOND = 'SECOND';
const MINUTE = 'MINUTE';
const HOUR = 'HOUR';
const DAY = 'DAY';
const WEEK = 'WEEK';
const MONTH = 'MONTH';
const YEAR = 'YEAR';
private static $CMP = array (
'SECOND' => 1,
'MINUTE' => 2,
'HOUR' => 3,
'DAY' => 4,
'WEEK' => 5,
'MONTH' => 6,
'YEAR' => 7);
private final function __construct() {
// prevent subclassing and instantiation
}
/**
* @param string
* A unit.
* @throws \InvalidArgumentException if the unit is not one of the constants from this class.
* @return string A one-character representation of the unit.
*/
public static function symbol($unit) {
switch ($unit) {
case self::SECOND:
return 's';
case self::MINUTE:
return 'm';
case self::HOUR:
return 'h';
case self::DAY:
return 'd';
case self::WEEK:
return 'w';
case self::MONTH:
return 'M';
case self::YEAR:
return 'y';
}
throw new \InvalidArgumentException('Unknown unit: ' . $unit);
}
/**
* @param string
* A one-character representation of a unit.
* @throws \InvalidArgumentException if no unit is associated with the symbol.
* @return string The unit associated with the symbol.
*/
public static function forSymbol($symbol) {
switch ($symbol) {
case 's':
return self::SECOND;
case 'm':
return self::MINUTE;
case 'h':
return self::HOUR;
case 'd':
return self::DAY;
case 'w':
return self::WEEK;
case 'M':
return self::MONTH;
case 'y':
return self::YEAR;
}
throw new \InvalidArgumentException('Unknown symbol: ' . $symbol);
}
/**
* Formats a DateTime depending on the unit.
*
* @param string
* @param \DateTime
* @throws \InvalidArgumentException if the unit is not one of the constants from this class.
* @return string
*/
public static function displayName($unit, \DateTime $dt) {
switch ((string) $unit) {
case self::YEAR:
return $dt->format('Y');
case self::MONTH:
return $dt->format('Y F');
case self::WEEK:
return $dt->format('Y \wW');
case self::DAY:
return $dt->format('Y-m-d');
case self::HOUR:
return $dt->format('Y-m-d H:00');
case self::MINUTE:
return $dt->format('Y-m-d H:i');
case self::SECOND:
return $dt->format('Y-m-d H:i:s');
}
throw new \InvalidArgumentException('Unknown unit: ' . $unit);
}
/**
* Moves a DateTime {@code $amount} units in time.
*
* Use a positive amount to move forward and a negative amount to move backwards.
*
* @param \DateTime
* @param int
* The number of units to move.
* @param string
* The unit. Must be one of the constants in Unit.
* @return \DateTime The DateTime for chaining.
*/
public static function add(\DateTime $dt, $amount, $unit) {
$amount = (int) $amount;
$sub = false;
if ($amount < 0) {
$sub = true;
$amount = -$amount;
}
$symbol = Unit::symbol($unit);
$iso = 'P';
switch ($symbol) {
case 's':
case 'm':
case 'h':
$iso .= 'T';
// fall-through
default:
$iso .= $amount . strtoupper($symbol);
}
if ($sub) {
return $dt->sub(new \DateInterval($iso));
}
return $dt->add(new \DateInterval($iso));
}
/**
* Returns a new DateTime moved back in time to the closest whole unit.
*
* @param \DateTime
* The DateTime to floor.
* @param string
* The unit. Must be one of the constants in Unit.
* @param string
* The english three letter abbreviation for the first day of the week, e.g. 'Mon' or 'Sun'. Only needed when unit is WEEK.
* @throws \InvalidArgumentException
* @return \DateTime A new DateTime.
*/
public static function floor(\DateTime $dt, $unit, $firstDayOfWeek = 'Mon') {
$symbol = self::symbol($unit);
$result = clone $dt;
switch ($symbol) {
case 'y':
$result->setTime(0, 0, 0);
return $result->setDate((int) $dt->format('Y'), 1, 1);
case 'M':
$result->setTime(0, 0, 0);
return $result->setDate((int) $dt->format('Y'), (int) $dt->format('m'), 1);
case 'w':
$result->setTime(0, 0, 0);
for ($i = 0; $i < 7; ++$i) {
if ($result->format('D') === $firstDayOfWeek) {
return $result;
}
$result->modify('-1 day');
}
throw new \InvalidArgumentException('Invalid first day of week: ' . $firstDayOfWeek);
case 'd':
return $result->setTime(0, 0, 0);
case 'h':
return $result->setTime($dt->format('H'), 0, 0);
case 'm':
return $result->setTime($dt->format('H'), $dt->format('i'), 0);
case 's':
return $result;
}
return null;
}
/**
* Returns a new DateTime moved forward in time to the closest whole unit.
*
* Will not change the DateTime if the time is already ceiled.
*
* @param \DateTime
* The DateTime to ceil.
* @param string
* The unit. Must be one of the constants in Unit.
* @param string
* The english three letter abbreviation for the first day of the week, e.g. 'Mon' or 'Sun'. Only needed when unit is WEEK.
* @return \DateTime A new DateTime.
*/
public static function ceil(\DateTime $dt, $unit, $firstDayOfWeek = 'Mon') {
$t = $dt->getTimestamp();
$result = self::floor($dt, $unit, $firstDayOfWeek);
if ($result->getTimestamp() < $t) {
self::add($result, 1, $unit);
}
return $result;
}
/**
* Returns a new DateTime moved to the closest whole unit.
*
* Will not change the DateTime if the time is already at the start of a unit.
*
* @param \DateTime
* The DateTime to ceil.
* @param string
* The unit. Must be one of the constants in Unit.
* @param string
* The english three letter abbreviation for the first day of the week, e.g. 'Mon' or 'Sun'. Only needed when unit is WEEK.
* @return \DateTime A new DateTime.
*/
public static function round(\DateTime $dt, $unit, $firstDayOfWeek = 'Mon') {
$t = $dt->getTimestamp();
$floor = self::floor($dt, $unit, $firstDayOfWeek);
$floor_t = $floor->getTimestamp();
$ceil = clone $floor;
self::add($ceil, 1, $unit);
$ceil_t = $ceil->getTimestamp();
if ($t - $floor_t < $ceil_t - $t) {
return $floor;
}
return $ceil;
}
/**
* @internal
* @param string
* A unit.
* @param string
* Another unit.
* @throws \InvalidArgumentException if any of the two arguments are not units.
* @return int 0 if the units are the same, -1 if the first unit is smaller than the second unit or 1 if the first unit is larger than the second unit.
*/
public static function compare($u1, $u2) {
if (!isset(self::$CMP[$u1])) throw new \InvalidArgumentException('Not a unit: ' . $u1);
if (!isset(self::$CMP[$u2])) throw new \InvalidArgumentException('Not a unit: ' . $u2);
$c1 = self::$CMP[$u1];
$c2 = self::$CMP[$u2];
if ($c1 === $c2) return 0;
if ($c1 < $c2) return -1;
return 1;
}
/**
* @internal
* @param string
* A unit.
* @param string
* Another unit.
* @throws \InvalidArgumentException if any of the two arguments are not units.
* @return int 0 if the units are the same, 1 if the first unit is smaller than the second unit or -1 if the first unit is larger than the second unit.
*/
public static function reverse_compare($u1, $u2) {
return self::compare($u2, $u1);
}
}