DateTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Jenssegers;
  12. use Carbon\Carbon;
  13. use DateTimeImmutable;
  14. class DateTest extends TestCaseBase
  15. {
  16. public function testConstructFromString()
  17. {
  18. Carbon::setTestNow();
  19. $date = new Carbon('2013-01-31');
  20. $this->assertSame(1359590400, $date->getTimestamp());
  21. $before = (new DateTimeImmutable())->getTimestamp();
  22. $date = new Carbon('1 day ago');
  23. $after = (new DateTimeImmutable())->getTimestamp();
  24. $this->assertGreaterThanOrEqual($before - 86400, $date->getTimestamp());
  25. $this->assertLessThanOrEqual($after - 86400, $date->getTimestamp());
  26. }
  27. public function testMake()
  28. {
  29. $date1 = Carbon::make('Sunday 28 April 2013 21:58:16');
  30. $date2 = new Carbon('Sunday 28 April 2013 21:58:16');
  31. $this->assertEquals($date1, $date2);
  32. }
  33. public function testCreateFromCarbon()
  34. {
  35. $date = Carbon::make(Carbon::createFromFormat('U', '1367186296'));
  36. $this->assertInstanceOf(Carbon::class, $date);
  37. $this->assertSame(1367186296, $date->getTimestamp());
  38. }
  39. public function testManipulation()
  40. {
  41. $now = Carbon::now();
  42. $this->assertSame(86400, $now->copy()->add('1 day')->getTimestamp() - $now->getTimestamp());
  43. $this->assertSame(4 * 86400, $now->copy()->add('4 day')->getTimestamp() - $now->getTimestamp());
  44. $this->assertSame(-86400, $now->copy()->sub('1 day')->getTimestamp() - $now->getTimestamp());
  45. $this->assertSame(-4 * 86400, $now->copy()->sub('4 day')->getTimestamp() - $now->getTimestamp());
  46. $this->assertSame(10 * 86400, $now->copy()->add('P10D')->getTimestamp() - $now->getTimestamp());
  47. $this->assertSame(-10 * 86400, $now->copy()->sub('P10D')->getTimestamp() - $now->getTimestamp());
  48. }
  49. public function testFormat()
  50. {
  51. $date = new Carbon(1367186296);
  52. $this->assertSame('Sunday 28 April 2013 21:58:16', $date->format('l j F Y H:i:s'));
  53. }
  54. public function testAge()
  55. {
  56. // Age test can't work on February 29th
  57. if (Carbon::now()->format('m-d') === '02-29') {
  58. Carbon::setTestNow(Carbon::now()->subDay());
  59. }
  60. $date = Carbon::parse('-5 years');
  61. $this->assertSame(5, $date->age);
  62. }
  63. public function testAgo()
  64. {
  65. // Ago test can't work on February 29th
  66. if (Carbon::now()->format('m-d') === '02-29') {
  67. Carbon::setTestNow(Carbon::now()->subDay());
  68. }
  69. $date = Carbon::parse('-5 years');
  70. $this->assertSame('5 years ago', $date->ago());
  71. $date = JenssegersDate::now()->subMonthsNoOverflow(5);
  72. $this->assertSame('5 months ago', $date->ago());
  73. $date = Carbon::parse('-32 days');
  74. $this->assertSame('1 month ago', $date->ago());
  75. $date = Carbon::parse('-4 days');
  76. $this->assertSame('4 days ago', $date->ago());
  77. $date = Carbon::parse('-1 day');
  78. $this->assertSame('1 day ago', $date->ago());
  79. $date = Carbon::parse('-3 hours');
  80. $this->assertSame('3 hours ago', $date->ago());
  81. $date = Carbon::parse('-1 hour');
  82. $this->assertSame('1 hour ago', $date->ago());
  83. $date = Carbon::parse('-2 minutes');
  84. $this->assertSame('2 minutes ago', $date->ago());
  85. $date = Carbon::parse('-1 minute');
  86. $this->assertSame('1 minute ago', $date->ago());
  87. $date = Carbon::parse('-50 second');
  88. $this->assertSame('50 seconds ago', $date->ago());
  89. $date = Carbon::parse('-1 second');
  90. $this->assertSame('1 second ago', $date->ago());
  91. $date = Carbon::parse('+5 days');
  92. $this->assertSame('5 days from now', $date->ago());
  93. $date = Carbon::parse('+5 days');
  94. $this->assertSame('5 days after', $date->ago(Carbon::now()));
  95. $date = Carbon::parse('-5 days');
  96. $this->assertSame('5 days before', $date->ago(Carbon::now()));
  97. }
  98. public function testAbsoluteAgo()
  99. {
  100. $date = Carbon::parse('-5 days');
  101. $this->assertSame('5 days', $date->ago(Carbon::now(), true));
  102. $date = Carbon::parse('+5 days');
  103. $this->assertSame('5 days', $date->ago(Carbon::now(), true));
  104. }
  105. public function testDiffForHumans()
  106. {
  107. // Diff for humans test can't work on February 29th
  108. if (Carbon::now()->format('m-d') === '02-29') {
  109. Carbon::setTestNow(Carbon::now()->subDay());
  110. }
  111. $date = Carbon::parse('-5 years');
  112. $this->assertSame('5 years ago', $date->diffForHumans());
  113. $date = Carbon::parse('-15 days');
  114. $this->assertSame('2 weeks ago', $date->diffForHumans());
  115. $date = Carbon::parse('-13 days');
  116. $this->assertSame('1 week ago', $date->diffForHumans());
  117. $date = Carbon::parse('-13 days');
  118. $this->assertSame('1 week', $date->diffForHumans(null, true));
  119. $date = JenssegersDate::now()->subMonthsNoOverflow(3);
  120. $this->assertSame('3 months', $date->diffForHumans(null, true));
  121. $date = Carbon::parse('-1 week');
  122. $future = Carbon::parse('+1 week');
  123. $this->assertSame('2 weeks after', $future->diffForHumans($date));
  124. $this->assertSame('2 weeks before', $date->diffForHumans($future));
  125. }
  126. public function testTimespan()
  127. {
  128. $date = new Carbon(1403619368);
  129. $date = $date->sub('-100 days -3 hours -20 minutes');
  130. $this->assertSame('3 months, 1 week, 1 day, 3 hours, 20 minutes', $date->timespan(1403619368));
  131. }
  132. public function testTranslateTimeString()
  133. {
  134. Carbon::setLocale('ru');
  135. $date = Carbon::translateTimeString('понедельник 21 март 2015');
  136. $this->assertSame('monday 21 march 2015', mb_strtolower($date));
  137. Carbon::setLocale('de');
  138. $date = Carbon::translateTimeString('Montag 21 März 2015');
  139. $this->assertSame('monday 21 march 2015', mb_strtolower($date));
  140. $this->assertSame('Foobar', Carbon::translateTimeString('Foobar', 'xx'));
  141. }
  142. public function testTranslateTimeStringWithOrdinalWords()
  143. {
  144. $date = Carbon::translateTimeString('Premier lundi de mai', 'fr', 'en');
  145. $this->assertSame('first monday of may', mb_strtolower($date));
  146. $date = Carbon::translateTimeString('Premier lundi de mai', 'fr', 'es');
  147. $this->assertSame('primer lunes de mayo', mb_strtolower($date));
  148. }
  149. }