DiffTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /*
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Carbon\Carbon;
  11. class DiffTest extends TestFixture
  12. {
  13. public function testDiffInYearsPositive()
  14. {
  15. $dt = Carbon::createFromDate(2000, 1, 1);
  16. $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()));
  17. }
  18. public function testDiffInYearsNegativeWithSign()
  19. {
  20. $dt = Carbon::createFromDate(2000, 1, 1);
  21. $this->assertSame(-1, $dt->diffInYears($dt->copy()->subYear(), false));
  22. }
  23. public function testDiffInYearsNegativeNoSign()
  24. {
  25. $dt = Carbon::createFromDate(2000, 1, 1);
  26. $this->assertSame(1, $dt->diffInYears($dt->copy()->subYear()));
  27. }
  28. public function testDiffInYearsVsDefaultNow()
  29. {
  30. $this->assertSame(1, Carbon::now()->subYear()->diffInYears());
  31. }
  32. public function testDiffInYearsEnsureIsTruncated()
  33. {
  34. $dt = Carbon::createFromDate(2000, 1, 1);
  35. $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()->addMonths(7)));
  36. }
  37. public function testDiffInMonthsPositive()
  38. {
  39. $dt = Carbon::createFromDate(2000, 1, 1);
  40. $this->assertSame(13, $dt->diffInMonths($dt->copy()->addYear()->addMonth()));
  41. }
  42. public function testDiffInMonthsNegativeWithSign()
  43. {
  44. $dt = Carbon::createFromDate(2000, 1, 1);
  45. $this->assertSame(-11, $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false));
  46. }
  47. public function testDiffInMonthsNegativeNoSign()
  48. {
  49. $dt = Carbon::createFromDate(2000, 1, 1);
  50. $this->assertSame(11, $dt->diffInMonths($dt->copy()->subYear()->addMonth()));
  51. }
  52. public function testDiffInMonthsVsDefaultNow()
  53. {
  54. $this->assertSame(12, Carbon::now()->subYear()->diffInMonths());
  55. }
  56. public function testDiffInMonthsEnsureIsTruncated()
  57. {
  58. $dt = Carbon::createFromDate(2000, 1, 1);
  59. $this->assertSame(1, $dt->diffInMonths($dt->copy()->addMonth()->addDays(16)));
  60. }
  61. public function testDiffInDaysPositive()
  62. {
  63. $dt = Carbon::createFromDate(2000, 1, 1);
  64. $this->assertSame(366, $dt->diffInDays($dt->copy()->addYear()));
  65. }
  66. public function testDiffInDaysNegativeWithSign()
  67. {
  68. $dt = Carbon::createFromDate(2000, 1, 1);
  69. $this->assertSame(-365, $dt->diffInDays($dt->copy()->subYear(), false));
  70. }
  71. public function testDiffInDaysNegativeNoSign()
  72. {
  73. $dt = Carbon::createFromDate(2000, 1, 1);
  74. $this->assertSame(365, $dt->diffInDays($dt->copy()->subYear()));
  75. }
  76. public function testDiffInDaysVsDefaultNow()
  77. {
  78. $this->assertSame(7, Carbon::now()->subWeek()->diffInDays());
  79. }
  80. public function testDiffInDaysEnsureIsTruncated()
  81. {
  82. $dt = Carbon::createFromDate(2000, 1, 1);
  83. $this->assertSame(1, $dt->diffInDays($dt->copy()->addDay()->addHours(13)));
  84. }
  85. public function testDiffInHoursPositive()
  86. {
  87. $dt = Carbon::createFromDate(2000, 1, 1);
  88. $this->assertSame(26, $dt->diffInHours($dt->copy()->addDay()->addHours(2)));
  89. }
  90. public function testDiffInHoursNegativeWithSign()
  91. {
  92. $dt = Carbon::createFromDate(2000, 1, 1);
  93. $this->assertSame(-22, $dt->diffInHours($dt->copy()->subDay()->addHours(2), false));
  94. }
  95. public function testDiffInHoursNegativeNoSign()
  96. {
  97. $dt = Carbon::createFromDate(2000, 1, 1);
  98. $this->assertSame(22, $dt->diffInHours($dt->copy()->subDay()->addHours(2)));
  99. }
  100. public function testDiffInHoursVsDefaultNow()
  101. {
  102. $this->assertSame(48, Carbon::now()->subDays(2)->diffInHours());
  103. }
  104. public function testDiffInHoursEnsureIsTruncated()
  105. {
  106. $dt = Carbon::createFromDate(2000, 1, 1);
  107. $this->assertSame(1, $dt->diffInHours($dt->copy()->addHour()->addMinutes(31)));
  108. }
  109. public function testDiffInMinutesPositive()
  110. {
  111. $dt = Carbon::createFromDate(2000, 1, 1);
  112. $this->assertSame(62, $dt->diffInMinutes($dt->copy()->addHour()->addMinutes(2)));
  113. }
  114. public function testDiffInMinutesPositiveAlot()
  115. {
  116. $dt = Carbon::createFromDate(2000, 1, 1);
  117. $this->assertSame(1502, $dt->diffInMinutes($dt->copy()->addHours(25)->addMinutes(2)));
  118. }
  119. public function testDiffInMinutesNegativeWithSign()
  120. {
  121. $dt = Carbon::createFromDate(2000, 1, 1);
  122. $this->assertSame(-58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false));
  123. }
  124. public function testDiffInMinutesNegativeNoSign()
  125. {
  126. $dt = Carbon::createFromDate(2000, 1, 1);
  127. $this->assertSame(58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2)));
  128. }
  129. public function testDiffInMinutesVsDefaultNow()
  130. {
  131. $this->assertSame(60, Carbon::now()->subHour()->diffInMinutes());
  132. }
  133. public function testDiffInMinutesEnsureIsTruncated()
  134. {
  135. $dt = Carbon::createFromDate(2000, 1, 1);
  136. $this->assertSame(1, $dt->diffInMinutes($dt->copy()->addMinute()->addSeconds(31)));
  137. }
  138. public function testDiffInSecondsPositive()
  139. {
  140. $dt = Carbon::createFromDate(2000, 1, 1);
  141. $this->assertSame(62, $dt->diffInSeconds($dt->copy()->addMinute()->addSeconds(2)));
  142. }
  143. public function testDiffInSecondsPositiveAlot()
  144. {
  145. $dt = Carbon::createFromDate(2000, 1, 1);
  146. $this->assertSame(7202, $dt->diffInSeconds($dt->copy()->addHours(2)->addSeconds(2)));
  147. }
  148. public function testDiffInSecondsNegativeWithSign()
  149. {
  150. $dt = Carbon::createFromDate(2000, 1, 1);
  151. $this->assertSame(-58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false));
  152. }
  153. public function testDiffInSecondsNegativeNoSign()
  154. {
  155. $dt = Carbon::createFromDate(2000, 1, 1);
  156. $this->assertSame(58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2)));
  157. }
  158. public function testDiffInSecondsVsDefaultNow()
  159. {
  160. $this->assertSame(3600, Carbon::now()->subHour()->diffInSeconds());
  161. }
  162. public function testDiffInSecondsEnsureIsTruncated()
  163. {
  164. $dt = Carbon::createFromDate(2000, 1, 1);
  165. $this->assertSame(1, $dt->diffInSeconds($dt->copy()->addSeconds(1.9)));
  166. }
  167. public function testDiffInSecondsWithTimezones()
  168. {
  169. $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
  170. $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver');
  171. $this->assertSame(3*60*60, $dtOttawa->diffInSeconds($dtVancouver));
  172. }
  173. public function testDiffInSecondsWithTimezonesAndVsDefault()
  174. {
  175. $dt = Carbon::now('America/Vancouver');
  176. $this->assertSame(0, $dt->diffInSeconds());
  177. }
  178. public function testDiffForHumansNowAndSecond()
  179. {
  180. $d = Carbon::now();
  181. $this->assertSame('1 second ago', $d->diffForHumans());
  182. }
  183. public function testDiffForHumansNowAndSecondWithTimezone()
  184. {
  185. $d = Carbon::now('America/Vancouver');
  186. $this->assertSame('1 second ago', $d->diffForHumans());
  187. }
  188. public function testDiffForHumansNowAndSeconds()
  189. {
  190. $d = Carbon::now()->subSeconds(2);
  191. $this->assertSame('2 seconds ago', $d->diffForHumans());
  192. }
  193. public function testDiffForHumansNowAndMinute()
  194. {
  195. $d = Carbon::now()->subMinute();
  196. $this->assertSame('1 minute ago', $d->diffForHumans());
  197. }
  198. public function testDiffForHumansNowAndMinutes()
  199. {
  200. $d = Carbon::now()->subMinutes(2);
  201. $this->assertSame('2 minutes ago', $d->diffForHumans());
  202. }
  203. public function testDiffForHumansNowAndHour()
  204. {
  205. $d = Carbon::now()->subHour();
  206. $this->assertSame('1 hour ago', $d->diffForHumans());
  207. }
  208. public function testDiffForHumansNowAndHours()
  209. {
  210. $d = Carbon::now()->subHours(2);
  211. $this->assertSame('2 hours ago', $d->diffForHumans());
  212. }
  213. public function testDiffForHumansNowAndDay()
  214. {
  215. $d = Carbon::now()->subDay();
  216. $this->assertSame('1 day ago', $d->diffForHumans());
  217. }
  218. public function testDiffForHumansNowAndDays()
  219. {
  220. $d = Carbon::now()->subDays(2);
  221. $this->assertSame('2 days ago', $d->diffForHumans());
  222. }
  223. public function testDiffForHumansNowAndMonth()
  224. {
  225. $d = Carbon::now()->subMonth();
  226. $this->assertSame('1 month ago', $d->diffForHumans());
  227. }
  228. public function testDiffForHumansNowAndMonths()
  229. {
  230. $d = Carbon::now()->subMonths(2);
  231. $this->assertSame('2 months ago', $d->diffForHumans());
  232. }
  233. public function testDiffForHumansNowAndYear()
  234. {
  235. $d = Carbon::now()->subYear();
  236. $this->assertSame('1 year ago', $d->diffForHumans());
  237. }
  238. public function testDiffForHumansNowAndYears()
  239. {
  240. $d = Carbon::now()->subYears(2);
  241. $this->assertSame('2 years ago', $d->diffForHumans());
  242. }
  243. public function testDiffForHumansNowAndFutureSecond()
  244. {
  245. $d = Carbon::now()->addSecond();
  246. $this->assertSame('1 second from now', $d->diffForHumans());
  247. }
  248. public function testDiffForHumansNowAndFutureSeconds()
  249. {
  250. $d = Carbon::now()->addSeconds(2);
  251. $this->assertSame('2 seconds from now', $d->diffForHumans());
  252. }
  253. public function testDiffForHumansNowAndFutureMinute()
  254. {
  255. $d = Carbon::now()->addMinute();
  256. $this->assertSame('1 minute from now', $d->diffForHumans());
  257. }
  258. public function testDiffForHumansNowAndFutureMinutes()
  259. {
  260. $d = Carbon::now()->addMinutes(2);
  261. $this->assertSame('2 minutes from now', $d->diffForHumans());
  262. }
  263. public function testDiffForHumansNowAndFutureHour()
  264. {
  265. $d = Carbon::now()->addHour();
  266. $this->assertSame('1 hour from now', $d->diffForHumans());
  267. }
  268. public function testDiffForHumansNowAndFutureHours()
  269. {
  270. $d = Carbon::now()->addHours(2);
  271. $this->assertSame('2 hours from now', $d->diffForHumans());
  272. }
  273. public function testDiffForHumansNowAndFutureDay()
  274. {
  275. $d = Carbon::now()->addDay();
  276. $this->assertSame('1 day from now', $d->diffForHumans());
  277. }
  278. public function testDiffForHumansNowAndFutureDays()
  279. {
  280. $d = Carbon::now()->addDays(2);
  281. $this->assertSame('2 days from now', $d->diffForHumans());
  282. }
  283. public function testDiffForHumansNowAndFutureMonth()
  284. {
  285. $d = Carbon::now()->addMonth();
  286. $this->assertSame('1 month from now', $d->diffForHumans());
  287. }
  288. public function testDiffForHumansNowAndFutureMonths()
  289. {
  290. $d = Carbon::now()->addMonths(2);
  291. $this->assertSame('2 months from now', $d->diffForHumans());
  292. }
  293. public function testDiffForHumansNowAndFutureYear()
  294. {
  295. $d = Carbon::now()->addYear();
  296. $this->assertSame('1 year from now', $d->diffForHumans());
  297. }
  298. public function testDiffForHumansNowAndFutureYears()
  299. {
  300. $d = Carbon::now()->addYears(2);
  301. $this->assertSame('2 years from now', $d->diffForHumans());
  302. }
  303. public function testDiffForHumansOtherAndSecond()
  304. {
  305. $d = Carbon::now()->addSecond();
  306. $this->assertSame('1 second before', Carbon::now()->diffForHumans($d));
  307. }
  308. public function testDiffForHumansOtherAndSeconds()
  309. {
  310. $d = Carbon::now()->addSeconds(2);
  311. $this->assertSame('2 seconds before', Carbon::now()->diffForHumans($d));
  312. }
  313. public function testDiffForHumansOtherAndMinute()
  314. {
  315. $d = Carbon::now()->addMinute();
  316. $this->assertSame('1 minute before', Carbon::now()->diffForHumans($d));
  317. }
  318. public function testDiffForHumansOtherAndMinutes()
  319. {
  320. $d = Carbon::now()->addMinutes(2);
  321. $this->assertSame('2 minutes before', Carbon::now()->diffForHumans($d));
  322. }
  323. public function testDiffForHumansOtherAndHour()
  324. {
  325. $d = Carbon::now()->addHour();
  326. $this->assertSame('1 hour before', Carbon::now()->diffForHumans($d));
  327. }
  328. public function testDiffForHumansOtherAndHours()
  329. {
  330. $d = Carbon::now()->addHours(2);
  331. $this->assertSame('2 hours before', Carbon::now()->diffForHumans($d));
  332. }
  333. public function testDiffForHumansOtherAndDay()
  334. {
  335. $d = Carbon::now()->addDay();
  336. $this->assertSame('1 day before', Carbon::now()->diffForHumans($d));
  337. }
  338. public function testDiffForHumansOtherAndDays()
  339. {
  340. $d = Carbon::now()->addDays(2);
  341. $this->assertSame('2 days before', Carbon::now()->diffForHumans($d));
  342. }
  343. public function testDiffForHumansOtherAndMonth()
  344. {
  345. $d = Carbon::now()->addMonth();
  346. $this->assertSame('1 month before', Carbon::now()->diffForHumans($d));
  347. }
  348. public function testDiffForHumansOtherAndMonths()
  349. {
  350. $d = Carbon::now()->addMonths(2);
  351. $this->assertSame('2 months before', Carbon::now()->diffForHumans($d));
  352. }
  353. public function testDiffForHumansOtherAndYear()
  354. {
  355. $d = Carbon::now()->addYear();
  356. $this->assertSame('1 year before', Carbon::now()->diffForHumans($d));
  357. }
  358. public function testDiffForHumansOtherAndYears()
  359. {
  360. $d = Carbon::now()->addYears(2);
  361. $this->assertSame('2 years before', Carbon::now()->diffForHumans($d));
  362. }
  363. public function testDiffForHumansOtherAndFutureSecond()
  364. {
  365. $d = Carbon::now()->subSecond();
  366. $this->assertSame('1 second after', Carbon::now()->diffForHumans($d));
  367. }
  368. public function testDiffForHumansOtherAndFutureSeconds()
  369. {
  370. $d = Carbon::now()->subSeconds(2);
  371. $this->assertSame('2 seconds after', Carbon::now()->diffForHumans($d));
  372. }
  373. public function testDiffForHumansOtherAndFutureMinute()
  374. {
  375. $d = Carbon::now()->subMinute();
  376. $this->assertSame('1 minute after', Carbon::now()->diffForHumans($d));
  377. }
  378. public function testDiffForHumansOtherAndFutureMinutes()
  379. {
  380. $d = Carbon::now()->subMinutes(2);
  381. $this->assertSame('2 minutes after', Carbon::now()->diffForHumans($d));
  382. }
  383. public function testDiffForHumansOtherAndFutureHour()
  384. {
  385. $d = Carbon::now()->subHour();
  386. $this->assertSame('1 hour after', Carbon::now()->diffForHumans($d));
  387. }
  388. public function testDiffForHumansOtherAndFutureHours()
  389. {
  390. $d = Carbon::now()->subHours(2);
  391. $this->assertSame('2 hours after', Carbon::now()->diffForHumans($d));
  392. }
  393. public function testDiffForHumansOtherAndFutureDay()
  394. {
  395. $d = Carbon::now()->subDay();
  396. $this->assertSame('1 day after', Carbon::now()->diffForHumans($d));
  397. }
  398. public function testDiffForHumansOtherAndFutureDays()
  399. {
  400. $d = Carbon::now()->subDays(2);
  401. $this->assertSame('2 days after', Carbon::now()->diffForHumans($d));
  402. }
  403. public function testDiffForHumansOtherAndFutureMonth()
  404. {
  405. $d = Carbon::now()->subMonth();
  406. $this->assertSame('1 month after', Carbon::now()->diffForHumans($d));
  407. }
  408. public function testDiffForHumansOtherAndFutureMonths()
  409. {
  410. $d = Carbon::now()->subMonths(2);
  411. $this->assertSame('2 months after', Carbon::now()->diffForHumans($d));
  412. }
  413. public function testDiffForHumansOtherAndFutureYear()
  414. {
  415. $d = Carbon::now()->subYear();
  416. $this->assertSame('1 year after', Carbon::now()->diffForHumans($d));
  417. }
  418. public function testDiffForHumansOtherAndFutureYears()
  419. {
  420. $d = Carbon::now()->subYears(2);
  421. $this->assertSame('2 years after', Carbon::now()->diffForHumans($d));
  422. }
  423. }