ConstructTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 ConstructTest extends AbstractTestCase
  17. {
  18. public function testCreatesAnInstanceDefaultToNow()
  19. {
  20. $c = new Carbon();
  21. $now = Carbon::now();
  22. $this->assertInstanceOfCarbon($c);
  23. $this->assertSame($now->tzName, $c->tzName);
  24. $this->assertCarbon($c, $now->year, $now->month, $now->day, $now->hour, $now->minute, $now->second);
  25. }
  26. public function testCreatesAnInstanceFromADateTime()
  27. {
  28. $c = new Carbon(Carbon::parse('2009-09-09 09:09:09'));
  29. $this->assertSame('2009-09-09 09:09:09 America/Toronto', $c->format('Y-m-d H:i:s e'));
  30. $c = new Carbon(new DateTime('2009-09-09 09:09:09'));
  31. $this->assertSame('2009-09-09 09:09:09 America/Toronto', $c->format('Y-m-d H:i:s e'));
  32. $c = new Carbon(new DateTime('2009-09-09 09:09:09', new DateTimeZone('Europe/Paris')));
  33. $this->assertSame('2009-09-09 09:09:09 Europe/Paris', $c->format('Y-m-d H:i:s e'));
  34. $c = new Carbon(new DateTime('2009-09-09 09:09:09'), 'Europe/Paris');
  35. $this->assertSame('2009-09-09 15:09:09 Europe/Paris', $c->format('Y-m-d H:i:s e'));
  36. $c = new Carbon(new DateTime('2009-09-09 09:09:09', new DateTimeZone('Asia/Tokyo')), 'Europe/Paris');
  37. $this->assertSame('2009-09-09 02:09:09 Europe/Paris', $c->format('Y-m-d H:i:s e'));
  38. \Carbon\Carbon::useStrictMode(false);
  39. $c = new Carbon(new DateTime('2009-09-09 09:09:09', new DateTimeZone('Asia/Tokyo')), '¤¤ Incorrect Timezone ¤¤');
  40. $this->assertSame('2009-09-09 09:09:09 America/Toronto', $c->format('Y-m-d H:i:s e'));
  41. }
  42. public function testParseCreatesAnInstanceDefaultToNow()
  43. {
  44. $c = Carbon::parse();
  45. $now = Carbon::now();
  46. $this->assertInstanceOfCarbon($c);
  47. $this->assertSame($now->tzName, $c->tzName);
  48. $this->assertCarbon($c, $now->year, $now->month, $now->day, $now->hour, $now->minute, $now->second);
  49. }
  50. public function testWithFancyString()
  51. {
  52. Carbon::setTestNow(Carbon::today());
  53. $c = new Carbon('first day of January 2008');
  54. $this->assertCarbon($c, 2008, 1, 1, 0, 0, 0);
  55. }
  56. public function testParseWithFancyString()
  57. {
  58. Carbon::setTestNow(Carbon::today());
  59. $c = Carbon::parse('first day of January 2008');
  60. $this->assertCarbon($c, 2008, 1, 1, 0, 0, 0);
  61. }
  62. public function testParseWithYYYMMDD()
  63. {
  64. $c = Carbon::parse('20201128');
  65. $this->assertCarbon($c, 2020, 11, 28, 0, 0, 0);
  66. }
  67. public function testParseWithYYYMMDDHHMMSS()
  68. {
  69. $c = Carbon::parse('20201128192533');
  70. $this->assertCarbon($c, 2020, 11, 28, 19, 25, 33);
  71. }
  72. public function testDefaultTimezone()
  73. {
  74. $c = new Carbon('now');
  75. $this->assertSame('America/Toronto', $c->tzName);
  76. }
  77. public function testParseWithDefaultTimezone()
  78. {
  79. $c = Carbon::parse('now');
  80. $this->assertSame('America/Toronto', $c->tzName);
  81. }
  82. public function testSettingTimezone()
  83. {
  84. $timezone = 'Europe/London';
  85. $dtz = new DateTimeZone($timezone);
  86. $dt = new DateTime('now', $dtz);
  87. $dayLightSavingTimeOffset = (int) $dt->format('I');
  88. $c = new Carbon('now', $dtz);
  89. $this->assertSame($timezone, $c->tzName);
  90. $this->assertSame($dayLightSavingTimeOffset, $c->offsetHours);
  91. }
  92. public function testParseSettingTimezone()
  93. {
  94. $timezone = 'Europe/London';
  95. $dtz = new DateTimeZone($timezone);
  96. $dt = new DateTime('now', $dtz);
  97. $dayLightSavingTimeOffset = (int) $dt->format('I');
  98. $c = Carbon::parse('now', $dtz);
  99. $this->assertSame($timezone, $c->tzName);
  100. $this->assertSame($dayLightSavingTimeOffset, $c->offsetHours);
  101. }
  102. public function testSettingTimezoneWithString()
  103. {
  104. $timezone = 'Asia/Tokyo';
  105. $dtz = new DateTimeZone($timezone);
  106. $dt = new DateTime('now', $dtz);
  107. $dayLightSavingTimeOffset = (int) $dt->format('I');
  108. $c = new Carbon('now', $timezone);
  109. $this->assertSame($timezone, $c->tzName);
  110. $this->assertSame(9 + $dayLightSavingTimeOffset, $c->offsetHours);
  111. }
  112. public function testParseSettingTimezoneWithString()
  113. {
  114. $timezone = 'Asia/Tokyo';
  115. $dtz = new DateTimeZone($timezone);
  116. $dt = new DateTime('now', $dtz);
  117. $dayLightSavingTimeOffset = (int) $dt->format('I');
  118. $c = Carbon::parse('now', $timezone);
  119. $this->assertSame($timezone, $c->tzName);
  120. $this->assertSame(9 + $dayLightSavingTimeOffset, $c->offsetHours);
  121. }
  122. public function testMockingWithMicroseconds()
  123. {
  124. $c = new Carbon(Carbon::now()->toDateTimeString().'.123456');
  125. Carbon::setTestNow($c);
  126. $mockedC = Carbon::now();
  127. $this->assertTrue($c->eq($mockedC));
  128. Carbon::setTestNow();
  129. }
  130. public function testTimestamp()
  131. {
  132. $date = new Carbon(1367186296);
  133. $this->assertSame('Sunday 28 April 2013 21:58:16.000000', $date->format('l j F Y H:i:s.u'));
  134. $date = new Carbon(123);
  135. $this->assertSame('Thursday 1 January 1970 00:02:03.000000', $date->format('l j F Y H:i:s.u'));
  136. }
  137. public function testFloatTimestamp()
  138. {
  139. $date = new Carbon(1367186296.654321);
  140. $this->assertSame('Sunday 28 April 2013 21:58:16.654321', $date->format('l j F Y H:i:s.u'));
  141. $date = new Carbon(123.5);
  142. $this->assertSame('Thursday 1 January 1970 00:02:03.500000', $date->format('l j F Y H:i:s.u'));
  143. }
  144. }