GettersTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 GettersTest extends AbstractTestCase
  16. {
  17. public function testGettersThrowExceptionOnUnknownGetter()
  18. {
  19. $this->expectExceptionObject(new InvalidArgumentException(
  20. "Unknown getter 'doesNotExit'"
  21. ));
  22. /** @var mixed $d */
  23. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  24. $d->doesNotExit;
  25. }
  26. public function testYearGetter()
  27. {
  28. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  29. $this->assertSame(1234, $d->year);
  30. }
  31. public function testYearIsoGetter()
  32. {
  33. $d = Carbon::createFromDate(2012, 12, 31);
  34. $this->assertSame(2013, $d->yearIso);
  35. }
  36. public function testMonthGetter()
  37. {
  38. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  39. $this->assertSame(5, $d->month);
  40. }
  41. public function testDayGetter()
  42. {
  43. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  44. $this->assertSame(6, $d->day);
  45. }
  46. public function testHourGetter()
  47. {
  48. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  49. $this->assertSame(7, $d->hour);
  50. }
  51. public function testMinuteGetter()
  52. {
  53. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  54. $this->assertSame(8, $d->minute);
  55. }
  56. public function testSecondGetter()
  57. {
  58. $d = Carbon::create(1234, 5, 6, 7, 8, 9);
  59. $this->assertSame(9, $d->second);
  60. }
  61. public function testMicroGetter()
  62. {
  63. $micro = 345678;
  64. $d = Carbon::parse('2014-01-05 12:34:11.'.$micro);
  65. $this->assertSame($micro, $d->micro);
  66. }
  67. public function testMicroGetterWithDefaultNow()
  68. {
  69. $now = Carbon::getTestNow();
  70. Carbon::setTestNow(null);
  71. $start = microtime(true);
  72. usleep(10000);
  73. $d = Carbon::now();
  74. usleep(10000);
  75. $end = microtime(true);
  76. $microTime = $d->getTimestamp() + $d->micro / 1000000;
  77. $this->assertGreaterThan($start, $microTime);
  78. $this->assertLessThan($end, $microTime);
  79. Carbon::setTestNow($now);
  80. }
  81. public function testDayOfWeekGetter()
  82. {
  83. $d = Carbon::create(2012, 5, 7, 7, 8, 9);
  84. $this->assertSame(Carbon::MONDAY, $d->dayOfWeek);
  85. $d = Carbon::create(2012, 5, 8, 7, 8, 9);
  86. $this->assertSame(Carbon::TUESDAY, $d->dayOfWeek);
  87. $d = Carbon::create(2012, 5, 9, 7, 8, 9);
  88. $this->assertSame(Carbon::WEDNESDAY, $d->dayOfWeek);
  89. $d = Carbon::create(2012, 5, 10, 0, 0, 0);
  90. $this->assertSame(Carbon::THURSDAY, $d->dayOfWeek);
  91. $d = Carbon::create(2012, 5, 11, 23, 59, 59);
  92. $this->assertSame(Carbon::FRIDAY, $d->dayOfWeek);
  93. $d = Carbon::create(2012, 5, 12, 12, 0, 0);
  94. $this->assertSame(Carbon::SATURDAY, $d->dayOfWeek);
  95. $d = Carbon::create(2012, 5, 13, 12, 0, 0);
  96. $this->assertSame(Carbon::SUNDAY, $d->dayOfWeek);
  97. }
  98. public function testDayOfWeekIsoGetter()
  99. {
  100. $d = Carbon::create(2012, 5, 7, 7, 8, 9);
  101. $this->assertSame(1, $d->dayOfWeekIso);
  102. $d = Carbon::create(2012, 5, 8, 7, 8, 9);
  103. $this->assertSame(2, $d->dayOfWeekIso);
  104. $d = Carbon::create(2012, 5, 9, 7, 8, 9);
  105. $this->assertSame(3, $d->dayOfWeekIso);
  106. $d = Carbon::create(2012, 5, 10, 0, 0, 0);
  107. $this->assertSame(4, $d->dayOfWeekIso);
  108. $d = Carbon::create(2012, 5, 11, 23, 59, 59);
  109. $this->assertSame(5, $d->dayOfWeekIso);
  110. $d = Carbon::create(2012, 5, 12, 12, 0, 0);
  111. $this->assertSame(6, $d->dayOfWeekIso);
  112. $d = Carbon::create(2012, 5, 13, 12, 0, 0);
  113. $this->assertSame(7, $d->dayOfWeekIso);
  114. }
  115. public function testDayOfYearGetter()
  116. {
  117. $d = Carbon::createFromDate(2012, 5, 7);
  118. $this->assertSame(128, $d->dayOfYear);
  119. }
  120. public function testDaysInMonthGetter()
  121. {
  122. $d = Carbon::createFromDate(2012, 5, 7);
  123. $this->assertSame(31, $d->daysInMonth);
  124. }
  125. public function testTimestampGetter()
  126. {
  127. $d = Carbon::create();
  128. $d = $d->setTimezone('GMT');
  129. $this->assertSame(0, $d->setDateTime(1970, 1, 1, 0, 0, 0)->timestamp);
  130. }
  131. public function testGetAge()
  132. {
  133. $d = Carbon::now();
  134. $this->assertSame(0, $d->age);
  135. }
  136. public function testGetAgeWithRealAge()
  137. {
  138. $d = Carbon::createFromDate(1975, 5, 21);
  139. $age = (int) (substr((string) ((int) (date('Ymd')) - (int) (date('Ymd', $d->timestamp))), 0, -4));
  140. $this->assertSame($age, $d->age);
  141. }
  142. public static function dataForTestQuarter()
  143. {
  144. return [
  145. [1, 1],
  146. [2, 1],
  147. [3, 1],
  148. [4, 2],
  149. [5, 2],
  150. [6, 2],
  151. [7, 3],
  152. [8, 3],
  153. [9, 3],
  154. [10, 4],
  155. [11, 4],
  156. [12, 4],
  157. ];
  158. }
  159. /**
  160. * @dataProvider \Tests\CarbonImmutable\GettersTest::dataForTestQuarter
  161. *
  162. * @param int $month
  163. * @param int $quarter
  164. */
  165. public function testQuarterFirstOfMonth($month, $quarter)
  166. {
  167. $c = Carbon::create(2015, $month, 1)->startOfMonth();
  168. $this->assertSame($quarter, $c->quarter);
  169. }
  170. /**
  171. * @dataProvider \Tests\CarbonImmutable\GettersTest::dataForTestQuarter
  172. *
  173. * @param int $month
  174. * @param int $quarter
  175. */
  176. public function testQuarterMiddleOfMonth($month, $quarter)
  177. {
  178. $c = Carbon::create(2015, $month, 15, 12, 13, 14);
  179. $this->assertSame($quarter, $c->quarter);
  180. }
  181. /**
  182. * @dataProvider \Tests\CarbonImmutable\GettersTest::dataForTestQuarter
  183. *
  184. * @param int $month
  185. * @param int $quarter
  186. */
  187. public function testQuarterLastOfMonth($month, $quarter)
  188. {
  189. $c = Carbon::create(2015, $month, 1)->endOfMonth();
  190. $this->assertSame($quarter, $c->quarter);
  191. }
  192. public function testGetLocalTrue()
  193. {
  194. // Default timezone has been set to America/Toronto in AbstractTestCase.php
  195. // @see : https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
  196. $this->assertTrue(Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->local);
  197. $this->assertTrue(Carbon::createFromDate(2012, 1, 1, 'America/New_York')->local);
  198. }
  199. public function testGetLocalFalse()
  200. {
  201. $this->assertFalse(Carbon::createFromDate(2012, 7, 1, 'UTC')->local);
  202. $this->assertFalse(Carbon::createFromDate(2012, 7, 1, 'Europe/London')->local);
  203. }
  204. public function testGetUtcFalse()
  205. {
  206. $this->assertFalse(Carbon::createFromDate(2013, 1, 1, 'America/Toronto')->utc);
  207. $this->assertFalse(Carbon::createFromDate(2013, 1, 1, 'Europe/Paris')->utc);
  208. }
  209. public function testGetUtcTrue()
  210. {
  211. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Atlantic/Reykjavik')->utc);
  212. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Europe/Lisbon')->utc);
  213. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Africa/Casablanca')->utc);
  214. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Africa/Dakar')->utc);
  215. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Europe/Dublin')->utc);
  216. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Europe/London')->utc);
  217. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'UTC')->utc);
  218. $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'GMT')->utc);
  219. }
  220. public function testGetDstFalse()
  221. {
  222. $this->assertFalse(Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->dst);
  223. $this->assertFalse(Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->isDST());
  224. }
  225. public function testGetDstTrue()
  226. {
  227. $this->assertTrue(Carbon::createFromDate(2012, 7, 1, 'America/Toronto')->dst);
  228. $this->assertTrue(Carbon::createFromDate(2012, 7, 1, 'America/Toronto')->isDST());
  229. }
  230. public function testGetMidDayAt()
  231. {
  232. $d = Carbon::now();
  233. $this->assertSame(12, $d->getMidDayAt());
  234. }
  235. public function testOffsetForTorontoWithDST()
  236. {
  237. $this->assertSame(-18000, Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->offset);
  238. }
  239. public function testOffsetForTorontoNoDST()
  240. {
  241. $this->assertSame(-14400, Carbon::createFromDate(2012, 6, 1, 'America/Toronto')->offset);
  242. }
  243. public function testOffsetForGMT()
  244. {
  245. $this->assertSame(0, Carbon::createFromDate(2012, 6, 1, 'GMT')->offset);
  246. }
  247. public function testOffsetHoursForTorontoWithDST()
  248. {
  249. $this->assertSame(-5, Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->offsetHours);
  250. }
  251. public function testOffsetHoursForTorontoNoDST()
  252. {
  253. $this->assertSame(-4, Carbon::createFromDate(2012, 6, 1, 'America/Toronto')->offsetHours);
  254. }
  255. public function testOffsetHoursForGMT()
  256. {
  257. $this->assertSame(0, Carbon::createFromDate(2012, 6, 1, 'GMT')->offsetHours);
  258. }
  259. public function testIsLeapYearTrue()
  260. {
  261. $this->assertTrue(Carbon::createFromDate(2012, 1, 1)->isLeapYear());
  262. }
  263. public function testIsLeapYearFalse()
  264. {
  265. $this->assertFalse(Carbon::createFromDate(2011, 1, 1)->isLeapYear());
  266. }
  267. public function testIsLongYearTrue()
  268. {
  269. $this->assertTrue(Carbon::createFromDate(2015, 1, 1)->isLongYear());
  270. }
  271. public function testIsLongYearFalse()
  272. {
  273. $this->assertFalse(Carbon::createFromDate(2016, 1, 1)->isLongYear());
  274. }
  275. public function testWeekOfMonth()
  276. {
  277. $this->assertSame(5, Carbon::createFromDate(2012, 9, 30)->weekOfMonth);
  278. $this->assertSame(4, Carbon::createFromDate(2012, 9, 28)->weekOfMonth);
  279. $this->assertSame(3, Carbon::createFromDate(2012, 9, 20)->weekOfMonth);
  280. $this->assertSame(2, Carbon::createFromDate(2012, 9, 8)->weekOfMonth);
  281. $this->assertSame(1, Carbon::createFromDate(2012, 9, 1)->weekOfMonth);
  282. }
  283. public function testWeekNumberInMonthIsNotFromTheBeginning()
  284. {
  285. $this->assertSame(5, Carbon::createFromDate(2017, 2, 28)->weekNumberInMonth);
  286. $this->assertSame(5, Carbon::createFromDate(2017, 2, 27)->weekNumberInMonth);
  287. $this->assertSame(4, Carbon::createFromDate(2017, 2, 26)->weekNumberInMonth);
  288. $this->assertSame(4, Carbon::createFromDate(2017, 2, 20)->weekNumberInMonth);
  289. $this->assertSame(3, Carbon::createFromDate(2017, 2, 19)->weekNumberInMonth);
  290. $this->assertSame(3, Carbon::createFromDate(2017, 2, 13)->weekNumberInMonth);
  291. $this->assertSame(2, Carbon::createFromDate(2017, 2, 12)->weekNumberInMonth);
  292. $this->assertSame(2, Carbon::createFromDate(2017, 2, 6)->weekNumberInMonth);
  293. $this->assertSame(1, Carbon::createFromDate(2017, 2, 1)->weekNumberInMonth);
  294. }
  295. public function testWeekOfYearFirstWeek()
  296. {
  297. $this->assertSame(52, Carbon::createFromDate(2012, 1, 1)->weekOfYear);
  298. $this->assertSame(1, Carbon::createFromDate(2012, 1, 2)->weekOfYear);
  299. }
  300. public function testWeekOfYearLastWeek()
  301. {
  302. $this->assertSame(52, Carbon::createFromDate(2012, 12, 30)->weekOfYear);
  303. $this->assertSame(1, Carbon::createFromDate(2012, 12, 31)->weekOfYear);
  304. }
  305. public function testGetTimezone()
  306. {
  307. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  308. $this->assertSame('America/Toronto', $dt->timezone->getName());
  309. $dt = Carbon::createFromDate(2000, 1, 1, -5);
  310. $this->assertSame('America/Chicago', $dt->timezone->getName());
  311. $dt = Carbon::createFromDate(2000, 1, 1, '-5');
  312. $this->assertSame('-05:00', $dt->timezone->getName());
  313. }
  314. public function testGetTz()
  315. {
  316. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  317. $this->assertSame('America/Toronto', $dt->tz->getName());
  318. $dt = Carbon::createFromDate(2000, 1, 1, -5);
  319. $this->assertSame('America/Chicago', $dt->tz->getName());
  320. $dt = Carbon::createFromDate(2000, 1, 1, '-5');
  321. $this->assertSame('-05:00', $dt->tz->getName());
  322. }
  323. public function testGetTimezoneName()
  324. {
  325. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  326. $this->assertSame('America/Toronto', $dt->timezoneName);
  327. $dt = Carbon::createFromDate(2000, 1, 1, -5);
  328. $this->assertSame('America/Chicago', $dt->timezoneName);
  329. $dt = Carbon::createFromDate(2000, 1, 1, '-5');
  330. $this->assertSame('-05:00', $dt->timezoneName);
  331. }
  332. public function testGetTzName()
  333. {
  334. $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  335. $this->assertSame('America/Toronto', $dt->tzName);
  336. $dt = Carbon::createFromDate(2000, 1, 1, -5);
  337. $this->assertSame('America/Chicago', $dt->timezoneName);
  338. $dt = Carbon::createFromDate(2000, 1, 1, '-5');
  339. $this->assertSame('-05:00', $dt->timezoneName);
  340. }
  341. public function testShortDayName()
  342. {
  343. $dt = Carbon::createFromDate(2018, 8, 6);
  344. $this->assertSame('Mon', $dt->shortDayName);
  345. $this->assertSame('lun.', $dt->locale('fr')->shortDayName);
  346. }
  347. public function testMinDayName()
  348. {
  349. $dt = Carbon::createFromDate(2018, 8, 6);
  350. $this->assertSame('Mo', $dt->minDayName);
  351. $this->assertSame('lu', $dt->locale('fr')->minDayName);
  352. }
  353. public function testShortMonthName()
  354. {
  355. $dt = Carbon::createFromDate(2018, 7, 6);
  356. $this->assertSame('Jul', $dt->shortMonthName);
  357. $this->assertSame('juil.', $dt->locale('fr')->shortMonthName);
  358. }
  359. public function testGetDays()
  360. {
  361. $days = [
  362. Carbon::SUNDAY => 'Sunday',
  363. Carbon::MONDAY => 'Monday',
  364. Carbon::TUESDAY => 'Tuesday',
  365. Carbon::WEDNESDAY => 'Wednesday',
  366. Carbon::THURSDAY => 'Thursday',
  367. Carbon::FRIDAY => 'Friday',
  368. Carbon::SATURDAY => 'Saturday',
  369. ];
  370. $this->assertSame($days, Carbon::getDays());
  371. }
  372. }