RoundTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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\Carbon;
  12. use Carbon\Carbon;
  13. use Carbon\CarbonInterval;
  14. use DateInterval;
  15. use InvalidArgumentException;
  16. use Tests\AbstractTestCase;
  17. class RoundTest extends AbstractTestCase
  18. {
  19. public function testRoundWithDefaultUnit()
  20. {
  21. $dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
  22. $copy = $dt->copy();
  23. $ref = $copy->round();
  24. $this->assertSame($ref, $copy);
  25. $this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
  26. $this->assertCarbon($dt->copy()->round(5), 2315, 7, 18, 22, 42, 20, 0);
  27. $this->assertCarbon($dt->copy()->floor()->round(5), 2315, 7, 18, 22, 42, 15, 0);
  28. $this->assertCarbon($dt->copy()->round(3), 2315, 7, 18, 22, 42, 18, 0);
  29. $this->assertCarbon($dt->copy()->round(4), 2315, 7, 18, 22, 42, 16, 0);
  30. $this->assertCarbon($dt->copy()->round(10), 2315, 7, 18, 22, 42, 20, 0);
  31. $this->assertCarbon($dt->copy()->round(0.5), 2315, 7, 18, 22, 42, 17, 500000);
  32. $this->assertCarbon($dt->copy()->round(0.25), 2315, 7, 18, 22, 42, 17, 750000);
  33. $this->assertCarbon($dt->copy()->round(3.8), 2315, 7, 18, 22, 42, 19, 800000);
  34. $this->assertCarbon($dt->copy()->floor(5), 2315, 7, 18, 22, 42, 15, 0);
  35. $this->assertCarbon($dt->copy()->floor()->floor(5), 2315, 7, 18, 22, 42, 15, 0);
  36. $this->assertCarbon($dt->copy()->floor(3), 2315, 7, 18, 22, 42, 15, 0);
  37. $this->assertCarbon($dt->copy()->floor(4), 2315, 7, 18, 22, 42, 16, 0);
  38. $this->assertCarbon($dt->copy()->floor(10), 2315, 7, 18, 22, 42, 10, 0);
  39. $this->assertCarbon($dt->copy()->floor(0.5), 2315, 7, 18, 22, 42, 17, 500000);
  40. $this->assertCarbon($dt->copy()->floor(0.25), 2315, 7, 18, 22, 42, 17, 500000);
  41. $this->assertCarbon($dt->copy()->floor(3.8), 2315, 7, 18, 22, 42, 15, 0);
  42. $this->assertCarbon($dt->copy()->ceil(5), 2315, 7, 18, 22, 42, 20, 0);
  43. $this->assertCarbon($dt->copy()->floor()->ceil(5), 2315, 7, 18, 22, 42, 20, 0);
  44. $this->assertCarbon($dt->copy()->ceil(3), 2315, 7, 18, 22, 42, 18, 0);
  45. $this->assertCarbon($dt->copy()->ceil(4), 2315, 7, 18, 22, 42, 20, 0);
  46. $this->assertCarbon($dt->copy()->ceil(10), 2315, 7, 18, 22, 42, 20, 0);
  47. $this->assertCarbon($dt->copy()->ceil(0.5), 2315, 7, 18, 22, 42, 18, 0);
  48. $this->assertCarbon($dt->copy()->ceil(0.25), 2315, 7, 18, 22, 42, 17, 750000);
  49. $this->assertCarbon($dt->copy()->ceil(3.8), 2315, 7, 18, 22, 42, 19, 800000);
  50. }
  51. public function testRoundWithStrings()
  52. {
  53. $dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
  54. $this->assertCarbon($dt->copy()->round('minute'), 2315, 7, 18, 22, 42, 0, 0);
  55. $this->assertCarbon($dt->copy()->floor('5 minutes'), 2315, 7, 18, 22, 40, 0, 0);
  56. $this->assertCarbon($dt->copy()->ceil('5 minutes'), 2315, 7, 18, 22, 45, 0, 0);
  57. }
  58. public function testRoundWithStringsException()
  59. {
  60. $this->expectExceptionObject(new InvalidArgumentException(
  61. 'Rounding is only possible with single unit intervals.'
  62. ));
  63. Carbon::create(2315, 7, 18, 22, 42, 17.643971)->round('2 hours 5 minutes');
  64. }
  65. public function testRoundWithInterval()
  66. {
  67. $dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
  68. $this->assertCarbon($dt->copy()->round(CarbonInterval::minute()), 2315, 7, 18, 22, 42, 0, 0);
  69. $this->assertCarbon($dt->copy()->floor(CarbonInterval::minutes(5)), 2315, 7, 18, 22, 40, 0, 0);
  70. $this->assertCarbon($dt->copy()->ceil(new DateInterval('PT5M')), 2315, 7, 18, 22, 45, 0, 0);
  71. }
  72. public function testRoundWithIntervalException()
  73. {
  74. $this->expectExceptionObject(new InvalidArgumentException(
  75. 'Rounding is only possible with single unit intervals.'
  76. ));
  77. Carbon::create(2315, 7, 18, 22, 42, 17.643971)->round(CarbonInterval::day()->minutes(5));
  78. }
  79. public function testRoundWithBaseUnit()
  80. {
  81. $dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
  82. $copy = $dt->copy();
  83. $ref = $copy->roundSecond();
  84. $this->assertSame($ref, $copy);
  85. $this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
  86. $this->assertCarbon($dt->copy()->roundDay(), 2315, 7, 19, 0, 0, 0, 0);
  87. $this->assertCarbon($dt->copy()->roundDay(5), 2315, 7, 21, 0, 0, 0, 0);
  88. $this->assertCarbon($dt->copy()->ceilDay(), 2315, 7, 19, 0, 0, 0, 0);
  89. $this->assertCarbon($dt->copy()->floorDay(), 2315, 7, 18, 0, 0, 0, 0);
  90. $this->assertCarbon($dt->copy()->roundYear(), 2316, 1, 1, 0, 0, 0, 0);
  91. $this->assertCarbon($dt->copy()->subMonths(2)->roundYear(), 2315, 1, 1, 0, 0, 0, 0);
  92. $this->assertCarbon($dt->copy()->roundYear(2), 2315, 1, 1, 0, 0, 0, 0);
  93. $this->assertCarbon($dt->copy()->floorYear(2), 2315, 1, 1, 0, 0, 0, 0);
  94. $this->assertCarbon($dt->copy()->ceilYear(2), 2317, 1, 1, 0, 0, 0, 0);
  95. $this->assertCarbon($dt->copy()->roundMonth(), 2315, 8, 1, 0, 0, 0, 0);
  96. $this->assertCarbon($dt->copy()->floorMonth(), 2315, 7, 1, 0, 0, 0, 0);
  97. for ($i = 1; $i <= Carbon::MONTHS_PER_YEAR; $i++) {
  98. $dt = Carbon::parse("2021-$i-01")->endOfMonth()->floorMonth();
  99. $this->assertCarbon($dt, 2021, $i, 1, 0, 0, 0, 0);
  100. }
  101. }
  102. public function testFloorYear()
  103. {
  104. $date = Carbon::create(2022)->endOfYear()->floorYear();
  105. $this->assertCarbon($date, 2022, 1, 1, 0, 0, 0, 0);
  106. $date = Carbon::create(2022)->endOfYear()->floorDay()->floorYear();
  107. $this->assertCarbon($date, 2022, 1, 1, 0, 0, 0, 0);
  108. $date = Carbon::create(2022)->endOfYear()->floorYear();
  109. $this->assertCarbon($date, 2022, 1, 1, 0, 0, 0, 0);
  110. $date = Carbon::create(2022)->addMonths(6)->floorYear();
  111. $this->assertCarbon($date, 2022, 1, 1, 0, 0, 0, 0);
  112. }
  113. public function testCeilYear()
  114. {
  115. $date = Carbon::create(2022)->addMonths(6)->ceilYear();
  116. $this->assertCarbon($date, 2023, 1, 1, 0, 0, 0, 0);
  117. $date = Carbon::create(2022)->endOfYear()->ceilYear();
  118. $this->assertCarbon($date, 2023, 1, 1, 0, 0, 0, 0);
  119. $date = Carbon::create(2022)->ceilYear();
  120. $this->assertCarbon($date, 2022, 1, 1, 0, 0, 0, 0);
  121. $date = Carbon::create(2022)->addMicrosecond()->ceilYear();
  122. $this->assertCarbon($date, 2023, 1, 1, 0, 0, 0, 0);
  123. }
  124. public function testRoundWithMetaUnit()
  125. {
  126. $dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
  127. $copy = $dt->copy();
  128. $ref = $copy->roundSecond();
  129. $this->assertSame($ref, $copy);
  130. $this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
  131. $this->assertCarbon($dt->copy()->roundMillisecond(), 2315, 7, 18, 22, 42, 17, 644000);
  132. $this->assertCarbon($dt->copy()->roundMillennium(), 2001, 1, 1, 0, 0, 0, 0);
  133. $this->assertCarbon($dt->copy()->roundQuarter(), 2315, 7, 1, 0, 0, 0, 0);
  134. $this->assertCarbon($dt->copy()->roundQuarters(2), 2315, 7, 1, 0, 0, 0, 0);
  135. $this->assertCarbon($dt->copy()->subMonth()->floorQuarter(), 2315, 4, 1, 0, 0, 0, 0);
  136. $this->assertCarbon($dt->copy()->subMonth()->floorQuarters(2), 2315, 1, 1, 0, 0, 0, 0);
  137. }
  138. public function testRoundWeek()
  139. {
  140. $dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
  141. $copy = $dt->copy();
  142. $ref = $copy->roundSecond();
  143. $this->assertSame($ref, $copy);
  144. $this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
  145. $this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 12, 0, 0, 0, 0);
  146. $this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 19, 0, 0, 0, 0);
  147. $this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 19, 0, 0, 0, 0);
  148. $dt = Carbon::create(2315, 7, 19, 0, 0, 0, 0);
  149. $this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 19, 0, 0, 0, 0);
  150. $this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 19, 0, 0, 0, 0);
  151. $this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 19, 0, 0, 0, 0);
  152. Carbon::setWeekStartsAt(Carbon::SUNDAY);
  153. Carbon::setWeekEndsAt(Carbon::SATURDAY);
  154. $this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 18, 0, 0, 0, 0);
  155. $this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 25, 0, 0, 0, 0);
  156. $this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 18, 0, 0, 0, 0);
  157. Carbon::setWeekStartsAt(Carbon::MONDAY);
  158. Carbon::setWeekEndsAt(Carbon::SUNDAY);
  159. }
  160. public function testCeilMonth()
  161. {
  162. $this->assertCarbon(Carbon::parse('2021-01-29')->ceilMonth(), 2021, 2, 1, 0, 0, 0);
  163. $this->assertCarbon(Carbon::parse('2021-01-31')->ceilMonth(), 2021, 2, 1, 0, 0, 0);
  164. $this->assertCarbon(Carbon::parse('2021-12-17')->ceilMonth(), 2022, 1, 1, 0, 0, 0);
  165. }
  166. public function testFloorMonth()
  167. {
  168. $this->assertCarbon(Carbon::parse('2021-05-31')->floorMonth(3), 2021, 4, 1, 0, 0, 0);
  169. }
  170. public function testRoundInvalidArgument()
  171. {
  172. $this->expectExceptionObject(new InvalidArgumentException(
  173. 'Unknown unit \'foobar\'.'
  174. ));
  175. Carbon::now()->roundUnit('foobar');
  176. }
  177. }