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:
<?php
namespace Apptus\ESales\Connector\Time;
/**
* A point on the time scale. An object of this class represents a
* specific point in time regardless of time zone or locale.
*
* Time points are immutable objects that can converted to and
* from the standard PHP time objects DateTime or time in seconds
* since 1 January 1970 (i.e. Epoch time).
*
* New time points can be calculated relative to this time point,
* using the methods forward, back, next, floor, ceil and round.
*
* ISO 8601 representations of date and time can be obtained by
* calling the date, weekDate, ordinalDate, time or dateTime
* methods supplying a local time zone and a locale
* (for week dates).
*
* Convenience methods for creating time points for the current
* time and for the beginning of the current day are also included.
*/
class TimePoint {
/**
* @var float|int
*/
private $timestamp;
/**
* Returns a time point representing the current time.
*
* @return TimePoint
*/
public static function now() {
return new TimePoint(microtime(true));
}
/**
* Returns a time point representing that start of today.
*
* @param \DateTimeZone
* @return TimePoint
*/
public static function today(\DateTimeZone $tz) {
$dt = new \DateTime(null, $tz);
$dt->setTime(0, 0, 0);
return new TimePoint($dt->getTimestamp());
}
/**
* Converts a PHP timestamp to a time point.
*
* @param float|int A float (for sub-second precision) or an integer with seconds since 1 January 1970 00:00:00.
* @return TimePoint
*/
public static function fromTimestamp($timestamp) {
return new TimePoint($timestamp);
}
/**
* Converts a PHP DateTime object to a time point.
*
* Note that \DateTime does not have millisecond precision.
*
* @param \DateTime
* @return TimePoint
*/
public static function fromDateTime(\DateTime $datetime) {
return new TimePoint($datetime->getTimestamp());
}
/**
* Converts a Java timestamp to a time point.
*
* @param string
* Milliseconds since 1 January 1970 00:00:00. Note that it is given as a string to avoid problems with 32 bit PHP.
* @return TimePoint
*/
public static function fromMillis($millis) {
$millis = (string) $millis;
$len = strlen($millis);
if ($len < 9) {
return new TimePoint(((int) $millis) / 1000);
}
$ts = (int) substr($millis, 0, $len - 3);
$millis = ltrim(substr($millis, $len - 3));
$millis = $millis === '' ? 0.0 : ((int) $millis) / 1000;
return new TimePoint($ts + $millis);
}
/**
* Parses a date-time specification as defined by ISO 8601 and
* returns a time point valid for the specified date-time.
*
* @param string
* The input string.
* @param \DateTimeZone
* The local time zone.
* @return TimePoint
*/
public static function parse($iso, \DateTimeZone $tz) {
return IsoDateTime::parse($iso)->toTimePoint($tz);
}
private function __construct($timestamp) {
$this->timestamp = $timestamp;
}
/**
* Returns a new time point moved forward the length of a duration.
*
* @param Duration
* The duration to move forward.
* @param \DateTimeZone
* The local time zone.
* @return TimePoint A new time point.
*/
public function forward(Duration $duration, \DateTimeZone $tz) {
$ts = (int) $this->timestamp;
$dt = new \DateTime(null, $tz);
$dt->setTimestamp($ts);
$dt = $duration->forward($dt);
if (gettype($this->timestamp) === 'double') {
$millis = $this->timestamp - $ts;
return new TimePoint($millis + $dt->getTimestamp());
}
return new TimePoint($dt->getTimestamp());
}
/**
* Returns a new time point moved back the length of a duration.
*
* @param Duration
* The duration to move back.
* @param \DateTimeZone
* The time zone.
* @return TimePoint A new time point.
*/
public function back(Duration $duration, \DateTimeZone $tz) {
$ts = (int) $this->timestamp;
$dt = new \DateTime(null, $tz);
$dt->setTimestamp($ts);
$dt = $duration->back($dt);
if (gettype($this->timestamp) === 'double') {
$millis = $this->timestamp - $ts;
return new TimePoint($millis + $dt->getTimestamp());
}
return new TimePoint($dt->getTimestamp());
}
/**
* Return the date of this TimePoint for the given time zone.
*
* @param \DateTimeZone The time zone.
* @return CalendarDate A CalendarDate object.
*/
public function date(\DateTimeZone $tz) {
$dt = new \DateTime(null, $tz);
$dt->setTimestamp((int) $this->timestamp);
return new CalendarDate((int) $dt->format('Y'), (int) $dt->format('n'), (int) $dt->format('j'));
}
public function weekDate(\DateTimeZone $tz) {
$dt = new \DateTime(null, $tz);
$dt->setTimestamp((int) $this->timestamp);
return new WeekDate((int) $dt->format('Y'), (int) $dt->format('W'), (int) $dt->format('N'));
}
public function ordinalDate(\DateTimeZone $tz) {
$dt = new \DateTime(null, $tz);
$dt->setTimestamp((int) $this->timestamp);
return new OrdinalDate((int) $dt->format('Y'), 1 + (int) $dt->format('z'));
}
/**
* Return the time of this TimePoint for the given time zone.
*
* @param \DateTimeZone The time zone.
* @return Time A Time object.
*/
public function time(\DateTimeZone $tz) {
$dt = new \DateTime(null, $tz);
$dt->setTimestamp((int) $this->timestamp);
$time = explode(':', $dt->format('G:i:s'));
$hour = (int) $time[0];
$minute = (int) ($time[1][0] === '0' ? $time[1][1] : $time[1]);
$second = (int) ($time[2][0] === '0' ? $time[2][1] : $time[2]);
$offset = $tz->getOffset($dt);
$offset = (int) ($offset / 60); // Convert to offset in minutes.
return new Time($hour, $minute, $second, $offset);
}
/**
* Retrun the date and time of this TimePoint for the given time zone.
*
* @param \DateTimeZone The time zone.
* @return IsoDateTime An IsoDateTime object.
*/
public function dateTime(\DateTimeZone $tz) {
$date = $this->date($tz);
$time = $this->time($tz);
return new IsoDateTime($date, $time);
}
/**
* Convert this TimePoint to a standard PHP DateTime object with the given DateTimeZone.
*
* @param \DateTimeZone The time zone.
* @return \DateTime A DateTime object.
*/
public function toDateTime(\DateTimeZone $tz) {
$dt = new \DateTime(null, $tz);
$dt->setTimestamp((int) $this->timestamp);
return $dt;
}
/**
* Convert this TimePoint to a PHP timestamp, i.e. seconds since 1 January 1970 00:00:00.
*
* @return float|int A float with sub-second precision or an integer, depending on how the TimePoint was created.
*/
public function toTimestamp() {
return $this->timestamp;
}
/**
* Convert this TimePoint to a Java timestamp, i.e. milliseconds since 1 January 1970 00:00:00.
*
* Note that the result is returned as a string to avoid 32 bit integer issues.
*
* @return string
*/
public function toMillis() {
if (gettype($this->timestamp) === 'double') {
$ts = (int) $this->timestamp;
$millis = (int) (1000 * ($this->timestamp - $ts));
return $ts . sprintf('%03d', $millis);
}
return $this->timestamp . '000';
}
/**
* Returns a string on the format YYYY-MM-DDTHH:MM:SSZ representing this time point.
*
* @return string
*/
public function __toString() {
return $this->toDateTime(new \DateTimeZone('UTC'))->format('Y-m-d\TH:i:s\Z');
}
}