PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/fightbulc/moment/src/Moment.php

https://gitlab.com/wormen/client.mastodont-engine
PHP | 1258 lines | 650 code | 181 blank | 427 comment | 45 complexity | 5ad704e03d3024e26c2137f55324ce84 MD5 | raw file
  1. <?php
  2. namespace Moment;
  3. /**
  4. * Moment
  5. * Wrapper for PHP's DateTime class inspired by moment.js
  6. *
  7. * @package Moment
  8. * @author Tino Ehrich (tino@bigpun.me)
  9. */
  10. class Moment extends \DateTime
  11. {
  12. /**
  13. * @var string
  14. */
  15. private $rawDateTimeString;
  16. /**
  17. * @var string
  18. */
  19. private $timezoneString;
  20. /**
  21. * @var boolean
  22. */
  23. private $immutableMode;
  24. /**
  25. * @param string $locale
  26. *
  27. * @return void
  28. */
  29. public static function setLocale($locale)
  30. {
  31. // set current language
  32. MomentLocale::setLocale($locale);
  33. }
  34. /**
  35. * @param string $dateTime
  36. * @param string $timezone
  37. * @param bool $immutableMode
  38. *
  39. * @throws MomentException
  40. */
  41. public function __construct($dateTime = 'now', $timezone = 'UTC', $immutableMode = false)
  42. {
  43. // set moment
  44. MomentLocale::setMoment($this);
  45. // load locale content
  46. MomentLocale::loadLocaleContent();
  47. // initialize DateTime
  48. $this->resetDateTime($dateTime, $timezone);
  49. // set immutable mode
  50. $this->setImmutableMode($immutableMode);
  51. }
  52. /**
  53. * @param boolean $mode
  54. *
  55. * @return self
  56. */
  57. public function setImmutableMode($mode)
  58. {
  59. // set immutable mode to true or false
  60. $this->immutableMode = $mode;
  61. return $this;
  62. }
  63. /**
  64. * @param string $dateTime
  65. * @param string $timezoneString
  66. *
  67. * @return $this
  68. * @throws MomentException
  69. */
  70. public function resetDateTime($dateTime = 'now', $timezoneString = 'UTC')
  71. {
  72. if ($this->immutableMode)
  73. {
  74. return $this->implicitCloning(__FUNCTION__, func_get_args());
  75. }
  76. // cache dateTime
  77. $this->setRawDateTimeString($dateTime);
  78. // create instance
  79. parent::__construct($dateTime, $this->getDateTimeZone($timezoneString));
  80. // set timezone if unix time
  81. if (strpos($dateTime, '@') !== false)
  82. {
  83. $this->setTimezone($timezoneString);
  84. }
  85. // date validation
  86. if ($this->isValidDate() === false)
  87. {
  88. throw new MomentException('Given date of "' . $dateTime . '" is invalid');
  89. }
  90. return $this;
  91. }
  92. /**
  93. * @param string $timezone
  94. *
  95. * @return \DateTime|Moment
  96. */
  97. public function setTimezone($timezone)
  98. {
  99. if ($this->immutableMode)
  100. {
  101. return $this->implicitCloning(__FUNCTION__, func_get_args());
  102. }
  103. $this->setTimezoneString($timezone);
  104. parent::setTimezone($this->getDateTimeZone($timezone));
  105. return $this;
  106. }
  107. /**
  108. * @param null $format
  109. * @param null|FormatsInterface $formatsInterface
  110. *
  111. * @return string
  112. */
  113. public function format($format = null, $formatsInterface = null)
  114. {
  115. // set default format
  116. if ($format === null)
  117. {
  118. $format = \DateTime::ISO8601;
  119. }
  120. // handle diverse format types
  121. if ($formatsInterface instanceof FormatsInterface)
  122. {
  123. $format = $formatsInterface->format($format);
  124. }
  125. // handle ordinals
  126. if (strpos($format, 'S') !== false)
  127. {
  128. preg_match_all('/(\wS)/', $format, $matches);
  129. if (count($matches) >= 1)
  130. {
  131. foreach ($matches[1] as $part)
  132. {
  133. $token = substr($part, 0, 1);
  134. $number = $this->format($token);
  135. $format = str_replace($part, $this->formatOrdinal($number, $token), $format);
  136. }
  137. }
  138. }
  139. // handle text
  140. if (strpos($format, '[') !== false)
  141. {
  142. preg_match_all('/\[([^\[]*)\]/', $format, $matches);
  143. foreach ($matches[1] as $part)
  144. {
  145. $format = preg_replace('/\[' . $part . '\]/u', preg_replace('/(\w)/u', '\\\\\1', $part), $format);
  146. }
  147. }
  148. // prepare locale formats
  149. $format = MomentLocale::prepareSpecialLocaleTags($format);
  150. // render moment
  151. $format = parent::format($format);
  152. // render locale format
  153. $format = MomentLocale::renderSpecialLocaleTags($format);
  154. return $format;
  155. }
  156. /**
  157. * @param int $seconds
  158. *
  159. * @return Moment
  160. */
  161. public function addSeconds($seconds = 1)
  162. {
  163. return $this->addTime('second', $seconds);
  164. }
  165. /**
  166. * @param int $minutes
  167. *
  168. * @return Moment
  169. */
  170. public function addMinutes($minutes = 1)
  171. {
  172. return $this->addTime('minute', $minutes);
  173. }
  174. /**
  175. * @param int $hours
  176. *
  177. * @return Moment
  178. */
  179. public function addHours($hours = 1)
  180. {
  181. return $this->addTime('hour', $hours);
  182. }
  183. /**
  184. * @param int $days
  185. *
  186. * @return Moment
  187. */
  188. public function addDays($days = 1)
  189. {
  190. return $this->addTime('day', $days);
  191. }
  192. /**
  193. * @param int $weeks
  194. *
  195. * @return Moment
  196. */
  197. public function addWeeks($weeks = 1)
  198. {
  199. return $this->addTime('week', $weeks);
  200. }
  201. /**
  202. * @param int $months
  203. *
  204. * @return Moment
  205. */
  206. public function addMonths($months = 1)
  207. {
  208. return $this->addTime('month', $months);
  209. }
  210. /**
  211. * @param int $years
  212. *
  213. * @return Moment
  214. */
  215. public function addYears($years = 1)
  216. {
  217. return $this->addTime('year', $years);
  218. }
  219. /**
  220. * @param int $seconds
  221. *
  222. * @return Moment
  223. */
  224. public function subtractSeconds($seconds = 1)
  225. {
  226. return $this->subtractTime('second', $seconds);
  227. }
  228. /**
  229. * @param int $minutes
  230. *
  231. * @return Moment
  232. */
  233. public function subtractMinutes($minutes = 1)
  234. {
  235. return $this->subtractTime('minute', $minutes);
  236. }
  237. /**
  238. * @param int $hours
  239. *
  240. * @return Moment
  241. */
  242. public function subtractHours($hours = 1)
  243. {
  244. return $this->subtractTime('hour', $hours);
  245. }
  246. /**
  247. * @param int $days
  248. *
  249. * @return Moment
  250. */
  251. public function subtractDays($days = 1)
  252. {
  253. return $this->subtractTime('day', $days);
  254. }
  255. /**
  256. * @param int $weeks
  257. *
  258. * @return Moment
  259. */
  260. public function subtractWeeks($weeks = 1)
  261. {
  262. return $this->subtractTime('week', $weeks);
  263. }
  264. /**
  265. * @param int $months
  266. *
  267. * @return Moment
  268. */
  269. public function subtractMonths($months = 1)
  270. {
  271. return $this->subtractTime('month', $months);
  272. }
  273. /**
  274. * @param int $years
  275. *
  276. * @return Moment
  277. */
  278. public function subtractYears($years = 1)
  279. {
  280. return $this->subtractTime('year', $years);
  281. }
  282. /**
  283. * @param $day
  284. *
  285. * @return Moment
  286. */
  287. public function setDay($day)
  288. {
  289. if ($this->immutableMode)
  290. {
  291. return $this->implicitCloning(__FUNCTION__, func_get_args());
  292. }
  293. $this->setDate($this->format('Y'), $this->format('m'), $day);
  294. return $this;
  295. }
  296. /**
  297. * @param $month
  298. *
  299. * @return Moment
  300. */
  301. public function setMonth($month)
  302. {
  303. if ($this->immutableMode)
  304. {
  305. return $this->implicitCloning(__FUNCTION__, func_get_args());
  306. }
  307. $this->setDate($this->format('Y'), $month, $this->format('d'));
  308. return $this;
  309. }
  310. /**
  311. * @param $year
  312. *
  313. * @return Moment
  314. */
  315. public function setYear($year)
  316. {
  317. if ($this->immutableMode)
  318. {
  319. return $this->implicitCloning(__FUNCTION__, func_get_args());
  320. }
  321. $this->setDate($year, $this->format('m'), $this->format('d'));
  322. return $this;
  323. }
  324. /**
  325. * @return string
  326. */
  327. public function getDay()
  328. {
  329. return (string)$this->format('d');
  330. }
  331. /**
  332. * @return string
  333. */
  334. public function getWeekday()
  335. {
  336. return (string)$this->format('N');
  337. }
  338. /**
  339. * @return string
  340. */
  341. public function getWeekdayNameLong()
  342. {
  343. return (string)$this->format('l');
  344. }
  345. /**
  346. * @return string
  347. */
  348. public function getWeekdayNameShort()
  349. {
  350. return (string)$this->format('D');
  351. }
  352. /**
  353. * @return string
  354. */
  355. public function getWeekOfYear()
  356. {
  357. return (string)$this->format('W');
  358. }
  359. /**
  360. * @return string
  361. */
  362. public function getMonth()
  363. {
  364. return (string)$this->format('m');
  365. }
  366. /**
  367. * @return string
  368. */
  369. public function getMonthNameLong()
  370. {
  371. return (string)$this->format('F');
  372. }
  373. /**
  374. * @return string
  375. */
  376. public function getMonthNameShort()
  377. {
  378. return (string)$this->format('M');
  379. }
  380. /**
  381. * @return string
  382. */
  383. public function getQuarter()
  384. {
  385. $currentMonth = $this->format('n');
  386. return (string)ceil($currentMonth / 3);
  387. }
  388. /**
  389. * @return string
  390. */
  391. public function getYear()
  392. {
  393. return (string)$this->format('Y');
  394. }
  395. /**
  396. * @param int $year
  397. * @param int $month
  398. * @param int $day
  399. *
  400. * @return self|\DateTime
  401. */
  402. public function setDate($year, $month, $day)
  403. {
  404. if ($this->immutableMode)
  405. {
  406. return $this->implicitCloning(__FUNCTION__, func_get_args());
  407. }
  408. parent::setDate($year, $month, $day);
  409. return $this;
  410. }
  411. /**
  412. * @param $second
  413. *
  414. * @return Moment
  415. */
  416. public function setSecond($second)
  417. {
  418. if ($this->immutableMode)
  419. {
  420. return $this->implicitCloning(__FUNCTION__, func_get_args());
  421. }
  422. $this->setTime($this->format('H'), $this->format('i'), $second);
  423. return $this;
  424. }
  425. /**
  426. * @param $minute
  427. *
  428. * @return Moment
  429. */
  430. public function setMinute($minute)
  431. {
  432. if ($this->immutableMode)
  433. {
  434. return $this->implicitCloning(__FUNCTION__, func_get_args());
  435. }
  436. $this->setTime($this->format('H'), $minute, $this->format('s'));
  437. return $this;
  438. }
  439. /**
  440. * @param $hour
  441. *
  442. * @return Moment
  443. */
  444. public function setHour($hour)
  445. {
  446. if ($this->immutableMode)
  447. {
  448. return $this->implicitCloning(__FUNCTION__, func_get_args());
  449. }
  450. $this->setTime($hour, $this->format('i'), $this->format('s'));
  451. return $this;
  452. }
  453. /**
  454. * @return string
  455. */
  456. public function getSecond()
  457. {
  458. return (string)$this->format('s');
  459. }
  460. /**
  461. * @return string
  462. */
  463. public function getMinute()
  464. {
  465. return (string)$this->format('i');
  466. }
  467. /**
  468. * @return string
  469. */
  470. public function getHour()
  471. {
  472. return (string)$this->format('H');
  473. }
  474. /**
  475. * @param int $hour
  476. * @param int $minute
  477. * @param null $second
  478. *
  479. * @return $this|\DateTime
  480. */
  481. public function setTime($hour, $minute, $second = null)
  482. {
  483. if ($this->immutableMode)
  484. {
  485. return $this->implicitCloning(__FUNCTION__, func_get_args());
  486. }
  487. parent::setTime($hour, $minute, $second);
  488. return $this;
  489. }
  490. /**
  491. * @param string|Moment $fromMoment
  492. * @param null $timezoneString
  493. *
  494. * @return MomentFromVo
  495. */
  496. public function from($fromMoment = 'now', $timezoneString = null)
  497. {
  498. // create moment first
  499. if ($this->isMoment($fromMoment) === false)
  500. {
  501. // use custom timezone or fallback the current moment
  502. $useTimezoneString = $timezoneString !== null ? $timezoneString : $this->getTimezoneString();
  503. $fromMoment = new Moment($fromMoment, $useTimezoneString);
  504. }
  505. // calc difference between dates
  506. $dateDiff = parent::diff($fromMoment);
  507. $momentFromVo = new MomentFromVo($fromMoment);
  508. return $momentFromVo
  509. ->setDirection($dateDiff->format('%R'))
  510. ->setSeconds($this->fromToSeconds($dateDiff))
  511. ->setMinutes($this->fromToMinutes($dateDiff))
  512. ->setHours($this->fromToHours($dateDiff))
  513. ->setDays($this->fromToDays($dateDiff))
  514. ->setWeeks($this->fromToWeeks($dateDiff));
  515. }
  516. /**
  517. * @param null $timezoneString
  518. *
  519. * @return MomentFromVo
  520. */
  521. public function fromNow($timezoneString = null)
  522. {
  523. // use custom timezone or fallback the current moment
  524. $useTimezoneString = $timezoneString !== null ? $timezoneString : $this->getTimezoneString();
  525. return $this->from('now', $useTimezoneString);
  526. }
  527. /**
  528. * @param $input
  529. *
  530. * @return bool
  531. */
  532. public function isMoment($input)
  533. {
  534. return $input instanceof Moment;
  535. }
  536. /**
  537. * @param string $type
  538. * @param int $value
  539. *
  540. * @return Moment
  541. */
  542. private function addTime($type = 'day', $value = 1)
  543. {
  544. if ($this->immutableMode)
  545. {
  546. return $this->implicitCloning(__FUNCTION__, func_get_args());
  547. }
  548. parent::modify('+' . $value . ' ' . $type);
  549. return $this;
  550. }
  551. /**
  552. * @param \DateInterval $dateInterval
  553. *
  554. * @return string
  555. */
  556. private function fromToSeconds(\DateInterval $dateInterval)
  557. {
  558. return
  559. ($dateInterval->days * 24 * 60 * 60)
  560. + ($dateInterval->h * 60 * 60)
  561. + ($dateInterval->i * 60)
  562. + $dateInterval->s;
  563. }
  564. /**
  565. * @param \DateInterval $dateInterval
  566. *
  567. * @return string
  568. */
  569. private function fromToMinutes(\DateInterval $dateInterval)
  570. {
  571. return $this->fromToSeconds($dateInterval) / 60;
  572. }
  573. /**
  574. * @param \DateInterval $dateInterval
  575. *
  576. * @return string
  577. */
  578. private function fromToHours(\DateInterval $dateInterval)
  579. {
  580. return $this->fromToMinutes($dateInterval) / 60;
  581. }
  582. /**
  583. * @param \DateInterval $dateInterval
  584. *
  585. * @return string
  586. */
  587. private function fromToDays(\DateInterval $dateInterval)
  588. {
  589. return $this->fromToHours($dateInterval) / 24;
  590. }
  591. /**
  592. * @param \DateInterval $dateInterval
  593. *
  594. * @return string
  595. */
  596. private function fromToWeeks(\DateInterval $dateInterval)
  597. {
  598. return $this->fromToDays($dateInterval) / 7;
  599. }
  600. /**
  601. * @param $period
  602. *
  603. * @return MomentPeriodVo
  604. * @throws MomentException
  605. */
  606. public function getPeriod($period)
  607. {
  608. switch ($period)
  609. {
  610. case 'week':
  611. $interval = $this->format('W');
  612. $start = new Moment('@' . $this->format('U'));
  613. $start->setTimezone($this->getTimezoneString())
  614. ->subtractDays($this->getDaysAfterStartOfWeek())
  615. ->setTime(0, 0, 0);
  616. $end = new Moment('@' . $this->format('U'));
  617. $end->setTimezone($this->getTimezoneString())
  618. ->addDays(6 - $this->getDaysAfterStartOfWeek())
  619. ->setTime(23, 59, 59);
  620. break;
  621. // ------------------------------
  622. case 'month':
  623. $maxMonthDays = $this->format('t');
  624. $currentMonthDay = $this->format('j');
  625. $interval = $this->getMonth();
  626. $start = new Moment('@' . $this->format('U'));
  627. $start->setTimezone($this->getTimezoneString())
  628. ->subtractDays($currentMonthDay - 1)
  629. ->setTime(0, 0, 0);
  630. $end = new Moment('@' . $this->format('U'));
  631. $end->setTimezone($this->getTimezoneString())
  632. ->addDays($maxMonthDays - $currentMonthDay)
  633. ->setTime(23, 59, 59);
  634. break;
  635. // ------------------------------
  636. case 'quarter':
  637. $quarter = $this->getQuarter();
  638. $momentPeriodVo = MomentHelper::getQuarterPeriod($quarter, $this->getYear(), $this->getTimezoneString());
  639. $start = $momentPeriodVo->getStartDate();
  640. $end = $momentPeriodVo->getEndDate();
  641. $interval = $quarter;
  642. break;
  643. // ------------------------------
  644. default:
  645. throw new MomentException("Period \"{$period}\" is not supported. Supported: \"week\", \"month\", \"quarter\".");
  646. }
  647. $momentPeriodVo = new MomentPeriodVo();
  648. return $momentPeriodVo
  649. ->setRefDate($this)
  650. ->setInterval($interval)
  651. ->setStartDate($start)
  652. ->setEndDate($end);
  653. }
  654. /**
  655. * @param bool $withTime
  656. * @param Moment $refMoment
  657. *
  658. * @return string
  659. */
  660. public function calendar($withTime = true, Moment $refMoment = null)
  661. {
  662. $refMoment = $refMoment ? $refMoment : new Moment('now', $this->getTimezoneString());
  663. $momentFromVo = $this->cloning()->startOf('day')->from($refMoment->startOf('day'));
  664. $diff = $momentFromVo->getDays();
  665. // handle time string
  666. $renderedTimeString = MomentLocale::renderLocaleString(array('calendar', 'withTime'), array($this));
  667. $addTime = false;
  668. // apply cases
  669. if ($diff > 7)
  670. {
  671. $localeKeys = array('calendar', 'default');
  672. }
  673. elseif ($diff > 1)
  674. {
  675. $localeKeys = array('calendar', 'lastWeek');
  676. $addTime = true;
  677. }
  678. elseif ($diff > 0)
  679. {
  680. $localeKeys = array('calendar', 'lastDay');
  681. $addTime = true;
  682. }
  683. elseif ($diff == 0)
  684. {
  685. $localeKeys = array('calendar', 'sameDay');
  686. $addTime = true;
  687. }
  688. elseif ($diff == -1)
  689. {
  690. $localeKeys = array('calendar', 'nextDay');
  691. $addTime = true;
  692. }
  693. elseif ($diff > -7)
  694. {
  695. $localeKeys = array('calendar', 'sameElse');
  696. $addTime = true;
  697. }
  698. else
  699. {
  700. $localeKeys = array('calendar', 'default');
  701. }
  702. // render format
  703. $format = MomentLocale::renderLocaleString($localeKeys, array($this));
  704. // add time if valid
  705. if ($addTime && $withTime === true)
  706. {
  707. $format .= ' ' . $renderedTimeString;
  708. }
  709. return $this->format($format);
  710. }
  711. /**
  712. * @param $period
  713. *
  714. * @return Moment
  715. */
  716. public function startOf($period)
  717. {
  718. switch ($period)
  719. {
  720. // set to now, but with 0 seconds
  721. case 'minute':
  722. return $this->setTime($this->getHour(), $this->getMinute(), 0);
  723. break;
  724. // set to now, but with 0 mins, 0 secs
  725. case 'hour':
  726. return $this->setTime($this->getHour(), 0, 0);
  727. break;
  728. // set to 00:00:00 today
  729. case 'day':
  730. return $this->setTime(0, 0, 0);
  731. break;
  732. // set to the first day of this week, 00:00:00
  733. case 'week':
  734. return $this->resetDateTime(
  735. $this->getPeriod('week')->getStartDate()->format('c')
  736. );
  737. break;
  738. // set to the beginning of the current quarter, 1st day of months, 00:00:00
  739. case 'quarter':
  740. return $this->resetDateTime(
  741. $this->getPeriod('quarter')->getStartDate()->format('c')
  742. );
  743. break;
  744. // set to the first of this month, 00:00:00
  745. case 'month':
  746. return $this->resetDateTime(
  747. $this->getPeriod('month')->getStartDate()->format('c')
  748. );
  749. break;
  750. // set to January 1st, 00:00:00 this year
  751. case 'year':
  752. return $this->setDate($this->getYear(), 1, 1)->setTime(0, 0, 0);
  753. break;
  754. default:
  755. return $this;
  756. }
  757. }
  758. /**
  759. * @param $period
  760. *
  761. * @return Moment
  762. */
  763. public function endOf($period)
  764. {
  765. switch ($period)
  766. {
  767. // set to now, but with 59 seconds
  768. case 'minute':
  769. return $this->setTime($this->getHour(), $this->getMinute(), 59);
  770. break;
  771. // set to now, but with 59 mins, 59 secs
  772. case 'hour':
  773. return $this->setTime($this->getHour(), 59, 59);
  774. break;
  775. // set to 23:59:59 today
  776. case 'day':
  777. return $this->setTime(23, 59, 59);
  778. break;
  779. // set to the last day of this week, 23:59
  780. case 'week':
  781. return $this->resetDateTime(
  782. $this->getPeriod('week')->getEndDate()->format('c')
  783. );
  784. break;
  785. // set to the end of the current quarter, last day of months, 23:59:59
  786. case 'quarter':
  787. return $this->resetDateTime(
  788. $this->getPeriod('quarter')->getEndDate()->format('c')
  789. );
  790. break;
  791. // set to the last of this month, 23:59:59
  792. case 'month':
  793. return $this->resetDateTime(
  794. $this->getPeriod('month')->getEndDate()->format('c')
  795. );
  796. break;
  797. // set to January 1st, 23:59:59 this year
  798. case 'year':
  799. return $this->setDate($this->getYear(), 12, 31)->setTime(23, 59, 59);
  800. break;
  801. default:
  802. return $this;
  803. }
  804. }
  805. /**
  806. * @return Moment
  807. */
  808. public function cloning()
  809. {
  810. return clone($this);
  811. }
  812. /**
  813. * @param string $method
  814. * @param array $params
  815. *
  816. * @return self
  817. */
  818. private function implicitCloning($method, $params = array())
  819. {
  820. $clone = $this->cloning();
  821. $clone->setImmutableMode(false);
  822. $retval = call_user_func_array(array($clone, $method), $params);
  823. $clone->setImmutableMode(true);
  824. return is_null($retval) ? $clone : $retval;
  825. }
  826. /**
  827. * @param array $weekdayNumbers
  828. * @param int $forUpcomingWeeks
  829. *
  830. * @return Moment[]
  831. */
  832. public function getMomentsByWeekdays(array $weekdayNumbers, $forUpcomingWeeks = 1)
  833. {
  834. /** @var Moment[] $moments */
  835. $dates = array();
  836. // get today's week day number
  837. $todayWeekday = $this->getWeekday();
  838. // generate for upcoming weeks
  839. for ($w = 1; $w <= $forUpcomingWeeks; $w++)
  840. {
  841. for ($d = 1; $d <= 7; $d++)
  842. {
  843. if (in_array($d, $weekdayNumbers) && ($w > 1 || $d > $todayWeekday))
  844. {
  845. // calculate add days from today's perspective
  846. $addDays = $w === 1 ? $d - $todayWeekday : ($d - $todayWeekday) + ($w * 7 - 7);
  847. // set date
  848. $dates[] = $this->cloning()->addDays($addDays);
  849. }
  850. }
  851. }
  852. return $dates;
  853. }
  854. /**
  855. * Returns copy of Moment normalized to UTC timezone
  856. *
  857. * @return Moment
  858. */
  859. public function toUTC()
  860. {
  861. return $this->cloning()->setTimezone('UTC');
  862. }
  863. /**
  864. * Check if a moment is the same as another moment
  865. *
  866. * @param string|Moment $dateTime
  867. * @param string $period 'seconds|minute|hour|day|month|year'
  868. *
  869. * @return boolean
  870. */
  871. public function isSame($dateTime, $period = 'seconds')
  872. {
  873. $dateTime = $this->isMoment($dateTime) ? $dateTime : new Moment($dateTime);
  874. return (bool)($this->toUTC()->startOf($period)->getTimestamp() === $dateTime->toUTC()->startOf($period)->getTimestamp());
  875. }
  876. /**
  877. * Checks if Moment is before given time
  878. *
  879. * @param string|Moment $dateTime
  880. * @param string $period 'seconds|minute|hour|day|month|year'
  881. *
  882. * @return boolean
  883. */
  884. public function isBefore($dateTime, $period = 'seconds')
  885. {
  886. $dateTime = $this->isMoment($dateTime) ? $dateTime : new Moment($dateTime);
  887. return (bool)($this->toUTC()->startOf($period)->getTimestamp() < $dateTime->toUTC()->startOf($period)->getTimestamp());
  888. }
  889. /**
  890. * Checks if Moment is after given time
  891. *
  892. * @param string|Moment $dateTime
  893. * @param string $period 'seconds|minute|hour|day|month|year'
  894. *
  895. * @return bool
  896. */
  897. public function isAfter($dateTime, $period = 'seconds')
  898. {
  899. $dateTime = $this->isMoment($dateTime) ? $dateTime : new Moment($dateTime);
  900. return $dateTime->isBefore($this, $period);
  901. }
  902. /**
  903. * Checks if Moment is between given time range
  904. *
  905. * @param string|Moment $minDateTime
  906. * @param string|Moment $maxDateTime
  907. * @param boolean $closed
  908. * @param string $period 'seconds|minute|hour|day|month|year'
  909. *
  910. * @return bool
  911. */
  912. public function isBetween($minDateTime, $maxDateTime, $closed = true, $period = 'seconds')
  913. {
  914. $isBefore = $this->isBefore($minDateTime, $period);
  915. $isAfter = $this->isAfter($maxDateTime, $period);
  916. // include endpoints
  917. if ($closed === true)
  918. {
  919. return $isBefore === false && $isAfter === false;
  920. }
  921. return $isBefore === true && $isAfter === true;
  922. }
  923. /**
  924. * @param string $rawDateTimeString
  925. *
  926. * @return Moment
  927. */
  928. private function setRawDateTimeString($rawDateTimeString)
  929. {
  930. if ($this->immutableMode)
  931. {
  932. return $this->implicitCloning(__FUNCTION__, func_get_args());
  933. }
  934. $this->rawDateTimeString = $rawDateTimeString;
  935. return $this;
  936. }
  937. /**
  938. * @return string
  939. */
  940. private function getRawDateTimeString()
  941. {
  942. return $this->rawDateTimeString;
  943. }
  944. /**
  945. * @param string $timezoneString
  946. *
  947. * @return Moment
  948. */
  949. private function setTimezoneString($timezoneString)
  950. {
  951. if ($this->immutableMode)
  952. {
  953. return $this->implicitCloning(__FUNCTION__, func_get_args());
  954. }
  955. $this->timezoneString = $timezoneString;
  956. return $this;
  957. }
  958. /**
  959. * @return string
  960. */
  961. private function getTimezoneString()
  962. {
  963. return $this->timezoneString;
  964. }
  965. /**
  966. * @param string $timezoneString
  967. *
  968. * @return \DateTimeZone
  969. */
  970. private function getDateTimeZone($timezoneString)
  971. {
  972. // cache timezone
  973. $this->setTimezoneString($timezoneString);
  974. return new \DateTimeZone($timezoneString);
  975. }
  976. /**
  977. * @return int
  978. */
  979. private function getDaysAfterStartOfWeek()
  980. {
  981. $dow = MomentLocale::getLocaleString(array('week', 'dow')) % 7;
  982. $currentWeekDay = (int)$this->getWeekday();
  983. $distance = (7 - $dow + $currentWeekDay) % 7;
  984. return $distance;
  985. }
  986. /**
  987. * @return bool
  988. */
  989. private function isValidDate()
  990. {
  991. $rawDateTime = $this->getRawDateTimeString();
  992. if (strpos($rawDateTime, '-') === false)
  993. {
  994. return true;
  995. }
  996. // ----------------------------------
  997. // time with indicator "T"
  998. if (strpos($rawDateTime, 'T') !== false)
  999. {
  1000. // remove fraction if any
  1001. $rawDateTime = preg_replace('/\.[0-9][0-9][0-9]/', '', $rawDateTime);
  1002. // get timezone if any
  1003. $rawTimeZone = substr($rawDateTime, 19);
  1004. // timezone w/ difference in hours: e.g. +0200
  1005. if (empty($rawTimeZone) === false)
  1006. {
  1007. if (strpos($rawTimeZone, '+') !== false || strpos($rawTimeZone, '-') !== false)
  1008. {
  1009. // with colon: +-HH:MM
  1010. if (substr_count($rawTimeZone, ':') > 0)
  1011. {
  1012. $momentDateTime = $this->format('Y-m-d\TH:i:sP');
  1013. }
  1014. // without colon: +-HHMM
  1015. else
  1016. {
  1017. $momentDateTime = $this->format('Y-m-d\TH:i:sO');
  1018. }
  1019. }
  1020. // timezone with name: e.g. UTC
  1021. else
  1022. {
  1023. $momentDateTime = $this->format('Y-m-d\TH:i:se');
  1024. }
  1025. }
  1026. // no timezone specified
  1027. else
  1028. {
  1029. $momentDateTime = $this->format('Y-m-d\TH:i:s');
  1030. }
  1031. }
  1032. // time without indicator "T"
  1033. elseif (strpos($rawDateTime, ':') !== false)
  1034. {
  1035. // with seconds
  1036. if (substr_count($rawDateTime, ':') === 2)
  1037. {
  1038. $momentDateTime = $this->format('Y-m-d H:i:s');
  1039. }
  1040. else
  1041. {
  1042. $momentDateTime = $this->format('Y-m-d H:i');
  1043. }
  1044. }
  1045. // without time
  1046. else
  1047. {
  1048. $momentDateTime = $this->format('Y-m-d');
  1049. }
  1050. return $rawDateTime === $momentDateTime;
  1051. }
  1052. /**
  1053. * @param string $type
  1054. * @param int $value
  1055. *
  1056. * @return Moment
  1057. */
  1058. private function subtractTime($type = 'day', $value = 1)
  1059. {
  1060. if ($this->immutableMode)
  1061. {
  1062. return $this->implicitCloning(__FUNCTION__, func_get_args());
  1063. }
  1064. parent::modify('-' . $value . ' ' . $type);
  1065. return $this;
  1066. }
  1067. /**
  1068. * @param int $number
  1069. * @param string $token
  1070. *
  1071. * @return string
  1072. */
  1073. private function formatOrdinal($number, $token)
  1074. {
  1075. return (string)call_user_func(MomentLocale::getLocaleString(array('ordinal')), $number, $token);
  1076. }
  1077. }