RoundTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 InvalidArgumentException;
  14. use Tests\AbstractTestCase;
  15. class RoundTest extends AbstractTestCase
  16. {
  17. public function testRoundWithDefaultUnit()
  18. {
  19. $dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
  20. $copy = $dt->copy();
  21. $ref = $copy->round();
  22. $this->assertNotEquals($ref, $copy);
  23. $this->assertNotSame($ref, $copy);
  24. $this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
  25. $this->assertCarbon($dt->copy()->round(5), 2315, 7, 18, 22, 42, 20, 0);
  26. $this->assertCarbon($dt->copy()->floor()->round(5), 2315, 7, 18, 22, 42, 15, 0);
  27. $this->assertCarbon($dt->copy()->round(3), 2315, 7, 18, 22, 42, 18, 0);
  28. $this->assertCarbon($dt->copy()->round(4), 2315, 7, 18, 22, 42, 16, 0);
  29. $this->assertCarbon($dt->copy()->round(10), 2315, 7, 18, 22, 42, 20, 0);
  30. $this->assertCarbon($dt->copy()->round(0.5), 2315, 7, 18, 22, 42, 17, 500000);
  31. $this->assertCarbon($dt->copy()->round(0.25), 2315, 7, 18, 22, 42, 17, 750000);
  32. $this->assertCarbon($dt->copy()->round(3.8), 2315, 7, 18, 22, 42, 19, 800000);
  33. $this->assertCarbon($dt->copy()->floor(5), 2315, 7, 18, 22, 42, 15, 0);
  34. $this->assertCarbon($dt->copy()->floor()->floor(5), 2315, 7, 18, 22, 42, 15, 0);
  35. $this->assertCarbon($dt->copy()->floor(3), 2315, 7, 18, 22, 42, 15, 0);
  36. $this->assertCarbon($dt->copy()->floor(4), 2315, 7, 18, 22, 42, 16, 0);
  37. $this->assertCarbon($dt->copy()->floor(10), 2315, 7, 18, 22, 42, 10, 0);
  38. $this->assertCarbon($dt->copy()->floor(0.5), 2315, 7, 18, 22, 42, 17, 500000);
  39. $this->assertCarbon($dt->copy()->floor(0.25), 2315, 7, 18, 22, 42, 17, 500000);
  40. $this->assertCarbon($dt->copy()->floor(3.8), 2315, 7, 18, 22, 42, 15, 0);
  41. $this->assertCarbon($dt->copy()->ceil(5), 2315, 7, 18, 22, 42, 20, 0);
  42. $this->assertCarbon($dt->copy()->floor()->ceil(5), 2315, 7, 18, 22, 42, 20, 0);
  43. $this->assertCarbon($dt->copy()->ceil(3), 2315, 7, 18, 22, 42, 18, 0);
  44. $this->assertCarbon($dt->copy()->ceil(4), 2315, 7, 18, 22, 42, 20, 0);
  45. $this->assertCarbon($dt->copy()->ceil(10), 2315, 7, 18, 22, 42, 20, 0);
  46. $this->assertCarbon($dt->copy()->ceil(0.5), 2315, 7, 18, 22, 42, 18, 0);
  47. $this->assertCarbon($dt->copy()->ceil(0.25), 2315, 7, 18, 22, 42, 17, 750000);
  48. $this->assertCarbon($dt->copy()->ceil(3.8), 2315, 7, 18, 22, 42, 19, 800000);
  49. }
  50. public function testRoundWithBaseUnit()
  51. {
  52. $dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
  53. $copy = $dt->copy();
  54. $ref = $copy->roundSecond();
  55. $this->assertNotEquals($ref, $copy);
  56. $this->assertNotSame($ref, $copy);
  57. $this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
  58. $this->assertCarbon($dt->copy()->roundDay(), 2315, 7, 19, 0, 0, 0, 0);
  59. $this->assertCarbon($dt->copy()->roundDay(5), 2315, 7, 21, 0, 0, 0, 0);
  60. $this->assertCarbon($dt->copy()->ceilDay(), 2315, 7, 19, 0, 0, 0, 0);
  61. $this->assertCarbon($dt->copy()->floorDay(), 2315, 7, 18, 0, 0, 0, 0);
  62. $this->assertCarbon($dt->copy()->roundYear(), 2316, 1, 1, 0, 0, 0, 0);
  63. $this->assertCarbon($dt->copy()->subMonths(2)->roundYear(), 2315, 1, 1, 0, 0, 0, 0);
  64. $this->assertCarbon($dt->copy()->roundYear(2), 2315, 1, 1, 0, 0, 0, 0);
  65. $this->assertCarbon($dt->copy()->floorYear(2), 2315, 1, 1, 0, 0, 0, 0);
  66. $this->assertCarbon($dt->copy()->ceilYear(2), 2317, 1, 1, 0, 0, 0, 0);
  67. $this->assertCarbon($dt->copy()->roundMonth(), 2315, 8, 1, 0, 0, 0, 0);
  68. $this->assertCarbon($dt->copy()->floorMonth(), 2315, 7, 1, 0, 0, 0, 0);
  69. }
  70. public function testRoundWithMetaUnit()
  71. {
  72. $dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
  73. $copy = $dt->copy();
  74. $ref = $copy->roundSecond();
  75. $this->assertNotEquals($ref, $copy);
  76. $this->assertNotSame($ref, $copy);
  77. $this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
  78. $this->assertCarbon($dt->copy()->roundMillisecond(), 2315, 7, 18, 22, 42, 17, 644000);
  79. $this->assertCarbon($dt->copy()->roundMillennium(), 2001, 1, 1, 0, 0, 0, 0);
  80. $this->assertCarbon($dt->copy()->roundQuarter(), 2315, 7, 1, 0, 0, 0, 0);
  81. $this->assertCarbon($dt->copy()->roundQuarters(2), 2315, 7, 1, 0, 0, 0, 0);
  82. $this->assertCarbon($dt->copy()->subMonth()->floorQuarter(), 2315, 4, 1, 0, 0, 0, 0);
  83. $this->assertCarbon($dt->copy()->subMonth()->floorQuarters(2), 2315, 1, 1, 0, 0, 0, 0);
  84. }
  85. public function testRoundWeek()
  86. {
  87. $dt = Carbon::create(2315, 7, 18, 22, 42, 17.643971);
  88. $copy = $dt->copy();
  89. $ref = $copy->roundSecond();
  90. $this->assertNotEquals($ref, $copy);
  91. $this->assertNotSame($ref, $copy);
  92. $this->assertCarbon($ref, 2315, 7, 18, 22, 42, 18, 0);
  93. $this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 12, 0, 0, 0, 0);
  94. $this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 19, 0, 0, 0, 0);
  95. $this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 19, 0, 0, 0, 0);
  96. $dt = Carbon::create(2315, 7, 19, 0, 0, 0, 0);
  97. $this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 19, 0, 0, 0, 0);
  98. $this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 19, 0, 0, 0, 0);
  99. $this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 19, 0, 0, 0, 0);
  100. Carbon::setWeekStartsAt(Carbon::SUNDAY);
  101. Carbon::setWeekEndsAt(Carbon::SATURDAY);
  102. $this->assertCarbon($dt->copy()->floorWeek(), 2315, 7, 18, 0, 0, 0, 0);
  103. $this->assertCarbon($dt->copy()->ceilWeek(), 2315, 7, 25, 0, 0, 0, 0);
  104. $this->assertCarbon($dt->copy()->roundWeek(), 2315, 7, 18, 0, 0, 0, 0);
  105. Carbon::setWeekStartsAt(Carbon::MONDAY);
  106. Carbon::setWeekEndsAt(Carbon::SUNDAY);
  107. }
  108. public function testCeilMonth()
  109. {
  110. $this->assertCarbon(Carbon::parse('2021-01-29')->ceilMonth(), 2021, 2, 1, 0, 0, 0);
  111. $this->assertCarbon(Carbon::parse('2021-01-31')->ceilMonth(), 2021, 2, 1, 0, 0, 0);
  112. $this->assertCarbon(Carbon::parse('2021-12-17')->ceilMonth(), 2022, 1, 1, 0, 0, 0);
  113. }
  114. public function testFloorMonth()
  115. {
  116. $this->assertCarbon(Carbon::parse('2021-05-31')->floorMonth(3), 2021, 4, 1, 0, 0, 0);
  117. }
  118. public function testRoundInvalidArgument()
  119. {
  120. $this->expectExceptionObject(new InvalidArgumentException(
  121. 'Unknown unit \'foobar\'.'
  122. ));
  123. Carbon::now()->roundUnit('foobar');
  124. }
  125. }