CreateFromTimestampTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 DateTimeZone;
  14. use Tests\AbstractTestCase;
  15. class CreateFromTimestampTest extends AbstractTestCase
  16. {
  17. public function testCreateReturnsDatingInstance()
  18. {
  19. $d = Carbon::createFromTimestamp(Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp);
  20. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5);
  21. }
  22. public function testCreateFromTimestampMs()
  23. {
  24. $baseTimestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000;
  25. $timestamp = $baseTimestamp + 321;
  26. $d = Carbon::createFromTimestampMs($timestamp);
  27. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);
  28. $timestamp = $baseTimestamp + 321.8;
  29. $d = Carbon::createFromTimestampMs($timestamp);
  30. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321800);
  31. $timestamp = $baseTimestamp + 321.84;
  32. $d = Carbon::createFromTimestampMs($timestamp);
  33. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321840);
  34. $timestamp = $baseTimestamp + 321.847;
  35. $d = Carbon::createFromTimestampMs($timestamp);
  36. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321847);
  37. $timestamp = $baseTimestamp + 321.8474;
  38. $d = Carbon::createFromTimestampMs($timestamp);
  39. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321847);
  40. $timestamp = $baseTimestamp + 321.8479;
  41. $d = Carbon::createFromTimestampMs($timestamp);
  42. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321848);
  43. }
  44. public function testCreateFromTimestampMsUTC()
  45. {
  46. // Toronto is GMT-04:00 in May
  47. $baseTimestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000;
  48. $timestamp = $baseTimestamp + 321;
  49. $d = Carbon::createFromTimestampMsUTC($timestamp);
  50. $this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321000);
  51. $timestamp = $baseTimestamp + 321.8;
  52. $d = Carbon::createFromTimestampMsUTC($timestamp);
  53. $this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321800);
  54. $timestamp = $baseTimestamp + 321.84;
  55. $d = Carbon::createFromTimestampMsUTC($timestamp);
  56. $this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321840);
  57. $timestamp = $baseTimestamp + 321.847;
  58. $d = Carbon::createFromTimestampMsUTC($timestamp);
  59. $this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321847);
  60. $timestamp = $baseTimestamp + 321.8474;
  61. $d = Carbon::createFromTimestampMsUTC($timestamp);
  62. $this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321847);
  63. $timestamp = $baseTimestamp + 321.8479;
  64. $d = Carbon::createFromTimestampMsUTC($timestamp);
  65. $this->assertCarbon($d, 1975, 5, 22, 2, 32, 5, 321848);
  66. $d = Carbon::createFromTimestampMsUTC(1);
  67. $this->assertCarbon($d, 1970, 1, 1, 0, 0, 0, 1000);
  68. $d = Carbon::createFromTimestampMsUTC(60);
  69. $this->assertCarbon($d, 1970, 1, 1, 0, 0, 0, 60000);
  70. $d = Carbon::createFromTimestampMsUTC(1000);
  71. $this->assertCarbon($d, 1970, 1, 1, 0, 0, 1, 0);
  72. $d = Carbon::createFromTimestampMsUTC(-0.04);
  73. $this->assertCarbon($d, 1969, 12, 31, 23, 59, 59, 999960);
  74. }
  75. public function testComaDecimalSeparatorLocale()
  76. {
  77. $date = new Carbon('2017-07-29T13:57:27.123456Z');
  78. $this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));
  79. $date = Carbon::createFromFormat('Y-m-d\TH:i:s.uT', '2017-07-29T13:57:27.123456Z');
  80. $this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));
  81. $timestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000 + 321;
  82. $d = Carbon::createFromTimestampMs($timestamp);
  83. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);
  84. $locale = setlocale(LC_ALL, '0');
  85. if (setlocale(LC_ALL, 'fr_FR.UTF-8', 'fr_FR.utf8', 'French_France.UTF8') === false) {
  86. $this->markTestSkipped('testComaDecimalSeparatorLocale test need fr_FR.UTF-8.');
  87. }
  88. $timestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000 + 321;
  89. $d = Carbon::createFromTimestampMs($timestamp);
  90. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);
  91. $date = new Carbon('2017-07-29T13:57:27.123456Z');
  92. $this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));
  93. $date = Carbon::createFromFormat('Y-m-d\TH:i:s.uT', '2017-07-29T13:57:27.123456Z');
  94. $this->assertSame('2017-07-29 13:57:27.123456 Z', $date->format('Y-m-d H:i:s.u e'));
  95. $timestamp = Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp * 1000 + 321;
  96. $d = Carbon::createFromTimestampMs($timestamp);
  97. $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5, 321000);
  98. setlocale(LC_ALL, $locale);
  99. }
  100. public function testCreateFromTimestampUsesDefaultTimezone()
  101. {
  102. $d = Carbon::createFromTimestamp(0);
  103. // We know Toronto is -5 since no DST in Jan
  104. $this->assertSame(1969, $d->year);
  105. $this->assertSame(-5 * 3600, $d->offset);
  106. }
  107. public function testCreateFromTimestampWithDateTimeZone()
  108. {
  109. $d = Carbon::createFromTimestamp(0, new DateTimeZone('UTC'));
  110. $this->assertSame('UTC', $d->tzName);
  111. $this->assertCarbon($d, 1970, 1, 1, 0, 0, 0);
  112. }
  113. public function testCreateFromTimestampWithString()
  114. {
  115. $d = Carbon::createFromTimestamp(0, 'UTC');
  116. $this->assertCarbon($d, 1970, 1, 1, 0, 0, 0);
  117. $this->assertSame(0, $d->offset);
  118. $this->assertSame('UTC', $d->tzName);
  119. }
  120. public function testCreateFromTimestampGMTDoesNotUseDefaultTimezone()
  121. {
  122. $d = Carbon::createFromTimestampUTC(0);
  123. $this->assertCarbon($d, 1970, 1, 1, 0, 0, 0);
  124. $this->assertSame(0, $d->offset);
  125. }
  126. /**
  127. * Ensures DST php bug does not affect createFromTimestamp in DST change.
  128. *
  129. * @see https://github.com/briannesbitt/Carbon/issues/1951
  130. */
  131. public function testCreateFromTimestampInDstChange()
  132. {
  133. $this->assertSame(
  134. '2019-11-03T01:00:00-04:00',
  135. Carbon::createFromTimestamp(1572757200, 'America/New_York')->toIso8601String()
  136. );
  137. $this->assertSame(
  138. '2019-11-03T01:00:00-05:00',
  139. Carbon::createFromTimestamp(1572757200 + 3600, 'America/New_York')->toIso8601String()
  140. );
  141. $this->assertSame(
  142. '2019-11-03T01:00:00-04:00',
  143. Carbon::createFromTimestampMs(1572757200000, 'America/New_York')->toIso8601String()
  144. );
  145. $this->assertSame(
  146. '2019-11-03T01:00:00-05:00',
  147. Carbon::createFromTimestampMs(1572757200000 + 3600000, 'America/New_York')->toIso8601String()
  148. );
  149. }
  150. public function testCreateFromMicrotimeFloat()
  151. {
  152. $microtime = 1600887164.88952298;
  153. $d = Carbon::createFromTimestamp($microtime);
  154. $this->assertSame('America/Toronto', $d->tzName);
  155. $this->assertSame('2020-09-23 14:52:44.889523', $d->format('Y-m-d H:i:s.u'));
  156. $this->assertSame('1600887164.889523', $d->format('U.u'));
  157. $microtime = 1600887164.0603;
  158. $d = Carbon::createFromTimestamp($microtime);
  159. $this->assertSame('America/Toronto', $d->tzName);
  160. $this->assertSame('2020-09-23 14:52:44.060300', $d->format('Y-m-d H:i:s.u'));
  161. $this->assertSame('1600887164.060300', $d->format('U.u'));
  162. $this->assertSame('010000', Carbon::createFromTimestamp(0.01)->format('u'));
  163. }
  164. public function testCreateFromMicrotimeStrings()
  165. {
  166. $microtime = '0.88951247 1600887164';
  167. $d = Carbon::createFromTimestamp($microtime);
  168. $this->assertSame('America/Toronto', $d->tzName);
  169. $this->assertSame('2020-09-23 14:52:44.889512', $d->format('Y-m-d H:i:s.u'));
  170. $this->assertSame('1600887164.889512', $d->format('U.u'));
  171. $microtime = '0.88951247/1600887164/12.56';
  172. $d = Carbon::createFromTimestamp($microtime);
  173. $this->assertSame('America/Toronto', $d->tzName);
  174. $this->assertSame('2020-09-23 14:52:57.449512', $d->format('Y-m-d H:i:s.u'));
  175. $this->assertSame('1600887177.449512', $d->format('U.u'));
  176. $d = Carbon::createFromTimestamp('-10.6');
  177. $this->assertSame('1969-12-31 18:59:49.400000 -05:00', $d->format('Y-m-d H:i:s.u P'));
  178. $d = new Carbon('-10.6');
  179. $this->assertSame('1969-12-31 23:59:49.400000 +00:00', $d->format('Y-m-d H:i:s.u P'));
  180. }
  181. public function testCreateFromMicrotimeUTCFloat()
  182. {
  183. $microtime = 1600887164.88952298;
  184. $d = Carbon::createFromTimestampUTC($microtime);
  185. $this->assertSame('+00:00', $d->tzName);
  186. $this->assertSame('2020-09-23 18:52:44.889523', $d->format('Y-m-d H:i:s.u'));
  187. $this->assertSame('1600887164.889523', $d->format('U.u'));
  188. }
  189. public function testCreateFromMicrotimeUTCStrings()
  190. {
  191. $microtime = '0.88951247 1600887164';
  192. $d = Carbon::createFromTimestampUTC($microtime);
  193. $this->assertSame('+00:00', $d->tzName);
  194. $this->assertSame('2020-09-23 18:52:44.889512', $d->format('Y-m-d H:i:s.u'));
  195. $this->assertSame('1600887164.889512', $d->format('U.u'));
  196. $microtime = '0.88951247/1600887164/12.56';
  197. $d = Carbon::createFromTimestampUTC($microtime);
  198. $this->assertSame('+00:00', $d->tzName);
  199. $this->assertSame('2020-09-23 18:52:57.449512', $d->format('Y-m-d H:i:s.u'));
  200. $this->assertSame('1600887177.449512', $d->format('U.u'));
  201. }
  202. public function testNegativeIntegerTimestamp()
  203. {
  204. $this->assertSame(
  205. '1969-12-31 18:59:59.000000 -05:00',
  206. Carbon::createFromTimestamp(-1)->format('Y-m-d H:i:s.u P')
  207. );
  208. }
  209. }