NowAndOtherStaticHelpersTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 DateTime;
  14. use DateTimeZone;
  15. use Tests\AbstractTestCase;
  16. class NowAndOtherStaticHelpersTest extends AbstractTestCase
  17. {
  18. public function testNow()
  19. {
  20. $dt = Carbon::now();
  21. $this->assertSame($this->immutableNow->getTimestamp(), $dt->timestamp);
  22. Carbon::setTestNow();
  23. $before = $this->getTimestamp();
  24. $dt = Carbon::now();
  25. $after = $this->getTimestamp();
  26. $this->assertGreaterThanOrEqual($before, $dt->timestamp);
  27. $this->assertLessThanOrEqual($after, $dt->timestamp);
  28. }
  29. public function testGetPreciseTimestamp()
  30. {
  31. $dt = Carbon::parse('2018-01-06 12:34:10.987126');
  32. $this->assertSame(1515260.0, $dt->getPreciseTimestamp(-3));
  33. $this->assertSame(151526005.0, $dt->getPreciseTimestamp(-1));
  34. $this->assertSame(1515260051.0, $dt->getPreciseTimestamp(0));
  35. $this->assertSame(15152600510.0, $dt->getPreciseTimestamp(1));
  36. $this->assertSame(151526005099.0, $dt->getPreciseTimestamp(2));
  37. $this->assertSame(1515260050987.0, $dt->valueOf());
  38. $this->assertSame(15152600509871.0, $dt->getPreciseTimestamp(4));
  39. $this->assertSame(151526005098713.0, $dt->getPreciseTimestamp(5));
  40. $this->assertSame(1515260050987126.0, $dt->getPreciseTimestamp(6));
  41. $this->assertSame(151526005098712600.0, $dt->getPreciseTimestamp(8));
  42. $this->assertSame(1515260050987126000.0, $dt->getPreciseTimestamp(9));
  43. }
  44. public function testGetTimestampMs()
  45. {
  46. $dt = Carbon::parse('2018-01-06 12:34:10.987126');
  47. $this->assertSame(1515260050987, $dt->getTimestampMs());
  48. }
  49. public function testNowWithTimezone()
  50. {
  51. $dt = Carbon::now('Europe/London');
  52. $this->assertSame($this->immutableNow->getTimestamp(), $dt->timestamp);
  53. Carbon::setTestNow();
  54. $before = $this->getTimestamp();
  55. $dt = Carbon::now('Europe/London');
  56. $after = $this->getTimestamp();
  57. $this->assertGreaterThanOrEqual($before, $dt->timestamp);
  58. $this->assertLessThanOrEqual($after, $dt->timestamp);
  59. $this->assertSame('Europe/London', $dt->tzName);
  60. }
  61. public function testToday()
  62. {
  63. $dt = Carbon::today();
  64. $this->assertSame(date('Y-m-d 00:00:00'), $dt->toDateTimeString());
  65. }
  66. public function testTodayWithTimezone()
  67. {
  68. $dt = Carbon::today('Europe/London');
  69. $dt2 = new DateTime('now', new DateTimeZone('Europe/London'));
  70. $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString());
  71. }
  72. public function testTomorrow()
  73. {
  74. $dt = Carbon::tomorrow();
  75. $dt2 = new DateTime('tomorrow');
  76. $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString());
  77. }
  78. public function testTomorrowWithTimezone()
  79. {
  80. $dt = Carbon::tomorrow('Europe/London');
  81. $dt2 = new DateTime('tomorrow', new DateTimeZone('Europe/London'));
  82. $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString());
  83. }
  84. public function testYesterday()
  85. {
  86. $dt = Carbon::yesterday();
  87. $dt2 = new DateTime('yesterday');
  88. $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString());
  89. }
  90. public function testYesterdayWithTimezone()
  91. {
  92. $dt = Carbon::yesterday('Europe/London');
  93. $dt2 = new DateTime('yesterday', new DateTimeZone('Europe/London'));
  94. $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString());
  95. }
  96. public function testMinValue()
  97. {
  98. $this->assertLessThanOrEqual(-2147483647, Carbon::minValue()->getTimestamp());
  99. }
  100. public function testMaxValue()
  101. {
  102. $this->assertGreaterThanOrEqual(2147483647, Carbon::maxValue()->getTimestamp());
  103. }
  104. }