DateTimeHelper.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace fphammerle\helpers;
  3. class DateTimeHelper
  4. {
  5. /**
  6. * @param integer|null $timestamp unix timestamp
  7. * @return \DateTime|null
  8. */
  9. public static function timestampToDateTime($timestamp)
  10. {
  11. if($timestamp === null) {
  12. return null;
  13. } elseif(is_int($timestamp)) {
  14. $dt = new \DateTime();
  15. $dt->setTimestamp($timestamp);
  16. return $dt;
  17. } else {
  18. throw new \InvalidArgumentException('expected integer or null');
  19. }
  20. }
  21. /**
  22. * @param string|null $text
  23. * @return \DatePeriod|null
  24. */
  25. public static function parse($text)
  26. {
  27. if($text === null) {
  28. return null;
  29. } else {
  30. if(preg_match(
  31. '/^(?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2})'
  32. .'([ T](?P<h>\d{2}):(?P<i>\d{2}):(?P<s>\d{2}))?'
  33. . '(Z|[\+-]\d{2}:\d{2})?$/',
  34. $text,
  35. $attr
  36. )) {
  37. $start = new \DateTime($attr[0]);
  38. $start->setDate($attr['y'], $attr['m'], $attr['d']);
  39. $start->setTime(
  40. empty($attr['h']) ? 0 : $attr['h'],
  41. empty($attr['i']) ? 0 : $attr['i'],
  42. empty($attr['s']) ? 0 : $attr['s']
  43. );
  44. if(!empty($attr['h'])) {
  45. $interval = new \DateInterval('PT1S');
  46. } else {
  47. $interval = new \DateInterval('P1D');
  48. }
  49. return new \DatePeriod($start, $interval, 0);
  50. } else {
  51. throw new \InvalidArgumentException(
  52. sprintf("could not parse string '%s'", $text)
  53. );
  54. }
  55. }
  56. }
  57. /**
  58. * @param string|null $text
  59. * @return \DateTime|null
  60. */
  61. public static function parseGetStart($text)
  62. {
  63. $period = self::parse($text);
  64. if($period) {
  65. return $period->start;
  66. } else {
  67. return null;
  68. }
  69. }
  70. public static function deinvertInterval(\DateInterval $source = null)
  71. {
  72. // \DateInterval does not implement clone.
  73. // @see https://bugs.php.net/bug.php?id=50559
  74. $result = unserialize(serialize($source));
  75. if($result->invert) {
  76. $result->y *= -1;
  77. $result->m *= -1;
  78. $result->d *= -1;
  79. $result->h *= -1;
  80. $result->i *= -1;
  81. $result->s *= -1;
  82. $result->invert = 0;
  83. }
  84. return $result;
  85. }
  86. /**
  87. * @param \DateInterval|null $i
  88. * @return string|null
  89. */
  90. public static function intervalToIso(\DateInterval $i = null)
  91. {
  92. if(is_null($i)) {
  93. return null;
  94. } else {
  95. $i = self::deinvertInterval($i);
  96. if($i->y < 0 || $i->m < 0 || $i->d < 0 || $i->h < 0 || $i->i < 0 || $i->s < 0) {
  97. throw new \Exception('negative intervals are not supported');
  98. } else {
  99. return $i->format('P%yY%mM%dDT%hH%iM%sS');
  100. }
  101. }
  102. }
  103. /**
  104. * @param \DatePeriod|null $p
  105. * @return string|null
  106. */
  107. public static function periodToIso(\DatePeriod $p = null)
  108. {
  109. if(is_null($p)) {
  110. return null;
  111. } else {
  112. // Cave:
  113. // (new \DatePeriod(
  114. // new \DateTime('2016-08-05T14:50:14Z'),
  115. // new \DateInterval('P1D'),
  116. // -1
  117. // )->recurrences == 1
  118. if($p->recurrences <= 0) {
  119. throw new \Exception(
  120. 'conversion of periods with number of occurances'
  121. . ' being negative is not supported'
  122. );
  123. }
  124. $repetitions = -1;
  125. foreach($p as $dt) {
  126. $repetitions++;
  127. // printf("%d. %s\n", $repetitions, $dt->format(\DateTime::ATOM));
  128. }
  129. switch($repetitions) {
  130. case -1:
  131. // no valid date within period
  132. // e.g. new \DatePeriod(
  133. // new \DateTime('2016-08-05T14:50:14+08:00'),
  134. // new \DateInterval('PT1S'),
  135. // new \DateTime('2016-08-05T14:50:14+08:00')
  136. // )
  137. throw new \InvalidArgumentException(
  138. 'given period does not contain any valid date'
  139. );
  140. case 0:
  141. return sprintf(
  142. '%s/%s',
  143. $p->getStartDate()->format(\DateTime::ATOM),
  144. self::intervalToIso($p->getDateInterval())
  145. );
  146. default:
  147. return sprintf(
  148. 'R%d/%s/%s',
  149. $repetitions,
  150. $p->getStartDate()->format(\DateTime::ATOM),
  151. self::intervalToIso($p->getDateInterval())
  152. );
  153. }
  154. }
  155. }
  156. }