StringsTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 Carbon\CarbonInterface;
  14. use Carbon\FactoryImmutable;
  15. use DateTime;
  16. use Tests\AbstractTestCase;
  17. use Tests\Carbon\Fixtures\MyCarbon;
  18. use Tests\CarbonImmutable\Fixtures\BadIsoCarbon;
  19. class StringsTest extends AbstractTestCase
  20. {
  21. public function testToString()
  22. {
  23. $d = Carbon::now();
  24. $this->assertSame(Carbon::now()->toDateTimeString(), ''.$d);
  25. }
  26. public function testSetToStringFormatString()
  27. {
  28. Carbon::setToStringFormat('jS \o\f F, Y g:i:s a');
  29. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  30. $this->assertSame('25th of December, 1975 2:15:16 pm', ''.$d);
  31. }
  32. public function testSetToStringFormatClosure()
  33. {
  34. Carbon::setToStringFormat(function (CarbonInterface $d) {
  35. $format = $d->year === 1976 ?
  36. 'jS \o\f F g:i:s a' :
  37. 'jS \o\f F, Y g:i:s a';
  38. return $d->format($format);
  39. });
  40. $d = Carbon::create(1976, 12, 25, 14, 15, 16);
  41. $this->assertSame('25th of December 2:15:16 pm', ''.$d);
  42. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  43. $this->assertSame('25th of December, 1975 2:15:16 pm', ''.$d);
  44. }
  45. public function testSetToStringFormatViaSettings()
  46. {
  47. $factory = new FactoryImmutable([
  48. 'toStringFormat' => function (CarbonInterface $d) {
  49. return $d->isoFormat('dddd');
  50. },
  51. ]);
  52. $d = $factory->create(1976, 12, 25, 14, 15, 16);
  53. $this->assertSame('Saturday', ''.$d);
  54. }
  55. public function testResetToStringFormat()
  56. {
  57. $d = Carbon::now();
  58. Carbon::setToStringFormat('123');
  59. Carbon::resetToStringFormat();
  60. $this->assertSame($d->toDateTimeString(), ''.$d);
  61. }
  62. public function testExtendedClassToString()
  63. {
  64. $d = MyCarbon::now();
  65. $this->assertSame($d->toDateTimeString(), ''.$d);
  66. }
  67. public function testToDateString()
  68. {
  69. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  70. $this->assertSame('1975-12-25', $d->toDateString());
  71. }
  72. public function testToFormattedDateString()
  73. {
  74. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  75. $this->assertSame('Dec 25, 1975', $d->toFormattedDateString());
  76. }
  77. public function testToLocalizedFormattedDateString()
  78. {
  79. Carbon::useStrictMode(false);
  80. $this->wrapWithUtf8LcTimeLocale('fr_FR', function () {
  81. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  82. $date = $d->formatLocalized('%A %d %B %Y');
  83. $this->assertSame('jeudi 25 décembre 1975', $date);
  84. });
  85. }
  86. public function testToLocalizedFormattedDateStringWhenUtf8IsNedded()
  87. {
  88. Carbon::useStrictMode(false);
  89. $this->wrapWithUtf8LcTimeLocale('fr_FR', function () {
  90. $d = Carbon::create(1975, 12, 25, 14, 15, 16, 'Europe/Paris');
  91. Carbon::setUtf8(false);
  92. $nonUtf8Date = $d->formatLocalized('%B');
  93. Carbon::setUtf8(true);
  94. $utf8Date = $d->formatLocalized('%B');
  95. Carbon::setUtf8(false);
  96. $this->assertSame('décembre', $nonUtf8Date);
  97. $this->assertSame(mb_convert_encoding('décembre', 'UTF-8', mb_list_encodings()), $utf8Date);
  98. });
  99. }
  100. public function testToLocalizedFormattedTimezonedDateString()
  101. {
  102. Carbon::useStrictMode(false);
  103. $d = Carbon::create(1975, 12, 25, 14, 15, 16, 'Europe/London');
  104. $this->assertSame('Thursday 25 December 1975 14:15', $d->formatLocalized('%A %d %B %Y %H:%M'));
  105. }
  106. public function testToTimeString()
  107. {
  108. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  109. $this->assertSame('14:15:16', $d->toTimeString());
  110. }
  111. public function testToDateTimeString()
  112. {
  113. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  114. $this->assertSame('1975-12-25 14:15:16', $d->toDateTimeString());
  115. }
  116. public function testToDateTimeStringWithPaddedZeroes()
  117. {
  118. $d = Carbon::create(2000, 5, 2, 4, 3, 4);
  119. $this->assertSame('2000-05-02 04:03:04', $d->toDateTimeString());
  120. }
  121. public function testToDayDateTimeString()
  122. {
  123. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  124. $this->assertSame('Thu, Dec 25, 1975 2:15 PM', $d->toDayDateTimeString());
  125. }
  126. public function testToAtomString()
  127. {
  128. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  129. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toAtomString());
  130. }
  131. public function testToCOOKIEString()
  132. {
  133. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  134. $this->assertSame(
  135. DateTime::COOKIE === 'l, d-M-y H:i:s T'
  136. ? 'Thursday, 25-Dec-75 14:15:16 EST'
  137. : 'Thursday, 25-Dec-1975 14:15:16 EST',
  138. $d->toCookieString()
  139. );
  140. }
  141. public function testToIso8601String()
  142. {
  143. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  144. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toIso8601String());
  145. }
  146. public function testToIso8601ZuluString()
  147. {
  148. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  149. $this->assertSame('1975-12-25T19:15:16Z', $d->toIso8601ZuluString());
  150. }
  151. public function testToRC822String()
  152. {
  153. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  154. $this->assertSame('Thu, 25 Dec 75 14:15:16 -0500', $d->toRfc822String());
  155. }
  156. public function testToRfc850String()
  157. {
  158. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  159. $this->assertSame('Thursday, 25-Dec-75 14:15:16 EST', $d->toRfc850String());
  160. }
  161. public function testToRfc1036String()
  162. {
  163. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  164. $this->assertSame('Thu, 25 Dec 75 14:15:16 -0500', $d->toRfc1036String());
  165. }
  166. public function testToRfc1123String()
  167. {
  168. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  169. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRfc1123String());
  170. }
  171. public function testToRfc2822String()
  172. {
  173. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  174. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRfc2822String());
  175. }
  176. public function testToRfc3339String()
  177. {
  178. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  179. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toRfc3339String());
  180. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  181. $this->assertSame('1975-12-25T14:15:16.000-05:00', $d->toRfc3339String(true));
  182. }
  183. public function testToRssString()
  184. {
  185. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  186. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRssString());
  187. }
  188. public function testToW3cString()
  189. {
  190. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  191. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toW3cString());
  192. }
  193. public function testToRfc7231String()
  194. {
  195. $d = Carbon::create(1975, 12, 25, 14, 15, 16, 'GMT');
  196. $this->assertSame('Thu, 25 Dec 1975 14:15:16 GMT', $d->toRfc7231String());
  197. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  198. $this->assertSame('Thu, 25 Dec 1975 19:15:16 GMT', $d->toRfc7231String());
  199. }
  200. public function testIsoFormat()
  201. {
  202. $d = Carbon::parse('midnight');
  203. $this->assertSame('24', $d->isoFormat('k'));
  204. $d = Carbon::parse('2017-01-01');
  205. $this->assertSame('2017', $d->isoFormat('g'));
  206. $this->assertSame('2017', $d->locale('en_US')->isoFormat('g'));
  207. $this->assertSame('2016', $d->locale('fr')->isoFormat('g'));
  208. $this->assertSame('2016', $d->isoFormat('G'));
  209. $this->assertSame('2016', $d->locale('en_US')->isoFormat('G'));
  210. $this->assertSame('2016', $d->locale('fr')->isoFormat('G'));
  211. $d = Carbon::parse('2015-12-31');
  212. $this->assertSame('2016', $d->isoFormat('g'));
  213. $this->assertSame('2016', $d->locale('en_US')->isoFormat('g'));
  214. $this->assertSame('2015', $d->locale('fr')->isoFormat('g'));
  215. $this->assertSame('2015', $d->isoFormat('G'));
  216. $this->assertSame('2015', $d->locale('en_US')->isoFormat('G'));
  217. $this->assertSame('2015', $d->locale('fr')->isoFormat('G'));
  218. $d = Carbon::parse('2017-01-01 22:25:24.182937');
  219. $this->assertSame('1 18 182 1829 18293 182937', $d->isoFormat('S SS SSS SSSS SSSSS SSSSSS'));
  220. $this->assertSame('02017 +002017', $d->isoFormat('YYYYY YYYYYY'));
  221. $this->assertSame(-117, Carbon::create(-117, 1, 1)->year);
  222. $this->assertSame('-00117 -000117', Carbon::create(-117, 1, 1)->isoFormat('YYYYY YYYYYY'));
  223. $this->assertSame('M01', $d->isoFormat('\\MMM'));
  224. $this->assertSame('Jan', $d->isoFormat('MMM'));
  225. $this->assertSame('janv.', $d->locale('fr')->isoFormat('MMM'));
  226. $this->assertSame('ene.', $d->locale('es')->isoFormat('MMM'));
  227. $this->assertSame('1 de enero de 2017', $d->locale('es')->isoFormat('LL'));
  228. $this->assertSame('1 de ene. de 2017', $d->locale('es')->isoFormat('ll'));
  229. $this->assertSame('1st', Carbon::parse('2018-06-01')->isoFormat('Do'));
  230. $this->assertSame('11th', Carbon::parse('2018-06-11')->isoFormat('Do'));
  231. $this->assertSame('21st', Carbon::parse('2018-06-21')->isoFormat('Do'));
  232. $this->assertSame('15th', Carbon::parse('2018-06-15')->isoFormat('Do'));
  233. }
  234. public function testBadIsoFormat()
  235. {
  236. $d = BadIsoCarbon::parse('midnight');
  237. $this->assertSame('', $d->isoFormat('MMM'));
  238. }
  239. public function testTranslatedFormat()
  240. {
  241. $this->assertSame('1st', Carbon::parse('01-01-01')->translatedFormat('jS'));
  242. $this->assertSame('1er', Carbon::parse('01-01-01')->locale('fr')->translatedFormat('jS'));
  243. $this->assertSame('31 мая', Carbon::parse('2019-05-15')->locale('ru')->translatedFormat('t F'));
  244. $this->assertSame('5 май', Carbon::parse('2019-05-15')->locale('ru')->translatedFormat('n F'));
  245. }
  246. }