ModifyTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\CarbonImmutable;
  12. use Carbon\CarbonImmutable as Carbon;
  13. use Tests\AbstractTestCase;
  14. class ModifyTest extends AbstractTestCase
  15. {
  16. public function testSimpleModify()
  17. {
  18. $a = new Carbon('2014-03-30 00:00:00');
  19. $b = $a->addHours(24);
  20. $this->assertSame(24, $a->diffInHours($b));
  21. }
  22. /**
  23. * @requires PHP >= 8.0
  24. */
  25. public function testSimpleModifyWithNamedParameter()
  26. {
  27. $a = new Carbon('2014-03-30 00:00:00');
  28. $b = eval('return $a->addHours(value: 24);');
  29. $this->assertSame(24, $a->diffInHours($b));
  30. }
  31. public function testTimezoneModify()
  32. {
  33. // For daylight saving time reason 2014-03-30 0h59 is immediately followed by 2h00
  34. $a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
  35. $b = $a->addHours(24);
  36. $this->assertSame(24, $a->diffInHours($b));
  37. $this->assertSame(24, $a->diffInHours($b, false));
  38. $this->assertSame(24, $b->diffInHours($a));
  39. $this->assertSame(-24, $b->diffInHours($a, false));
  40. $this->assertSame(-23, $b->diffInRealHours($a, false));
  41. $this->assertSame(-23 * 60, $b->diffInRealMinutes($a, false));
  42. $this->assertSame(-23 * 60 * 60, $b->diffInRealSeconds($a, false));
  43. $a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
  44. $b = $a->addRealHours(24);
  45. $this->assertSame(-24, $b->diffInRealHours($a, false));
  46. $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
  47. $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
  48. $this->assertSame(-25, $b->diffInHours($a, false));
  49. $b = $b->subRealHours(24);
  50. $this->assertSame(0, $b->diffInRealHours($a, false));
  51. $this->assertSame(0, $b->diffInHours($a, false));
  52. $a = new Carbon('2014-03-30 00:59:00', 'Europe/London');
  53. $a = $a->addRealHour();
  54. $this->assertSame('02:59', $a->format('H:i'));
  55. $a = $a->subRealHour();
  56. $this->assertSame('00:59', $a->format('H:i'));
  57. $a = new Carbon('2014-03-30 00:59:00', 'Europe/London');
  58. $a = $a->addRealMinutes(2);
  59. $this->assertSame('02:01', $a->format('H:i'));
  60. $a = $a->subRealMinutes(2);
  61. $this->assertSame('00:59', $a->format('H:i'));
  62. $a = new Carbon('2014-03-30 00:59:30', 'Europe/London');
  63. $a = $a->addRealMinute();
  64. $this->assertSame('02:00:30', $a->format('H:i:s'));
  65. $a = $a->subRealMinute();
  66. $this->assertSame('00:59:30', $a->format('H:i:s'));
  67. $a = new Carbon('2014-03-30 00:59:30', 'Europe/London');
  68. $a = $a->addRealSeconds(40);
  69. $this->assertSame('02:00:10', $a->format('H:i:s'));
  70. $a = $a->subRealSeconds(40);
  71. $this->assertSame('00:59:30', $a->format('H:i:s'));
  72. $a = new Carbon('2014-03-30 00:59:59', 'Europe/London');
  73. $a = $a->addRealSecond();
  74. $this->assertSame('02:00:00', $a->format('H:i:s'));
  75. $a = $a->subRealSecond();
  76. $this->assertSame('00:59:59', $a->format('H:i:s'));
  77. $a = new Carbon('2014-03-30 00:59:59.999990', 'Europe/London');
  78. $a = $a->addRealMicroseconds(20);
  79. $this->assertSame('02:00:00.000010', $a->format('H:i:s.u'));
  80. $a = $a->subRealMicroseconds(20);
  81. $this->assertSame('00:59:59.999990', $a->format('H:i:s.u'));
  82. $a = new Carbon('2014-03-30 00:59:59.999999', 'Europe/London');
  83. $a = $a->addRealMicrosecond();
  84. $this->assertSame('02:00:00.000000', $a->format('H:i:s.u'));
  85. $a = $a->subRealMicrosecond();
  86. $this->assertSame('00:59:59.999999', $a->format('H:i:s.u'));
  87. $a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
  88. $b = $a->addRealDay();
  89. $this->assertSame(-24, $b->diffInRealHours($a, false));
  90. $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
  91. $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
  92. $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
  93. $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
  94. $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
  95. $this->assertSame(-25, $b->diffInHours($a, false));
  96. $a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
  97. $b = $a->addRealWeeks(1 / 7);
  98. $this->assertSame(-24, $b->diffInRealHours($a, false));
  99. $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
  100. $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
  101. $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
  102. $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
  103. $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
  104. $this->assertSame(-25, $b->diffInHours($a, false));
  105. $a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
  106. $b = $a->addRealMonths(1 / 30);
  107. $this->assertSame(-24, $b->diffInRealHours($a, false));
  108. $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
  109. $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
  110. $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
  111. $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
  112. $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
  113. $this->assertSame(-25, $b->diffInHours($a, false));
  114. $a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
  115. $b = $a->addRealQuarters(1 / 90);
  116. $this->assertSame(-24, $b->diffInRealHours($a, false));
  117. $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
  118. $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
  119. $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
  120. $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
  121. $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
  122. $this->assertSame(-25, $b->diffInHours($a, false));
  123. $a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
  124. $b = $a->addRealYears(1 / 365);
  125. $this->assertSame(-24, $b->diffInRealHours($a, false));
  126. $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
  127. $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
  128. $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
  129. $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
  130. $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
  131. $this->assertSame(-25, $b->diffInHours($a, false));
  132. $a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
  133. $b = $a->addRealDecades(1 / 3650);
  134. $this->assertSame(-24, $b->diffInRealHours($a, false));
  135. $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
  136. $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
  137. $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
  138. $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
  139. $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
  140. $this->assertSame(-25, $b->diffInHours($a, false));
  141. $a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
  142. $b = $a->addRealCenturies(1 / 36500);
  143. $this->assertSame(-24, $b->diffInRealHours($a, false));
  144. $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
  145. $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
  146. $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
  147. $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
  148. $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
  149. $this->assertSame(-25, $b->diffInHours($a, false));
  150. $a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
  151. $b = $a->addRealMillennia(1 / 365000);
  152. $this->assertSame(-24, $b->diffInRealHours($a, false));
  153. $this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
  154. $this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
  155. $this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
  156. $this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
  157. $this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
  158. $this->assertSame(-25, $b->diffInHours($a, false));
  159. }
  160. public function testNextAndPrevious()
  161. {
  162. Carbon::setTestNow('2019-06-02 13:27:09.816752');
  163. $this->assertSame('2019-06-02 14:00:00', Carbon::now()->next('2pm')->format('Y-m-d H:i:s'));
  164. $this->assertSame('2019-06-01 14:00:00', Carbon::now()->previous('2pm')->format('Y-m-d H:i:s'));
  165. $this->assertSame('2019-06-02 14:00:00', Carbon::now()->next('14h')->format('Y-m-d H:i:s'));
  166. $this->assertSame('2019-06-01 14:00:00', Carbon::now()->previous('14h')->format('Y-m-d H:i:s'));
  167. $this->assertSame('2019-06-03 09:00:00', Carbon::now()->next('9am')->format('Y-m-d H:i:s'));
  168. $this->assertSame('2019-06-02 09:00:00', Carbon::now()->previous('9am')->format('Y-m-d H:i:s'));
  169. $this->assertSame('2019-06-02 14:00:00', Carbon::parse('next 2pm')->format('Y-m-d H:i:s'));
  170. $this->assertSame('2019-06-01 14:00:00', Carbon::parse('previous 2pm')->format('Y-m-d H:i:s'));
  171. $this->assertSame('2019-06-02 14:00:00', Carbon::parse('next 14h')->format('Y-m-d H:i:s'));
  172. $this->assertSame('2019-06-01 14:00:00', Carbon::parse('previous 14h')->format('Y-m-d H:i:s'));
  173. $this->assertSame('2019-06-03 09:00:00', Carbon::parse('next 9am')->format('Y-m-d H:i:s'));
  174. $this->assertSame('2019-06-02 09:00:00', Carbon::parse('previous 9am')->format('Y-m-d H:i:s'));
  175. }
  176. }