StringsTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 Carbon\Carbon;
  13. use Carbon\CarbonInterface;
  14. use Carbon\Factory;
  15. use DateTime;
  16. use ErrorException;
  17. use InvalidArgumentException;
  18. use Tests\AbstractTestCase;
  19. use Tests\Carbon\Fixtures\BadIsoCarbon;
  20. use Tests\Carbon\Fixtures\MyCarbon;
  21. class StringsTest extends AbstractTestCase
  22. {
  23. public function testToStringCast()
  24. {
  25. $d = Carbon::now();
  26. $this->assertSame(Carbon::now()->toDateTimeString(), ''.$d);
  27. }
  28. public function testToString()
  29. {
  30. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  31. $this->assertSame('Thu Dec 25 1975 14:15:16 GMT-0500', $d->toString());
  32. }
  33. public function testToISOString()
  34. {
  35. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  36. $this->assertSame('1975-12-25T19:15:16.000000Z', $d->toISOString());
  37. $d = Carbon::create(21975, 12, 25, 14, 15, 16);
  38. $this->assertSame('+021975-12-25T19:15:16.000000Z', $d->toISOString());
  39. $d = Carbon::create(-75, 12, 25, 14, 15, 16);
  40. $this->assertStringStartsWith('-000075-', $d->toISOString());
  41. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  42. $this->assertSame('1975-12-25T14:15:16.000000-05:00', $d->toISOString(true));
  43. $d = Carbon::create(21975, 12, 25, 14, 15, 16);
  44. $this->assertSame('+021975-12-25T14:15:16.000000-05:00', $d->toISOString(true));
  45. $d = Carbon::create(-75, 12, 25, 14, 15, 16);
  46. $this->assertStringStartsWith('-000075-', $d->toISOString(true));
  47. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  48. $this->assertSame('1975-12-25T19:15:16.000000Z', $d->toJSON());
  49. $d = Carbon::create(21975, 12, 25, 14, 15, 16);
  50. $this->assertSame('+021975-12-25T19:15:16.000000Z', $d->toJSON());
  51. $d = Carbon::create(-75, 12, 25, 14, 15, 16);
  52. $this->assertStringStartsWith('-000075-', $d->toJSON());
  53. $d = Carbon::create(0);
  54. $this->assertNull($d->toISOString());
  55. }
  56. public function testSetToStringFormatString()
  57. {
  58. Carbon::setToStringFormat('jS \o\f F, Y g:i:s a');
  59. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  60. $this->assertSame('25th of December, 1975 2:15:16 pm', ''.$d);
  61. }
  62. public function testSetToStringFormatClosure()
  63. {
  64. Carbon::setToStringFormat(function (CarbonInterface $d) {
  65. $format = $d->year === 1976 ?
  66. 'jS \o\f F g:i:s a' :
  67. 'jS \o\f F, Y g:i:s a';
  68. return $d->format($format);
  69. });
  70. $d = Carbon::create(1976, 12, 25, 14, 15, 16);
  71. $this->assertSame('25th of December 2:15:16 pm', ''.$d);
  72. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  73. $this->assertSame('25th of December, 1975 2:15:16 pm', ''.$d);
  74. }
  75. public function testSetToStringFormatViaSettings()
  76. {
  77. $factory = new Factory([
  78. 'toStringFormat' => function (CarbonInterface $d) {
  79. return $d->isoFormat('dddd');
  80. },
  81. ]);
  82. $d = $factory->create(1976, 12, 25, 14, 15, 16);
  83. $this->assertSame('Saturday', ''.$d);
  84. }
  85. public function testResetToStringFormat()
  86. {
  87. $d = Carbon::now();
  88. Carbon::setToStringFormat('123');
  89. Carbon::resetToStringFormat();
  90. $this->assertSame($d->toDateTimeString(), ''.$d);
  91. }
  92. public function testExtendedClassToString()
  93. {
  94. $d = MyCarbon::now();
  95. $this->assertSame($d->toDateTimeString(), ''.$d);
  96. }
  97. public function testToDateString()
  98. {
  99. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  100. $this->assertSame('1975-12-25', $d->toDateString());
  101. }
  102. public function testToDateTimeLocalString()
  103. {
  104. $d = Carbon::create(1975, 12, 25, 14, 15, 16.615342);
  105. $this->assertSame('1975-12-25T14:15:16', $d->toDateTimeLocalString());
  106. $this->assertSame('1975-12-25T14:15', $d->toDateTimeLocalString('minute'));
  107. $this->assertSame('1975-12-25T14:15:16', $d->toDateTimeLocalString('second'));
  108. $this->assertSame('1975-12-25T14:15:16.615', $d->toDateTimeLocalString('millisecond'));
  109. $this->assertSame('1975-12-25T14:15:16.615342', $d->toDateTimeLocalString('µs'));
  110. $message = null;
  111. try {
  112. $d->toDateTimeLocalString('hour');
  113. } catch (InvalidArgumentException $exception) {
  114. $message = $exception->getMessage();
  115. }
  116. $this->assertSame('Precision unit expected among: minute, second, millisecond and microsecond.', $message);
  117. }
  118. public function testToFormattedDateString()
  119. {
  120. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  121. $this->assertSame('Dec 25, 1975', $d->toFormattedDateString());
  122. }
  123. public function testToLocalizedFormattedDateString()
  124. {
  125. Carbon::useStrictMode(false);
  126. $this->wrapWithUtf8LcTimeLocale('fr_FR', function () {
  127. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  128. $date = $d->formatLocalized('%A %d %B %Y');
  129. $this->assertSame('jeudi 25 décembre 1975', $date);
  130. });
  131. }
  132. public function testToLocalizedFormattedDeprecation()
  133. {
  134. if (version_compare(PHP_VERSION, '8.1.0-dev', '>=')) {
  135. $this->expectExceptionObject(
  136. new ErrorException('Function strftime() is deprecated', E_DEPRECATED)
  137. );
  138. }
  139. $this->wrapWithUtf8LcTimeLocale('fr_FR', function () {
  140. $date = Carbon::parse('2021-05-26')->formatLocalized('%A %d %B %Y');
  141. $this->assertSame('mercredi 26 mai 2021', $date);
  142. });
  143. }
  144. public function testToLocalizedFormattedDateStringWhenUtf8IsNedded()
  145. {
  146. Carbon::useStrictMode(false);
  147. $this->wrapWithUtf8LcTimeLocale('fr_FR', function () {
  148. $d = Carbon::create(1975, 12, 25, 14, 15, 16, 'Europe/Paris');
  149. Carbon::setUtf8(false);
  150. $nonUtf8Date = $d->formatLocalized('%B');
  151. Carbon::setUtf8(true);
  152. $utf8Date = $d->formatLocalized('%B');
  153. Carbon::setUtf8(false);
  154. $this->assertSame('décembre', $nonUtf8Date);
  155. $this->assertSame(mb_convert_encoding('décembre', 'UTF-8', mb_list_encodings()), $utf8Date);
  156. });
  157. }
  158. public function testToLocalizedFormattedTimezonedDateString()
  159. {
  160. Carbon::useStrictMode(false);
  161. $d = Carbon::create(1975, 12, 25, 14, 15, 16, 'Europe/London');
  162. $this->assertSame('Thursday 25 December 1975 14:15', $d->formatLocalized('%A %d %B %Y %H:%M'));
  163. }
  164. public function testToTimeString()
  165. {
  166. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  167. $this->assertSame('14:15:16', $d->toTimeString());
  168. }
  169. public function testToDateTimeString()
  170. {
  171. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  172. $this->assertSame('1975-12-25 14:15:16', $d->toDateTimeString());
  173. }
  174. public function testToDateTimeStringWithPaddedZeroes()
  175. {
  176. $d = Carbon::create(2000, 5, 2, 4, 3, 4);
  177. $this->assertSame('2000-05-02 04:03:04', $d->toDateTimeString());
  178. }
  179. public function testToDayDateTimeString()
  180. {
  181. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  182. $this->assertSame('Thu, Dec 25, 1975 2:15 PM', $d->toDayDateTimeString());
  183. }
  184. public function testToDayDateString()
  185. {
  186. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  187. $this->assertSame('Thu, Dec 25, 1975', $d->toFormattedDayDateString());
  188. }
  189. public function testToAtomString()
  190. {
  191. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  192. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toAtomString());
  193. }
  194. public function testToCOOKIEString()
  195. {
  196. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  197. $this->assertSame(
  198. DateTime::COOKIE === 'l, d-M-y H:i:s T'
  199. ? 'Thursday, 25-Dec-75 14:15:16 EST'
  200. : 'Thursday, 25-Dec-1975 14:15:16 EST',
  201. $d->toCookieString()
  202. );
  203. }
  204. public function testToIso8601String()
  205. {
  206. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  207. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toIso8601String());
  208. }
  209. public function testToIso8601ZuluString()
  210. {
  211. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  212. $this->assertSame('1975-12-25T19:15:16Z', $d->toIso8601ZuluString());
  213. }
  214. public function testToRC822String()
  215. {
  216. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  217. $this->assertSame('Thu, 25 Dec 75 14:15:16 -0500', $d->toRfc822String());
  218. }
  219. public function testToRfc850String()
  220. {
  221. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  222. $this->assertSame('Thursday, 25-Dec-75 14:15:16 EST', $d->toRfc850String());
  223. }
  224. public function testToRfc1036String()
  225. {
  226. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  227. $this->assertSame('Thu, 25 Dec 75 14:15:16 -0500', $d->toRfc1036String());
  228. }
  229. public function testToRfc1123String()
  230. {
  231. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  232. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRfc1123String());
  233. }
  234. public function testToRfc2822String()
  235. {
  236. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  237. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRfc2822String());
  238. }
  239. public function testToRfc3339String()
  240. {
  241. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  242. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toRfc3339String());
  243. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  244. $this->assertSame('1975-12-25T14:15:16.000-05:00', $d->toRfc3339String(true));
  245. }
  246. public function testToRssString()
  247. {
  248. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  249. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRssString());
  250. }
  251. public function testToW3cString()
  252. {
  253. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  254. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toW3cString());
  255. }
  256. public function testToRfc7231String()
  257. {
  258. $d = Carbon::create(1975, 12, 25, 14, 15, 16, 'GMT');
  259. $this->assertSame('Thu, 25 Dec 1975 14:15:16 GMT', $d->toRfc7231String());
  260. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  261. $this->assertSame('Thu, 25 Dec 1975 19:15:16 GMT', $d->toRfc7231String());
  262. }
  263. public function testIsoFormat()
  264. {
  265. $d = Carbon::parse('midnight');
  266. $this->assertSame('24', $d->isoFormat('k'));
  267. $d = Carbon::parse('2017-01-01');
  268. $this->assertSame('2017', $d->isoFormat('g'));
  269. $this->assertSame('2017', $d->locale('en_US')->isoFormat('g'));
  270. $this->assertSame('2016', $d->locale('fr')->isoFormat('g'));
  271. $this->assertSame('2016', $d->isoFormat('G'));
  272. $this->assertSame('2016', $d->locale('en_US')->isoFormat('G'));
  273. $this->assertSame('2016', $d->locale('fr')->isoFormat('G'));
  274. $d = Carbon::parse('2015-12-31');
  275. $this->assertSame('2016', $d->isoFormat('g'));
  276. $this->assertSame('2016', $d->locale('en_US')->isoFormat('g'));
  277. $this->assertSame('2015', $d->locale('fr')->isoFormat('g'));
  278. $this->assertSame('2015', $d->isoFormat('G'));
  279. $this->assertSame('2015', $d->locale('en_US')->isoFormat('G'));
  280. $this->assertSame('2015', $d->locale('fr')->isoFormat('G'));
  281. $d = Carbon::parse('2017-01-01 22:25:24.182937');
  282. $this->assertSame('1 18 182 1829 18293 182937 1829370 18293700 182937000', $d->isoFormat('S SS SSS SSSS SSSSS SSSSSS SSSSSSS SSSSSSSS SSSSSSSSS'));
  283. $this->assertSame('02017 +002017', $d->isoFormat('YYYYY YYYYYY'));
  284. $this->assertSame(-117, Carbon::create(-117, 1, 1)->year);
  285. $this->assertSame('-00117 -000117', Carbon::create(-117, 1, 1)->isoFormat('YYYYY YYYYYY'));
  286. $this->assertSame('M01', $d->isoFormat('\\MMM'));
  287. $this->assertSame('Jan', $d->isoFormat('MMM'));
  288. $this->assertSame('janv.', $d->locale('fr')->isoFormat('MMM'));
  289. $this->assertSame('ene.', $d->locale('es')->isoFormat('MMM'));
  290. $this->assertSame('1 de enero de 2017', $d->locale('es')->isoFormat('LL'));
  291. $this->assertSame('1 de ene. de 2017', $d->locale('es')->isoFormat('ll'));
  292. $this->assertSame('1st', Carbon::parse('2018-06-01')->isoFormat('Do'));
  293. $this->assertSame('11th', Carbon::parse('2018-06-11')->isoFormat('Do'));
  294. $this->assertSame('21st', Carbon::parse('2018-06-21')->isoFormat('Do'));
  295. $this->assertSame('15th', Carbon::parse('2018-06-15')->isoFormat('Do'));
  296. }
  297. public function testBadIsoFormat()
  298. {
  299. $d = BadIsoCarbon::parse('midnight');
  300. $this->assertSame('', $d->isoFormat('MMM'));
  301. }
  302. public function testTranslatedFormat()
  303. {
  304. $this->assertSame('1st', Carbon::parse('01-01-01')->translatedFormat('jS'));
  305. $this->assertSame('1er', Carbon::parse('01-01-01')->locale('fr')->translatedFormat('jS'));
  306. $this->assertSame('31 мая', Carbon::parse('2019-05-15')->locale('ru')->translatedFormat('t F'));
  307. $this->assertSame('5 май', Carbon::parse('2019-05-15')->locale('ru')->translatedFormat('n F'));
  308. }
  309. }