IsTest.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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\Carbon;
  12. use BadMethodCallException;
  13. use Carbon\Carbon;
  14. use DateTime;
  15. use Generator;
  16. use InvalidArgumentException;
  17. use stdClass;
  18. use Tests\AbstractTestCase;
  19. class IsTest extends AbstractTestCase
  20. {
  21. public function testIsWeekdayTrue()
  22. {
  23. $this->assertTrue(Carbon::createFromDate(2012, 1, 2)->isWeekday());
  24. }
  25. public function testIsWeekdayFalse()
  26. {
  27. $this->assertFalse(Carbon::createFromDate(2012, 1, 1)->isWeekday());
  28. }
  29. public function testIsWeekendTrue()
  30. {
  31. $this->assertTrue(Carbon::createFromDate(2012, 1, 1)->isWeekend());
  32. }
  33. public function testIsWeekendFalse()
  34. {
  35. $this->assertFalse(Carbon::createFromDate(2012, 1, 2)->isWeekend());
  36. }
  37. public function testIsYesterdayTrue()
  38. {
  39. $this->assertTrue(Carbon::now()->subDay()->isYesterday());
  40. }
  41. public function testIsYesterdayFalseWithToday()
  42. {
  43. $this->assertFalse(Carbon::now()->endOfDay()->isYesterday());
  44. }
  45. public function testIsYesterdayFalseWith2Days()
  46. {
  47. $this->assertFalse(Carbon::now()->subDays(2)->startOfDay()->isYesterday());
  48. }
  49. public function testIsTodayTrue()
  50. {
  51. $this->assertTrue(Carbon::now()->isToday());
  52. }
  53. public function testIsCurrentWeek()
  54. {
  55. $this->assertFalse(Carbon::now()->subWeek()->isCurrentWeek());
  56. $this->assertFalse(Carbon::now()->addWeek()->isCurrentWeek());
  57. $this->assertTrue(Carbon::now()->isCurrentWeek());
  58. $this->assertTrue(Carbon::now()->startOfWeek()->isCurrentWeek());
  59. $this->assertTrue(Carbon::now()->endOfWeek()->isCurrentWeek());
  60. }
  61. public function testIsSameWeek()
  62. {
  63. $this->assertFalse(Carbon::now()->subWeek()->isSameWeek(Carbon::now()));
  64. $this->assertFalse(Carbon::now()->addWeek()->isSameWeek(Carbon::now()));
  65. $this->assertTrue(Carbon::now()->isSameWeek(Carbon::now()));
  66. $this->assertTrue(Carbon::now()->startOfWeek()->isSameWeek(Carbon::now()));
  67. $this->assertTrue(Carbon::now()->endOfWeek()->isSameWeek(Carbon::now()));
  68. $this->assertTrue(Carbon::parse('2019-01-01')->isSameWeek(Carbon::parse('2018-12-31')));
  69. }
  70. public function testIsNextWeekTrue()
  71. {
  72. $this->assertTrue(Carbon::now()->addWeek()->isNextWeek());
  73. }
  74. public function testIsLastWeekTrue()
  75. {
  76. $this->assertTrue(Carbon::now()->subWeek()->isLastWeek());
  77. }
  78. public function testIsNextWeekFalse()
  79. {
  80. /** @var mixed $date */
  81. $date = Carbon::now();
  82. $this->assertFalse($date->addWeek(2)->isNextWeek());
  83. }
  84. public function testIsLastWeekFalse()
  85. {
  86. /** @var mixed $date */
  87. $date = Carbon::now();
  88. $this->assertFalse($date->subWeek(2)->isLastWeek());
  89. }
  90. public function testIsNextQuarterTrue()
  91. {
  92. $this->assertTrue(Carbon::now()->addQuarterNoOverflow()->isNextQuarter());
  93. }
  94. public function testIsLastQuarterTrue()
  95. {
  96. $this->assertTrue(Carbon::now()->subQuarterNoOverflow()->isLastQuarter());
  97. }
  98. public function testIsNextQuarterFalse()
  99. {
  100. $this->assertFalse(Carbon::now()->addQuartersNoOverflow(2)->isNextQuarter());
  101. }
  102. public function testIsLastQuarterFalse()
  103. {
  104. $this->assertFalse(Carbon::now()->subQuartersNoOverflow(2)->isLastQuarter());
  105. }
  106. public function testIsNextMonthTrue()
  107. {
  108. $this->assertTrue(Carbon::now()->addMonthNoOverflow()->isNextMonth());
  109. }
  110. public function testIsLastMonthTrue()
  111. {
  112. $this->assertTrue(Carbon::now()->subMonthNoOverflow()->isLastMonth());
  113. }
  114. public function testIsNextMonthFalse()
  115. {
  116. $this->assertFalse(Carbon::now()->addMonthsNoOverflow(2)->isNextMonth());
  117. }
  118. public function testIsLastMonthFalse()
  119. {
  120. Carbon::setTestNow(Carbon::create(2018, 5, 31));
  121. $this->assertFalse(Carbon::now()->subMonthsNoOverflow(2)->isLastMonth());
  122. }
  123. public function testIsNextYearTrue()
  124. {
  125. $this->assertTrue(Carbon::now()->addYear()->isNextYear());
  126. }
  127. public function testIsLastYearTrue()
  128. {
  129. $this->assertTrue(Carbon::now()->subYear()->isLastYear());
  130. }
  131. public function testIsNextYearFalse()
  132. {
  133. /** @var mixed $date */
  134. $date = Carbon::now();
  135. $this->assertFalse($date->addYear(2)->isNextYear());
  136. }
  137. public function testIsLastYearFalse()
  138. {
  139. /** @var mixed $date */
  140. $date = Carbon::now();
  141. $this->assertFalse($date->subYear(2)->isLastYear());
  142. }
  143. public function testIsTodayFalseWithYesterday()
  144. {
  145. $this->assertFalse(Carbon::now()->subDay()->endOfDay()->isToday());
  146. }
  147. public function testIsTodayFalseWithTomorrow()
  148. {
  149. $this->assertFalse(Carbon::now()->addDay()->startOfDay()->isToday());
  150. }
  151. public function testIsTodayWithTimezone()
  152. {
  153. $this->assertTrue(Carbon::now('Asia/Tokyo')->isToday());
  154. }
  155. public function testIsTomorrowTrue()
  156. {
  157. $this->assertTrue(Carbon::now()->addDay()->isTomorrow());
  158. }
  159. public function testIsTomorrowFalseWithToday()
  160. {
  161. $this->assertFalse(Carbon::now()->endOfDay()->isTomorrow());
  162. }
  163. public function testIsTomorrowFalseWith2Days()
  164. {
  165. $this->assertFalse(Carbon::now()->addDays(2)->startOfDay()->isTomorrow());
  166. }
  167. public function testIsFutureTrue()
  168. {
  169. $this->assertTrue(Carbon::now()->addSecond()->isFuture());
  170. }
  171. public function testIsFutureFalse()
  172. {
  173. $this->assertFalse(Carbon::now()->isFuture());
  174. }
  175. public function testIsFutureFalseInThePast()
  176. {
  177. $this->assertFalse(Carbon::now()->subSecond()->isFuture());
  178. }
  179. public function testIsPastTrue()
  180. {
  181. $this->assertTrue(Carbon::now()->subSecond()->isPast());
  182. }
  183. public function testIsPastFalse()
  184. {
  185. $this->assertFalse(Carbon::now()->addSecond()->isPast());
  186. }
  187. public function testNowIsPastFalse()
  188. {
  189. $this->assertFalse(Carbon::now()->isPast());
  190. }
  191. public function testIsLeapYearTrue()
  192. {
  193. $this->assertTrue(Carbon::createFromDate(2016, 1, 1)->isLeapYear());
  194. }
  195. public function testIsLeapYearFalse()
  196. {
  197. $this->assertFalse(Carbon::createFromDate(2014, 1, 1)->isLeapYear());
  198. }
  199. public function testIsCurrentYearTrue()
  200. {
  201. $this->assertTrue(Carbon::now()->isCurrentYear());
  202. }
  203. public function testIsCurrentYearFalse()
  204. {
  205. $this->assertFalse(Carbon::now()->subYear()->isCurrentYear());
  206. }
  207. public function testIsSameYearTrue()
  208. {
  209. $this->assertTrue(Carbon::now()->isSameYear(Carbon::now()));
  210. }
  211. public function testIsSameYearFalse()
  212. {
  213. $this->assertFalse(Carbon::now()->isSameYear(Carbon::now()->subYear()));
  214. }
  215. public function testIsCurrentQuarterTrue()
  216. {
  217. $this->assertTrue(Carbon::now()->isCurrentQuarter());
  218. }
  219. public function testIsCurrentQuarterFalse()
  220. {
  221. Carbon::useMonthsOverflow(false);
  222. $this->assertFalse(Carbon::now()->subQuarter()->isCurrentQuarter());
  223. Carbon::resetMonthsOverflow();
  224. }
  225. public function testIsSameQuarterTrue()
  226. {
  227. $this->assertTrue(Carbon::now()->isSameQuarter(Carbon::now()));
  228. }
  229. public function testIsSameQuarterTrueWithDateTime()
  230. {
  231. $this->assertTrue(Carbon::now()->isSameQuarter(new DateTime()));
  232. }
  233. public function testIsSameQuarterFalse()
  234. {
  235. Carbon::useMonthsOverflow(false);
  236. $this->assertFalse(Carbon::now()->isSameQuarter(Carbon::now()->subQuarter()));
  237. Carbon::resetMonthsOverflow();
  238. }
  239. public function testIsSameQuarterFalseWithDateTime()
  240. {
  241. $now = Carbon::now();
  242. $dt = new DateTime();
  243. $dt->modify((Carbon::MONTHS_PER_QUARTER * -1).' month');
  244. if ($dt->format('d') !== $now->format('d')) {
  245. $dt->modify('last day of previous month');
  246. }
  247. $this->assertFalse($now->isSameQuarter($dt));
  248. }
  249. public function testIsSameQuarterAndYearTrue()
  250. {
  251. $this->assertTrue(Carbon::now()->isSameQuarter(Carbon::now(), true));
  252. }
  253. public function testIsSameQuarterAndYearTrueWithDateTime()
  254. {
  255. $this->assertTrue(Carbon::now()->isSameQuarter(new DateTime(), true));
  256. }
  257. public function testIsSameQuarterAndYearFalse()
  258. {
  259. $this->assertFalse(Carbon::now()->isSameQuarter(Carbon::now()->subYear(), true));
  260. }
  261. public function testIsSameQuarterAndYearFalseWithDateTime()
  262. {
  263. $dt = new DateTime();
  264. $dt->modify('-1 year');
  265. $this->assertFalse(Carbon::now()->isSameQuarter($dt, true));
  266. }
  267. public function testIsCurrentMonth()
  268. {
  269. $this->assertTrue(Carbon::now()->isCurrentMonth());
  270. $dt = Carbon::now();
  271. $dt->modify(Carbon::now()->year.$dt->format('-m-').'01');
  272. $this->assertTrue($dt->isCurrentMonth());
  273. $dt->modify((Carbon::now()->year + 1).$dt->format('-m-').'28');
  274. $this->assertFalse($dt->isCurrentMonth());
  275. }
  276. public function testIsCurrentMonthFalse()
  277. {
  278. $this->assertFalse(Carbon::now()->day(15)->subMonth()->isCurrentMonth());
  279. $this->assertFalse(Carbon::now()->day(15)->addYear()->isCurrentMonth());
  280. }
  281. public function testIsSameMonth()
  282. {
  283. $this->assertTrue(Carbon::now()->isSameMonth(Carbon::now()));
  284. $dt = Carbon::now();
  285. for ($year = 1990; $year < Carbon::now()->year; $year++) {
  286. $dt->modify($year.$dt->format('-m-').'01');
  287. $this->assertTrue(Carbon::now()->isSameMonth($dt, false));
  288. $dt->modify($year.$dt->format('-m-').'28');
  289. $this->assertTrue(Carbon::now()->isSameMonth($dt, false));
  290. }
  291. }
  292. public function testIsSameMonthTrueWithDateTime()
  293. {
  294. $this->assertTrue(Carbon::now()->isSameMonth(new DateTime()));
  295. $dt = new DateTime();
  296. for ($year = 1990; $year < 2200; $year++) {
  297. $dt->modify($year.$dt->format('-m-').'01');
  298. $this->assertTrue(Carbon::now()->isSameMonth($dt, false));
  299. $dt->modify($year.$dt->format('-m-').'28');
  300. $this->assertTrue(Carbon::now()->isSameMonth($dt, false));
  301. }
  302. }
  303. public function testIsSameMonthOfSameYear()
  304. {
  305. $this->assertFalse(Carbon::now()->isSameMonth(Carbon::now()->day(15)->subMonth()));
  306. $this->assertTrue(Carbon::now()->isSameMonth(Carbon::now()));
  307. $dt = Carbon::now();
  308. for ($year = 1990; $year < Carbon::now()->year; $year++) {
  309. $dt->modify($year.$dt->format('-m-').'01');
  310. $this->assertFalse(Carbon::now()->isSameMonth($dt, true));
  311. $dt->modify($year.$dt->format('-m-').'28');
  312. $this->assertFalse(Carbon::now()->isSameMonth($dt, true));
  313. }
  314. $year = Carbon::now()->year;
  315. $dt->modify($year.$dt->format('-m-').'01');
  316. $this->assertTrue(Carbon::now()->isSameMonth($dt, true));
  317. $dt->modify($year.$dt->format('-m-').'28');
  318. $this->assertTrue(Carbon::now()->isSameMonth($dt, true));
  319. for ($year = Carbon::now()->year + 1; $year < 2200; $year++) {
  320. $dt->modify($year.$dt->format('-m-').'01');
  321. $this->assertFalse(Carbon::now()->isSameMonth($dt, true));
  322. $dt->modify($year.$dt->format('-m-').'28');
  323. $this->assertFalse(Carbon::now()->isSameMonth($dt, true));
  324. }
  325. }
  326. public function testIsSameMonthFalseWithDateTime()
  327. {
  328. $dt = new DateTime();
  329. $dt->modify('-2 months');
  330. $this->assertFalse(Carbon::now()->isSameMonth($dt));
  331. }
  332. public function testIsSameMonthAndYearTrue()
  333. {
  334. $this->assertTrue(Carbon::now()->isSameMonth(Carbon::now(), true));
  335. $dt = Carbon::now();
  336. $dt->modify($dt->format('Y-m-').'01');
  337. $this->assertTrue(Carbon::now()->isSameMonth($dt, true));
  338. $dt->modify($dt->format('Y-m-').'28');
  339. $this->assertTrue(Carbon::now()->isSameMonth($dt, true));
  340. }
  341. public function testIsSameMonthAndYearTrueWithDateTime()
  342. {
  343. $this->assertTrue(Carbon::now()->isSameMonth(new DateTime(), true));
  344. $dt = new DateTime();
  345. $dt->modify($dt->format('Y-m-').'01');
  346. $this->assertTrue(Carbon::now()->isSameMonth($dt, true));
  347. $dt->modify($dt->format('Y-m-').'28');
  348. $this->assertTrue(Carbon::now()->isSameMonth($dt, true));
  349. }
  350. public function testIsSameMonthAndYearFalse()
  351. {
  352. $this->assertFalse(Carbon::now()->isSameMonth(Carbon::now()->subYear(), true));
  353. }
  354. public function testIsSameMonthAndYearFalseWithDateTime()
  355. {
  356. $dt = new DateTime();
  357. $dt->modify('-1 year');
  358. $this->assertFalse(Carbon::now()->isSameMonth($dt, true));
  359. }
  360. public function testIsSameDayTrue()
  361. {
  362. $current = Carbon::createFromDate(2012, 1, 2);
  363. $this->assertTrue($current->isSameDay(Carbon::createFromDate(2012, 1, 2)));
  364. $this->assertTrue($current->isSameDay(Carbon::create(2012, 1, 2, 23, 59, 59)));
  365. }
  366. public function testIsSameDayWithString()
  367. {
  368. $current = Carbon::createFromDate(2012, 1, 2);
  369. $this->assertTrue($current->isSameDay('2012-01-02 15:00:25'));
  370. $this->assertTrue($current->isSameDay('2012-01-02'));
  371. }
  372. public function testIsSameDayTrueWithDateTime()
  373. {
  374. $current = Carbon::createFromDate(2012, 1, 2);
  375. $this->assertTrue($current->isSameDay(new DateTime('2012-01-02')));
  376. $this->assertTrue($current->isSameDay(new DateTime('2012-01-02 23:59:59')));
  377. }
  378. public function testIsSameDayFalse()
  379. {
  380. $current = Carbon::createFromDate(2012, 1, 2);
  381. $this->assertFalse($current->isSameDay(Carbon::createFromDate(2012, 1, 3)));
  382. $this->assertFalse($current->isSameDay(Carbon::createFromDate(2012, 6, 2)));
  383. }
  384. public function testIsSameDayFalseWithDateTime()
  385. {
  386. $current = Carbon::createFromDate(2012, 1, 2);
  387. $this->assertFalse($current->isSameDay(new DateTime('2012-01-03')));
  388. $this->assertFalse($current->isSameDay(new DateTime('2012-05-02')));
  389. }
  390. public function testIsCurrentDayTrue()
  391. {
  392. $this->assertTrue(Carbon::now()->isCurrentDay());
  393. $this->assertTrue(Carbon::now()->hour(0)->isCurrentDay());
  394. $this->assertTrue(Carbon::now()->hour(23)->isCurrentDay());
  395. }
  396. public function testIsCurrentDayFalse()
  397. {
  398. $this->assertFalse(Carbon::now()->subDay()->isCurrentDay());
  399. $this->assertFalse(Carbon::now()->subMonth()->isCurrentDay());
  400. }
  401. public function testIsSameHourTrue()
  402. {
  403. $current = Carbon::create(2018, 5, 6, 12);
  404. $this->assertTrue($current->isSameHour(Carbon::create(2018, 5, 6, 12)));
  405. $this->assertTrue($current->isSameHour(Carbon::create(2018, 5, 6, 12, 59, 59)));
  406. }
  407. public function testIsSameHourTrueWithDateTime()
  408. {
  409. $current = Carbon::create(2018, 5, 6, 12);
  410. $this->assertTrue($current->isSameHour(new DateTime('2018-05-06T12:00:00')));
  411. $this->assertTrue($current->isSameHour(new DateTime('2018-05-06T12:59:59')));
  412. }
  413. public function testIsSameHourFalse()
  414. {
  415. $current = Carbon::create(2018, 5, 6, 12);
  416. $this->assertFalse($current->isSameHour(Carbon::create(2018, 5, 6, 13)));
  417. $this->assertFalse($current->isSameHour(Carbon::create(2018, 5, 5, 12)));
  418. }
  419. public function testIsSameHourFalseWithDateTime()
  420. {
  421. $current = Carbon::create(2018, 5, 6, 12);
  422. $this->assertFalse($current->isSameHour(new DateTime('2018-05-06T13:00:00')));
  423. $this->assertFalse($current->isSameHour(new DateTime('2018-06-06T12:00:00')));
  424. }
  425. public function testIsCurrentHourTrue()
  426. {
  427. $this->assertTrue(Carbon::now()->isCurrentHour());
  428. $this->assertTrue(Carbon::now()->second(1)->isCurrentHour());
  429. $this->assertTrue(Carbon::now()->second(12)->isCurrentHour());
  430. $this->assertTrue(Carbon::now()->minute(0)->isCurrentHour());
  431. $this->assertTrue(Carbon::now()->minute(59)->second(59)->isCurrentHour());
  432. }
  433. public function testIsCurrentHourFalse()
  434. {
  435. $this->assertFalse(Carbon::now()->subHour()->isCurrentHour());
  436. $this->assertFalse(Carbon::now()->subDay()->isCurrentHour());
  437. }
  438. public function testIsSameMinuteTrue()
  439. {
  440. $current = Carbon::create(2018, 5, 6, 12, 30);
  441. $this->assertTrue($current->isSameMinute(Carbon::create(2018, 5, 6, 12, 30)));
  442. $current = Carbon::create(2018, 5, 6, 12, 30, 15);
  443. $this->assertTrue($current->isSameMinute(Carbon::create(2018, 5, 6, 12, 30, 45)));
  444. }
  445. public function testIsSameMinuteTrueWithDateTime()
  446. {
  447. $current = Carbon::create(2018, 5, 6, 12, 30);
  448. $this->assertTrue($current->isSameMinute(new DateTime('2018-05-06T12:30:00')));
  449. $current = Carbon::create(2018, 5, 6, 12, 30, 20);
  450. $this->assertTrue($current->isSameMinute(new DateTime('2018-05-06T12:30:40')));
  451. }
  452. public function testIsSameMinuteFalse()
  453. {
  454. $current = Carbon::create(2018, 5, 6, 12, 30);
  455. $this->assertFalse($current->isSameMinute(Carbon::create(2018, 5, 6, 13, 31)));
  456. $this->assertFalse($current->isSameMinute(Carbon::create(2019, 5, 6, 13, 30)));
  457. }
  458. public function testIsSameMinuteFalseWithDateTime()
  459. {
  460. $current = Carbon::create(2018, 5, 6, 12, 30);
  461. $this->assertFalse($current->isSameMinute(new DateTime('2018-05-06T13:31:00')));
  462. $this->assertFalse($current->isSameMinute(new DateTime('2018-05-06T17:30:00')));
  463. }
  464. public function testIsCurrentMinuteTrue()
  465. {
  466. $this->assertTrue(Carbon::now()->isCurrentMinute());
  467. $this->assertTrue(Carbon::now()->second(0)->isCurrentMinute());
  468. $this->assertTrue(Carbon::now()->second(59)->isCurrentMinute());
  469. }
  470. public function testIsCurrentMinuteFalse()
  471. {
  472. $this->assertFalse(Carbon::now()->subMinute()->isCurrentMinute());
  473. $this->assertFalse(Carbon::now()->subHour()->isCurrentMinute());
  474. }
  475. public function testIsSameSecondTrue()
  476. {
  477. $current = Carbon::create(2018, 5, 6, 12, 30, 13);
  478. $other = Carbon::create(2018, 5, 6, 12, 30, 13);
  479. $this->assertTrue($current->isSameSecond($other));
  480. $this->assertTrue($current->modify($current->hour.':'.$current->minute.':'.$current->second.'.0')->isSameSecond($other));
  481. $this->assertTrue($current->modify($current->hour.':'.$current->minute.':'.$current->second.'.999999')->isSameSecond($other));
  482. }
  483. public function testIsSameSecondTrueWithDateTime()
  484. {
  485. $current = Carbon::create(2018, 5, 6, 12, 30, 13);
  486. $this->assertTrue($current->isSameSecond(new DateTime('2018-05-06T12:30:13')));
  487. }
  488. public function testIsSameSecondFalse()
  489. {
  490. $current = Carbon::create(2018, 5, 6, 12, 30, 13);
  491. $this->assertFalse($current->isSameSecond(Carbon::create(2018, 5, 6, 12, 30, 55)));
  492. $this->assertFalse($current->isSameSecond(Carbon::create(2018, 5, 6, 14, 30, 13)));
  493. }
  494. public function testIsSameSecondFalseWithDateTime()
  495. {
  496. $current = Carbon::create(2018, 5, 6, 12, 30, 13);
  497. $this->assertFalse($current->isSameSecond(new DateTime('2018-05-06T13:30:54')));
  498. $this->assertFalse($current->isSameSecond(new DateTime('2018-05-06T13:36:13')));
  499. }
  500. public function testIsCurrentSecondTrue()
  501. {
  502. $this->assertTrue(Carbon::now()->isCurrentSecond());
  503. $now = Carbon::now();
  504. $this->assertTrue($now->modify($now->hour.':'.$now->minute.':'.$now->second.'.0')->isCurrentSecond());
  505. $this->assertTrue($now->modify($now->hour.':'.$now->minute.':'.$now->second.'.999999')->isCurrentSecond());
  506. }
  507. public function testIsCurrentSecondFalse()
  508. {
  509. $this->assertFalse(Carbon::now()->subSecond()->isCurrentSecond());
  510. $this->assertFalse(Carbon::now()->subDay()->isCurrentSecond());
  511. }
  512. public function testIsSameMicrosecond()
  513. {
  514. $current = new Carbon('2018-05-06T13:30:54.123456');
  515. $this->assertTrue($current->isSameMicrosecond(new DateTime('2018-05-06T13:30:54.123456')));
  516. $this->assertFalse($current->isSameMicrosecond(new DateTime('2018-05-06T13:30:54.123457')));
  517. $this->assertFalse($current->isSameMicrosecond(new DateTime('2019-05-06T13:30:54.123456')));
  518. $this->assertFalse($current->isSameMicrosecond(new DateTime('2018-05-06T13:30:55.123456')));
  519. $this->assertTrue($current->isSameSecond($current->copy()));
  520. $this->assertTrue(Carbon::now()->isCurrentMicrosecond());
  521. $this->assertFalse(Carbon::now()->subMicrosecond()->isCurrentMicrosecond());
  522. $this->assertFalse(Carbon::now()->isLastMicrosecond());
  523. $this->assertTrue(Carbon::now()->subMicrosecond()->isLastMicrosecond());
  524. $this->assertFalse(Carbon::now()->isNextMicrosecond());
  525. $this->assertTrue(Carbon::now()->addMicrosecond()->isNextMicrosecond());
  526. $this->assertTrue(Carbon::now()->subMicroseconds(Carbon::MICROSECONDS_PER_SECOND)->isLastSecond());
  527. $this->assertSame(4, Carbon::now()->subMicroseconds(4 * Carbon::MICROSECONDS_PER_SECOND)->diffInSeconds(Carbon::now()));
  528. }
  529. public function testIsDayOfWeek()
  530. {
  531. // True in the past past
  532. $this->assertTrue(Carbon::createFromDate(2015, 5, 31)->isDayOfWeek(0));
  533. $this->assertTrue(Carbon::createFromDate(2015, 6, 21)->isDayOfWeek(0));
  534. $this->assertTrue(Carbon::now()->subWeek()->previous(Carbon::SUNDAY)->isDayOfWeek(0));
  535. $this->assertTrue(Carbon::now()->subWeek()->previous(Carbon::SUNDAY)->isDayOfWeek('sunday'));
  536. $this->assertTrue(Carbon::now()->subWeek()->previous(Carbon::SUNDAY)->isDayOfWeek('SUNDAY'));
  537. // True in the future
  538. $this->assertTrue(Carbon::now()->addWeek()->previous(Carbon::SUNDAY)->isDayOfWeek(0));
  539. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::SUNDAY)->isDayOfWeek(0));
  540. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::SUNDAY)->isDayOfWeek('sunday'));
  541. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::SUNDAY)->isDayOfWeek('SUNDAY'));
  542. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::MONDAY)->isDayOfWeek('monday'));
  543. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::MONDAY)->isDayOfWeek('MONDAY'));
  544. // False in the past
  545. $this->assertFalse(Carbon::now()->subWeek()->previous(Carbon::MONDAY)->isDayOfWeek(0));
  546. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::MONDAY)->isDayOfWeek(0));
  547. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::MONDAY)->isDayOfWeek('sunday'));
  548. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::MONDAY)->isDayOfWeek('SUNDAY'));
  549. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::SUNDAY)->isDayOfWeek('monday'));
  550. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::SUNDAY)->isDayOfWeek('MONDAY'));
  551. // False in the future
  552. $this->assertFalse(Carbon::now()->addWeek()->previous(Carbon::MONDAY)->isDayOfWeek(0));
  553. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::MONDAY)->isDayOfWeek(0));
  554. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::MONDAY)->isDayOfWeek('sunday'));
  555. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::MONDAY)->isDayOfWeek('SUNDAY'));
  556. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::SUNDAY)->isDayOfWeek('monday'));
  557. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::SUNDAY)->isDayOfWeek('MONDAY'));
  558. }
  559. public function testIsSameAs()
  560. {
  561. $current = Carbon::createFromDate(2012, 1, 2);
  562. $this->assertTrue($current->isSameAs('c', $current));
  563. }
  564. public function testIsSameAsWithInvalidArgument()
  565. {
  566. $this->expectExceptionObject(new InvalidArgumentException(
  567. 'Expected null, string, DateTime or DateTimeInterface, stdClass given'
  568. ));
  569. $current = Carbon::createFromDate(2012, 1, 2);
  570. $current->isSameAs('Y-m-d', new stdClass());
  571. }
  572. public function testIsSunday()
  573. {
  574. // True in the past past
  575. $this->assertTrue(Carbon::createFromDate(2015, 5, 31)->isSunday());
  576. $this->assertTrue(Carbon::createFromDate(2015, 6, 21)->isSunday());
  577. $this->assertTrue(Carbon::now()->subWeek()->previous(Carbon::SUNDAY)->isSunday());
  578. $this->assertTrue(Carbon::now()->subWeek()->previous('Sunday')->isSunday());
  579. // True in the future
  580. $this->assertTrue(Carbon::now()->addWeek()->previous(Carbon::SUNDAY)->isSunday());
  581. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::SUNDAY)->isSunday());
  582. // False in the past
  583. $this->assertFalse(Carbon::now()->subWeek()->previous(Carbon::MONDAY)->isSunday());
  584. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::MONDAY)->isSunday());
  585. // False in the future
  586. $this->assertFalse(Carbon::now()->addWeek()->previous(Carbon::MONDAY)->isSunday());
  587. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::MONDAY)->isSunday());
  588. }
  589. public function testIsMonday()
  590. {
  591. // True in the past past
  592. $this->assertTrue(Carbon::createFromDate(2015, 6, 1)->isMonday());
  593. $this->assertTrue(Carbon::now()->subWeek()->previous(Carbon::MONDAY)->isMonday());
  594. // True in the future
  595. $this->assertTrue(Carbon::now()->addWeek()->previous(Carbon::MONDAY)->isMonday());
  596. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::MONDAY)->isMonday());
  597. // False in the past
  598. $this->assertFalse(Carbon::now()->subWeek()->previous(Carbon::TUESDAY)->isMonday());
  599. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::TUESDAY)->isMonday());
  600. // False in the future
  601. $this->assertFalse(Carbon::now()->addWeek()->previous(Carbon::TUESDAY)->isMonday());
  602. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::TUESDAY)->isMonday());
  603. }
  604. public function testIsTuesday()
  605. {
  606. // True in the past past
  607. $this->assertTrue(Carbon::createFromDate(2015, 6, 2)->isTuesday());
  608. $this->assertTrue(Carbon::now()->subWeek()->previous(Carbon::TUESDAY)->isTuesday());
  609. // True in the future
  610. $this->assertTrue(Carbon::now()->addWeek()->previous(Carbon::TUESDAY)->isTuesday());
  611. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::TUESDAY)->isTuesday());
  612. // False in the past
  613. $this->assertFalse(Carbon::now()->subWeek()->previous(Carbon::WEDNESDAY)->isTuesday());
  614. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::WEDNESDAY)->isTuesday());
  615. // False in the future
  616. $this->assertFalse(Carbon::now()->addWeek()->previous(Carbon::WEDNESDAY)->isTuesday());
  617. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::WEDNESDAY)->isTuesday());
  618. }
  619. public function testIsWednesday()
  620. {
  621. // True in the past past
  622. $this->assertTrue(Carbon::createFromDate(2015, 6, 3)->isWednesday());
  623. $this->assertTrue(Carbon::now()->subWeek()->previous(Carbon::WEDNESDAY)->isWednesday());
  624. // True in the future
  625. $this->assertTrue(Carbon::now()->addWeek()->previous(Carbon::WEDNESDAY)->isWednesday());
  626. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::WEDNESDAY)->isWednesday());
  627. // False in the past
  628. $this->assertFalse(Carbon::now()->subWeek()->previous(Carbon::THURSDAY)->isWednesday());
  629. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::THURSDAY)->isWednesday());
  630. // False in the future
  631. $this->assertFalse(Carbon::now()->addWeek()->previous(Carbon::THURSDAY)->isWednesday());
  632. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::THURSDAY)->isWednesday());
  633. }
  634. public function testIsThursday()
  635. {
  636. // True in the past past
  637. $this->assertTrue(Carbon::createFromDate(2015, 6, 4)->isThursday());
  638. $this->assertTrue(Carbon::now()->subWeek()->previous(Carbon::THURSDAY)->isThursday());
  639. // True in the future
  640. $this->assertTrue(Carbon::now()->addWeek()->previous(Carbon::THURSDAY)->isThursday());
  641. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::THURSDAY)->isThursday());
  642. // False in the past
  643. $this->assertFalse(Carbon::now()->subWeek()->previous(Carbon::FRIDAY)->isThursday());
  644. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::FRIDAY)->isThursday());
  645. // False in the future
  646. $this->assertFalse(Carbon::now()->addWeek()->previous(Carbon::FRIDAY)->isThursday());
  647. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::FRIDAY)->isThursday());
  648. }
  649. public function testIsFriday()
  650. {
  651. // True in the past past
  652. $this->assertTrue(Carbon::createFromDate(2015, 6, 5)->isFriday());
  653. $this->assertTrue(Carbon::now()->subWeek()->previous(Carbon::FRIDAY)->isFriday());
  654. // True in the future
  655. $this->assertTrue(Carbon::now()->addWeek()->previous(Carbon::FRIDAY)->isFriday());
  656. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::FRIDAY)->isFriday());
  657. // False in the past
  658. $this->assertFalse(Carbon::now()->subWeek()->previous(Carbon::SATURDAY)->isFriday());
  659. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::SATURDAY)->isFriday());
  660. // False in the future
  661. $this->assertFalse(Carbon::now()->addWeek()->previous(Carbon::SATURDAY)->isFriday());
  662. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::SATURDAY)->isFriday());
  663. }
  664. public function testIsSaturday()
  665. {
  666. // True in the past past
  667. $this->assertTrue(Carbon::createFromDate(2015, 6, 6)->isSaturday());
  668. $this->assertTrue(Carbon::now()->subWeek()->previous(Carbon::SATURDAY)->isSaturday());
  669. // True in the future
  670. $this->assertTrue(Carbon::now()->addWeek()->previous(Carbon::SATURDAY)->isSaturday());
  671. $this->assertTrue(Carbon::now()->addMonth()->previous(Carbon::SATURDAY)->isSaturday());
  672. // False in the past
  673. $this->assertFalse(Carbon::now()->subWeek()->previous(Carbon::SUNDAY)->isSaturday());
  674. $this->assertFalse(Carbon::now()->subMonth()->previous(Carbon::SUNDAY)->isSaturday());
  675. // False in the future
  676. $this->assertFalse(Carbon::now()->addWeek()->previous(Carbon::SUNDAY)->isSaturday());
  677. $this->assertFalse(Carbon::now()->addMonth()->previous(Carbon::SUNDAY)->isSaturday());
  678. }
  679. public function testIsStartOfDay()
  680. {
  681. $this->assertTrue(Carbon::parse('00:00:00')->isStartOfDay(false));
  682. $this->assertTrue(Carbon::parse('00:00:00.999999')->isStartOfDay(false));
  683. $this->assertTrue(Carbon::now()->startOfDay()->isStartOfDay(false));
  684. $this->assertFalse(Carbon::parse('15:30:45')->isStartOfDay(false));
  685. $this->assertFalse(Carbon::now()->endOfDay()->isStartOfDay(false));
  686. $this->assertTrue(Carbon::parse('00:00:00')->isStartOfDay());
  687. $this->assertTrue(Carbon::parse('00:00:00.999999')->isStartOfDay());
  688. $this->assertTrue(Carbon::now()->startOfDay()->isStartOfDay());
  689. $this->assertFalse(Carbon::parse('15:30:45')->isStartOfDay());
  690. $this->assertFalse(Carbon::now()->endOfDay()->isStartOfDay());
  691. }
  692. public function testIsStartOfDayWithMicroseconds()
  693. {
  694. $this->assertTrue(Carbon::parse('00:00:00')->isStartOfDay(true));
  695. $this->assertTrue(Carbon::now()->startOfDay()->isStartOfDay(true));
  696. $this->assertFalse(Carbon::parse('00:00:00.000001')->isStartOfDay(true));
  697. }
  698. public function testIsEndOfDay()
  699. {
  700. $this->assertTrue(Carbon::parse('23:59:59')->isEndOfDay(false));
  701. $this->assertTrue(Carbon::parse('23:59:59.000000')->isEndOfDay(false));
  702. $this->assertTrue(Carbon::now()->endOfDay()->isEndOfDay(false));
  703. $this->assertFalse(Carbon::parse('15:30:45')->isEndOfDay(false));
  704. $this->assertFalse(Carbon::now()->startOfDay()->isEndOfDay(false));
  705. $this->assertTrue(Carbon::parse('23:59:59')->isEndOfDay());
  706. $this->assertTrue(Carbon::parse('23:59:59.000000')->isEndOfDay());
  707. $this->assertTrue(Carbon::now()->endOfDay()->isEndOfDay());
  708. $this->assertFalse(Carbon::parse('15:30:45')->isEndOfDay());
  709. $this->assertFalse(Carbon::now()->startOfDay()->isEndOfDay());
  710. }
  711. public function testIsEndOfDayWithMicroseconds()
  712. {
  713. $this->assertTrue(Carbon::parse('23:59:59.999999')->isEndOfDay(true));
  714. $this->assertTrue(Carbon::now()->endOfDay()->isEndOfDay(true));
  715. $this->assertFalse(Carbon::parse('23:59:59')->isEndOfDay(true));
  716. $this->assertFalse(Carbon::parse('23:59:59.999998')->isEndOfDay(true));
  717. }
  718. public function testIsMidnight()
  719. {
  720. $this->assertTrue(Carbon::parse('00:00:00')->isMidnight());
  721. $this->assertFalse(Carbon::parse('15:30:45')->isMidnight());
  722. }
  723. public function testIsMidday()
  724. {
  725. $this->assertTrue(Carbon::parse('12:00:00')->isMidday());
  726. $this->assertFalse(Carbon::parse('15:30:45')->isMidday());
  727. }
  728. public function testHasFormat()
  729. {
  730. $this->assertTrue(Carbon::hasFormat('1975-05-01', 'Y-m-d'));
  731. $this->assertTrue(Carbon::hasFormat('12/30/2019', 'm/d/Y'));
  732. $this->assertTrue(Carbon::hasFormat('30/12/2019', 'd/m/Y'));
  733. $this->assertTrue(Carbon::hasFormat('Sun 21st', 'D jS'));
  734. $this->assertTrue(Carbon::hasFormat('2000-07-01T00:00:00+00:00', Carbon::ATOM));
  735. $this->assertTrue(Carbon::hasFormat('Y-01-30\\', '\\Y-m-d\\\\'));
  736. // @see https://github.com/briannesbitt/Carbon/issues/2180
  737. $this->assertTrue(Carbon::hasFormat('2020-09-01 12:00:00Europe/Moscow', 'Y-m-d H:i:se'));
  738. // Format failure
  739. $this->assertFalse(Carbon::hasFormat(null, 'd m Y'));
  740. $this->assertFalse(Carbon::hasFormat('1975-05-01', 'd m Y'));
  741. $this->assertFalse(Carbon::hasFormat('1975-01-30\\', '\\Y-m-d\\\\'));
  742. $this->assertFalse(Carbon::hasFormat('Foo 21st', 'D jS'));
  743. $this->assertFalse(Carbon::hasFormat('Sun 51st', 'D jS'));
  744. $this->assertFalse(Carbon::hasFormat('Sun 21xx', 'D jS'));
  745. // Regex failure
  746. $this->assertFalse(Carbon::hasFormat('1975-5-1', 'Y-m-d'));
  747. $this->assertFalse(Carbon::hasFormat('19-05-01', 'Y-m-d'));
  748. $this->assertFalse(Carbon::hasFormat('30/12/2019', 'm/d/Y'));
  749. $this->assertFalse(Carbon::hasFormat('12/30/2019', 'd/m/Y'));
  750. $this->assertFalse(Carbon::hasFormat('1975-05-01', 'Y-m-d!'));
  751. $this->assertFalse(Carbon::hasFormat('1975-05-01', 'Y-m-d|'));
  752. $this->assertFalse(Carbon::hasFormat('1975-05-01', 'Y-*-d'));
  753. $this->assertTrue(Carbon::hasFormat('2012-12-04 22:59.32130', 'Y-m-d H:s.vi'));
  754. }
  755. public function testHasFormatWithModifiers()
  756. {
  757. $this->assertTrue(Carbon::hasFormatWithModifiers('2021-05-03T00:00:00+02:00', 'Y-m-d\TH:i:sp'));
  758. $this->assertTrue(Carbon::hasFormatWithModifiers('2021-05-03T00:00:00+02:00', 'Y-m-d\TH:i:sP'));
  759. $this->assertTrue(Carbon::hasFormatWithModifiers('2021-05-03T00:00:00Z', 'Y-m-d\TH:i:sp'));
  760. $this->assertTrue(Carbon::hasFormatWithModifiers('1975-05-01', 'Y-m-d!'));
  761. $this->assertTrue(Carbon::hasFormatWithModifiers('1975-05-01', 'Y-m-d|'));
  762. $this->assertTrue(Carbon::hasFormatWithModifiers('1975-05-01', 'Y-*-d'));
  763. $this->assertTrue(Carbon::hasFormatWithModifiers('1975-05-01', 'Y-??-d!'));
  764. $this->assertTrue(Carbon::hasFormatWithModifiers('1975-05-01', 'Y#m#d'));
  765. $this->assertTrue(Carbon::hasFormatWithModifiers('1975/05/31', 'Y#m#d'));
  766. $this->assertFalse(Carbon::hasFormatWithModifiers('2021-05-03T00:00:00Z', 'Y-m-d\TH:i:sP'));
  767. $this->assertFalse(Carbon::hasFormatWithModifiers('1975/31/05', 'Y#m#d'));
  768. $this->assertFalse(Carbon::hasFormatWithModifiers('1975-05-01', 'Y-?-d|'));
  769. $this->assertFalse(Carbon::hasFormatWithModifiers('1975--01', 'Y-*-d'));
  770. $this->assertFalse(Carbon::hasFormatWithModifiers('1975705-01', 'Y#m#d'));
  771. }
  772. public static function dataForFormatLetters(): Generator
  773. {
  774. yield ['d'];
  775. yield ['D'];
  776. yield ['j'];
  777. yield ['l'];
  778. yield ['N'];
  779. yield ['S'];
  780. yield ['w'];
  781. yield ['z'];
  782. yield ['W'];
  783. yield ['F'];
  784. yield ['m'];
  785. yield ['M'];
  786. yield ['n'];
  787. yield ['t'];
  788. yield ['L'];
  789. yield ['o'];
  790. yield ['Y'];
  791. yield ['y'];
  792. yield ['a'];
  793. yield ['A'];
  794. yield ['B'];
  795. yield ['g'];
  796. yield ['G'];
  797. yield ['h'];
  798. yield ['H'];
  799. yield ['i'];
  800. yield ['s'];
  801. yield ['u'];
  802. yield ['v'];
  803. yield ['e'];
  804. yield ['I'];
  805. yield ['O'];
  806. yield ['P'];
  807. yield ['T'];
  808. yield ['Z'];
  809. yield ['U'];
  810. yield ['c'];
  811. yield ['r'];
  812. }
  813. /**
  814. * @dataProvider \Tests\Carbon\IsTest::dataForFormatLetters
  815. */
  816. public function testHasFormatWithSingleLetter($letter)
  817. {
  818. $output = Carbon::now()->format($letter);
  819. if ($output === '1000' && $letter === 'v' && version_compare(PHP_VERSION, '7.2.12', '<')) {
  820. $output = '000';
  821. }
  822. $this->assertTrue(Carbon::hasFormat($output, $letter), "'$letter' format should match '$output'");
  823. }
  824. public function testCanBeCreatedFromFormat()
  825. {
  826. $this->assertTrue(Carbon::canBeCreatedFromFormat('1975-05-01', 'Y-m-d'));
  827. $this->assertTrue(Carbon::canBeCreatedFromFormat('12/30/2019', 'm/d/Y'));
  828. $this->assertFalse(Carbon::canBeCreatedFromFormat('1975-05-01', 'd m Y'));
  829. $this->assertFalse(Carbon::canBeCreatedFromFormat('1975-5-1', 'Y-m-d'));
  830. $this->assertFalse(Carbon::canBeCreatedFromFormat('19-05-01', 'Y-m-d'));
  831. $this->assertFalse(Carbon::canBeCreatedFromFormat('30/12/2019', 'm/d/Y'));
  832. $this->assertFalse(Carbon::canBeCreatedFromFormat('12/30/2019', 'd/m/Y'));
  833. $this->assertFalse(Carbon::canBeCreatedFromFormat('5', 'N'));
  834. }
  835. public function testIsSameFoobar()
  836. {
  837. $this->expectExceptionObject(new BadMethodCallException(
  838. 'Method isSameFoobar does not exist.'
  839. ));
  840. /** @var mixed $date */
  841. $date = Carbon::parse('12:00:00');
  842. $date->isSameFoobar(Carbon::parse('15:30:45'));
  843. }
  844. public function testIsCurrentFoobar()
  845. {
  846. $this->expectExceptionObject(new BadMethodCallException(
  847. 'Method isCurrentFoobar does not exist.'
  848. ));
  849. /** @var mixed $date */
  850. $date = Carbon::parse('12:00:00');
  851. $date->isCurrentFoobar();
  852. }
  853. public function testIs()
  854. {
  855. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('2019'));
  856. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2018'));
  857. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('2019-06'));
  858. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2018-06'));
  859. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2019-07'));
  860. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('06-02'));
  861. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('06-03'));
  862. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('05-02'));
  863. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-02'));
  864. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-03'));
  865. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2019-05-02'));
  866. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2020-06-02'));
  867. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('Sunday'));
  868. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('Monday'));
  869. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('June'));
  870. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('May'));
  871. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('12:23'));
  872. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('12:26'));
  873. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('12:23:00'));
  874. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('12h'));
  875. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('15h'));
  876. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('12:00'));
  877. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('15:00'));
  878. $this->assertTrue(Carbon::parse('2019-06-02 15:23:45')->is('3pm'));
  879. $this->assertFalse(Carbon::parse('2019-06-02 15:23:45')->is('4pm'));
  880. $this->assertFalse(Carbon::parse('2019-06-02 15:23:45')->is('3am'));
  881. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-02 12:23'));
  882. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-03 12:23'));
  883. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-02 15:23'));
  884. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-02 12:33'));
  885. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('2 June 2019'));
  886. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('1 June 2019'));
  887. $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('June 2019'));
  888. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('August 2019'));
  889. $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('June 2018'));
  890. }
  891. public function testHasFormatWithDots()
  892. {
  893. $this->assertTrue(Carbon::hasFormat('2020.09.09', 'Y.m.d'));
  894. $this->assertFalse(Carbon::hasFormat('2020009009', 'Y.m.d'));
  895. $this->assertFalse(Carbon::hasFormat('2020-09-09', 'Y.m.d'));
  896. $this->assertFalse(Carbon::hasFormat('2020*09*09', 'Y.m.d'));
  897. $this->assertFalse(Carbon::hasFormat('2020k09d09', 'Y.m.d'));
  898. }
  899. }