StubIntlDateFormatterTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. namespace Symfony\Tests\Component\Locale\Stub;
  11. require_once __DIR__.'/../TestCase.php';
  12. use Symfony\Component\Locale\Locale;
  13. use Symfony\Component\Locale\Stub\StubIntlDateFormatter;
  14. use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase;
  15. class StubIntlDateFormatterTest extends LocaleTestCase
  16. {
  17. /**
  18. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  19. */
  20. public function testConstructorWithUnsupportedLocale()
  21. {
  22. $formatter = new StubIntlDateFormatter('pt_BR', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  23. }
  24. public function testConstructor()
  25. {
  26. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, 'y-M-d');
  27. $this->assertEquals('y-M-d', $formatter->getPattern());
  28. }
  29. /**
  30. * When a time zone is not specified, it uses the system default however it returns null in the getter method
  31. * @covers Symfony\Component\Locale\Stub\StubIntlDateFormatter::getTimeZoneId
  32. * @covers Symfony\Component\Locale\Stub\StubIntlDateFormatter::setTimeZoneId
  33. * @see StubIntlDateFormatterTest::testDefaultTimeZoneIntl()
  34. */
  35. public function testConstructorDefaultTimeZoneStub()
  36. {
  37. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  38. $this->assertNull($formatter->getTimeZoneId());
  39. }
  40. public function testConstructorDefaultTimeZoneIntl()
  41. {
  42. $this->skipIfIntlExtensionIsNotLoaded();
  43. $formatter = new \IntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  44. $this->assertNull($formatter->getTimeZoneId());
  45. }
  46. /**
  47. * @dataProvider formatProvider
  48. */
  49. public function testFormatStub($pattern, $timestamp, $expected)
  50. {
  51. $formatter = $this->createStubFormatter($pattern);
  52. $this->assertSame($expected, $formatter->format($timestamp));
  53. }
  54. /**
  55. * @dataProvider formatProvider
  56. */
  57. public function testFormatIntl($pattern, $timestamp, $expected)
  58. {
  59. $this->skipIfIntlExtensionIsNotLoaded();
  60. $formatter = $this->createIntlFormatter($pattern);
  61. $this->assertSame($expected, $formatter->format($timestamp));
  62. }
  63. public function formatProvider()
  64. {
  65. $formatData = array(
  66. /* general */
  67. array('y-M-d', 0, '1970-1-1'),
  68. array("yyyy.MM.dd 'at' HH:mm:ss zzz", 0, '1970.01.01 at 00:00:00 GMT+00:00'),
  69. array("EEE, MMM d, ''yy", 0, "Thu, Jan 1, '70"),
  70. array('h:mm a', 0, '12:00 AM'),
  71. array('K:mm a, z', 0, '0:00 AM, GMT+00:00'),
  72. array('yyyyy.MMMM.dd hh:mm aaa', 0, '01970.January.01 12:00 AM'),
  73. /* escaping */
  74. array("'M'", 0, 'M'),
  75. array("'yy'", 0, 'yy'),
  76. array("'''yy'", 0, "'yy"),
  77. array("''y", 0, "'1970"),
  78. array("''yy", 0, "'70"),
  79. array("H 'o'' clock'", 0, "0 o' clock"),
  80. /* month */
  81. array('M', 0, '1'),
  82. array('MM', 0, '01'),
  83. array('MMM', 0, 'Jan'),
  84. array('MMMM', 0, 'January'),
  85. array('MMMMM', 0, 'J'),
  86. array('MMMMMM', 0, '000001'),
  87. array('L', 0, '1'),
  88. array('LL', 0, '01'),
  89. array('LLL', 0, 'Jan'),
  90. array('LLLL', 0, 'January'),
  91. array('LLLLL', 0, 'J'),
  92. array('LLLLLL', 0, '000001'),
  93. /* year */
  94. array('y', 0, '1970'),
  95. array('yy', 0, '70'),
  96. array('yyy', 0, '1970'),
  97. array('yyyy', 0, '1970'),
  98. array('yyyyy', 0, '01970'),
  99. array('yyyyyy', 0, '001970'),
  100. /* day */
  101. array('d', 0, '1'),
  102. array('dd', 0, '01'),
  103. array('ddd', 0, '001'),
  104. /* quarter */
  105. array('Q', 0, '1'),
  106. array('QQ', 0, '01'),
  107. array('QQQ', 0, 'Q1'),
  108. array('QQQQ', 0, '1st quarter'),
  109. array('QQQQQ', 0, '1st quarter'),
  110. array('q', 0, '1'),
  111. array('qq', 0, '01'),
  112. array('qqq', 0, 'Q1'),
  113. array('qqqq', 0, '1st quarter'),
  114. array('qqqqq', 0, '1st quarter'),
  115. // 4 months
  116. array('Q', 7776000, '2'),
  117. array('QQ', 7776000, '02'),
  118. array('QQQ', 7776000, 'Q2'),
  119. array('QQQQ', 7776000, '2nd quarter'),
  120. // 7 months
  121. array('QQQQ', 15638400, '3rd quarter'),
  122. // 10 months
  123. array('QQQQ', 23587200, '4th quarter'),
  124. /* 12-hour (1-12) */
  125. array('h', 0, '12'),
  126. array('hh', 0, '12'),
  127. array('hhh', 0, '012'),
  128. array('h', 1, '12'),
  129. array('h', 3600, '1'),
  130. array('h', 43200, '12'), // 12 hours
  131. /* day of year */
  132. array('D', 0, '1'),
  133. array('D', 86400, '2'), // 1 day
  134. array('D', 31536000, '1'), // 1 year
  135. array('D', 31622400, '2'), // 1 year + 1 day
  136. /* day of week */
  137. array('E', 0, 'Thu'),
  138. array('EE', 0, 'Thu'),
  139. array('EEE', 0, 'Thu'),
  140. array('EEEE', 0, 'Thursday'),
  141. array('EEEEE', 0, 'T'),
  142. array('EEEEEE', 0, 'Thu'),
  143. array('E', 1296540000, 'Tue'), // 2011-02-01
  144. array('E', 1296950400, 'Sun'), // 2011-02-06
  145. /* am/pm marker */
  146. array('a', 0, 'AM'),
  147. array('aa', 0, 'AM'),
  148. array('aaa', 0, 'AM'),
  149. array('aaaa', 0, 'AM'),
  150. // 12 hours
  151. array('a', 43200, 'PM'),
  152. array('aa', 43200, 'PM'),
  153. array('aaa', 43200, 'PM'),
  154. array('aaaa', 43200, 'PM'),
  155. /* 24-hour (0-23) */
  156. array('H', 0, '0'),
  157. array('HH', 0, '00'),
  158. array('HHH', 0, '000'),
  159. array('H', 1, '0'),
  160. array('H', 3600, '1'),
  161. array('H', 43200, '12'),
  162. array('H', 46800, '13'),
  163. /* 24-hour (1-24) */
  164. array('k', 0, '24'),
  165. array('kk', 0, '24'),
  166. array('kkk', 0, '024'),
  167. array('k', 1, '24'),
  168. array('k', 3600, '1'),
  169. array('k', 43200, '12'),
  170. array('k', 46800, '13'),
  171. /* 12-hour (0-11) */
  172. array('K', 0, '0'),
  173. array('KK', 0, '00'),
  174. array('KKK', 0, '000'),
  175. array('K', 1, '0'),
  176. array('K', 3600, '1'),
  177. array('K', 43200, '0'), // 12 hours
  178. /* minute */
  179. array('m', 0, '0'),
  180. array('mm', 0, '00'),
  181. array('mmm', 0, '000'),
  182. array('m', 1, '0'),
  183. array('m', 60, '1'),
  184. array('m', 120, '2'),
  185. array('m', 180, '3'),
  186. array('m', 3600, '0'),
  187. array('m', 3660, '1'),
  188. array('m', 43200, '0'), // 12 hours
  189. /* second */
  190. array('s', 0, '0'),
  191. array('ss', 0, '00'),
  192. array('sss', 0, '000'),
  193. array('s', 1, '1'),
  194. array('s', 2, '2'),
  195. array('s', 5, '5'),
  196. array('s', 30, '30'),
  197. array('s', 59, '59'),
  198. array('s', 60, '0'),
  199. array('s', 120, '0'),
  200. array('s', 180, '0'),
  201. array('s', 3600, '0'),
  202. array('s', 3601, '1'),
  203. array('s', 3630, '30'),
  204. array('s', 43200, '0'), // 12 hours
  205. /* timezone */
  206. array('z', 0, 'GMT+00:00'),
  207. array('zz', 0, 'GMT+00:00'),
  208. array('zzz', 0, 'GMT+00:00'),
  209. array('zzzz', 0, 'GMT+00:00'),
  210. array('zzzzz', 0, 'GMT+00:00'),
  211. );
  212. return $formatData;
  213. }
  214. /**
  215. * @dataProvider formatWithTimezoneProvider
  216. */
  217. public function testFormatWithTimezoneStub($timestamp, $timezone, $expected)
  218. {
  219. $pattern = 'yyyy-MM-dd HH:mm:ss';
  220. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, $timezone, StubIntlDateFormatter::GREGORIAN, $pattern);
  221. $this->assertSame($expected, $formatter->format($timestamp));
  222. }
  223. /**
  224. * @dataProvider formatWithTimezoneProvider
  225. */
  226. public function testFormatWithTimezoneIntl($timestamp, $timezone, $expected)
  227. {
  228. $this->skipIfIntlExtensionIsNotLoaded();
  229. $pattern = 'yyyy-MM-dd HH:mm:ss';
  230. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $timezone, \IntlDateFormatter::GREGORIAN, $pattern);
  231. $this->assertSame($expected, $formatter->format($timestamp));
  232. }
  233. public function formatWithTimezoneProvider()
  234. {
  235. return array(
  236. array(0, 'UTC', '1970-01-01 00:00:00'),
  237. array(0, 'GMT', '1970-01-01 00:00:00'),
  238. array(0, 'GMT-03:00', '1969-12-31 21:00:00'),
  239. array(0, 'GMT+03:00', '1970-01-01 03:00:00'),
  240. array(0, 'Europe/Zurich', '1970-01-01 01:00:00'),
  241. array(0, 'Europe/Paris', '1970-01-01 01:00:00'),
  242. array(0, 'Africa/Cairo', '1970-01-01 02:00:00'),
  243. array(0, 'Africa/Casablanca', '1970-01-01 00:00:00'),
  244. array(0, 'Africa/Djibouti', '1970-01-01 03:00:00'),
  245. array(0, 'Africa/Johannesburg', '1970-01-01 02:00:00'),
  246. array(0, 'America/Antigua', '1969-12-31 20:00:00'),
  247. array(0, 'America/Toronto', '1969-12-31 19:00:00'),
  248. array(0, 'America/Vancouver', '1969-12-31 16:00:00'),
  249. array(0, 'Asia/Aqtau', '1970-01-01 05:00:00'),
  250. array(0, 'Asia/Bangkok', '1970-01-01 07:00:00'),
  251. array(0, 'Asia/Dubai', '1970-01-01 04:00:00'),
  252. array(0, 'Australia/Brisbane', '1970-01-01 10:00:00'),
  253. array(0, 'Australia/Eucla', '1970-01-01 08:45:00'),
  254. array(0, 'Australia/Melbourne', '1970-01-01 10:00:00'),
  255. array(0, 'Europe/Berlin', '1970-01-01 01:00:00'),
  256. array(0, 'Europe/Dublin', '1970-01-01 01:00:00'),
  257. array(0, 'Europe/Warsaw', '1970-01-01 01:00:00'),
  258. array(0, 'Pacific/Fiji', '1970-01-01 12:00:00'),
  259. // When time zone not exists, uses UTC by default
  260. array(0, 'Foo/Bar', '1970-01-01 00:00:00'),
  261. array(0, 'UTC+04:30', '1970-01-01 00:00:00'),
  262. array(0, 'UTC+04:AA', '1970-01-01 00:00:00'),
  263. );
  264. }
  265. /**
  266. * @expectedException Symfony\Component\Locale\Exception\NotImplementedException
  267. */
  268. public function testFormatWithTimezoneFormatOptionAndDifferentThanUtcStub()
  269. {
  270. $formatter = $this->createStubFormatter('zzzz');
  271. $formatter->setTimeZoneId('Pacific/Fiji');
  272. $formatter->format(0);
  273. }
  274. public function testFormatWithTimezoneFormatOptionAndDifferentThanUtcIntl()
  275. {
  276. $this->skipIfIntlExtensionIsNotLoaded();
  277. $formatter = $this->createIntlFormatter('zzzz');
  278. $formatter->setTimeZoneId('Pacific/Fiji');
  279. $this->assertEquals('Fiji Time', $formatter->format(0));
  280. }
  281. public function testFormatWithGmtTimezoneStub()
  282. {
  283. $formatter = $this->createStubFormatter('zzzz');
  284. $formatter->setTimeZoneId('GMT+03:00');
  285. $this->assertEquals('GMT+03:00', $formatter->format(0));
  286. }
  287. public function testFormatWithGmtTimezoneIntl()
  288. {
  289. $this->skipIfIntlExtensionIsNotLoaded();
  290. $formatter = $this->createIntlFormatter('zzzz');
  291. $formatter->setTimeZoneId('GMT+03:00');
  292. $this->assertEquals('GMT+03:00', $formatter->format(0));
  293. }
  294. public function testFormatWithDefaultTimezoneStub()
  295. {
  296. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  297. $formatter->setPattern('yyyy-MM-dd HH:mm:ss');
  298. $this->assertEquals(
  299. $this->createDateTime(0)->format('Y-m-d H:i:s'),
  300. $formatter->format(0)
  301. );
  302. }
  303. public function testFormatWithDefaultTimezoneIntl()
  304. {
  305. $this->skipIfIntlExtensionIsNotLoaded();
  306. $formatter = new \IntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  307. $formatter->setPattern('yyyy-MM-dd HH:mm:ss');
  308. $this->assertEquals(
  309. $this->createDateTime(0)->format('Y-m-d H:i:s'),
  310. $formatter->format(0)
  311. );
  312. }
  313. /**
  314. * @expectedException Symfony\Component\Locale\Exception\NotImplementedException
  315. */
  316. public function testFormatWithUnimplementedCharsStub()
  317. {
  318. $pattern = 'Y';
  319. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  320. $formatter->format(0);
  321. }
  322. /**
  323. * @expectedException Symfony\Component\Locale\Exception\NotImplementedException
  324. */
  325. public function testFormatWithNonIntegerTimestamp()
  326. {
  327. $formatter = $this->createStubFormatter();
  328. $formatter->format(array());
  329. }
  330. /**
  331. * @dataProvider dateAndTimeTypeProvider
  332. */
  333. public function testDateAndTimeTypeStub($timestamp, $datetype, $timetype, $expected)
  334. {
  335. $formatter = new StubIntlDateFormatter('en', $datetype, $timetype, 'UTC');
  336. $this->assertSame($expected, $formatter->format($timestamp));
  337. }
  338. /**
  339. * @dataProvider dateAndTimeTypeProvider
  340. */
  341. public function testDateAndTimeTypeIntl($timestamp, $datetype, $timetype, $expected)
  342. {
  343. $this->skipIfIntlExtensionIsNotLoaded();
  344. $formatter = new \IntlDateFormatter('en', $datetype, $timetype, 'UTC');
  345. $this->assertSame($expected, $formatter->format($timestamp));
  346. }
  347. public function dateAndTimeTypeProvider()
  348. {
  349. return array(
  350. array(0, StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'Thursday, January 1, 1970'),
  351. array(0, StubIntlDateFormatter::LONG, StubIntlDateFormatter::NONE, 'January 1, 1970'),
  352. array(0, StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::NONE, 'Jan 1, 1970'),
  353. array(0, StubIntlDateFormatter::SHORT, StubIntlDateFormatter::NONE, '1/1/70'),
  354. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL, '12:00:00 AM GMT+00:00'),
  355. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::LONG, '12:00:00 AM GMT+00:00'),
  356. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::MEDIUM, '12:00:00 AM'),
  357. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::SHORT, '12:00 AM'),
  358. );
  359. }
  360. public function testGetCalendar()
  361. {
  362. $formatter = $this->createStubFormatter();
  363. $this->assertEquals(StubIntlDateFormatter::GREGORIAN, $formatter->getCalendar());
  364. }
  365. public function testGetDateType()
  366. {
  367. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  368. $this->assertEquals(StubIntlDateFormatter::FULL, $formatter->getDateType());
  369. }
  370. public function testGetErrorCode()
  371. {
  372. $formatter = $this->createStubFormatter();
  373. $this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR, $formatter->getErrorCode());
  374. }
  375. public function testGetErrorMessage()
  376. {
  377. $formatter = $this->createStubFormatter();
  378. $this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR_MESSAGE, $formatter->getErrorMessage());
  379. }
  380. public function testGetLocale()
  381. {
  382. $formatter = $this->createStubFormatter();
  383. $this->assertEquals('en', $formatter->getLocale());
  384. }
  385. public function testGetPattern()
  386. {
  387. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'UTC', StubIntlDateFormatter::GREGORIAN, 'yyyy-MM-dd');
  388. $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
  389. }
  390. public function testGetTimeType()
  391. {
  392. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL);
  393. $this->assertEquals(StubIntlDateFormatter::FULL, $formatter->getTimeType());
  394. }
  395. /**
  396. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  397. */
  398. public function testIsLenient()
  399. {
  400. $formatter = $this->createStubFormatter();
  401. $formatter->isLenient();
  402. }
  403. /**
  404. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  405. */
  406. public function testLocaltime()
  407. {
  408. $formatter = $this->createStubFormatter();
  409. $formatter->localtime('Wednesday, December 31, 1969 4:00:00 PM PT');
  410. }
  411. /**
  412. * @dataProvider parseProvider
  413. */
  414. public function testParseIntl($pattern, $value, $expected)
  415. {
  416. $this->skipIfIntlExtensionIsNotLoaded();
  417. $formatter = $this->createIntlFormatter($pattern);
  418. $this->assertSame($expected, $formatter->parse($value));
  419. }
  420. /**
  421. * @dataProvider parseProvider
  422. */
  423. public function testParseStub($pattern, $value, $expected)
  424. {
  425. $formatter = $this->createStubFormatter($pattern);
  426. $this->assertSame($expected, $formatter->parse($value));
  427. }
  428. public function parseProvider()
  429. {
  430. return array(
  431. // years
  432. array('y-M-d', '1970-1-1', 0),
  433. // TODO: review to support or not this variant
  434. // array('yy-M-d', '70-1-1', 0),
  435. // months
  436. array('y-M-d', '1970-1-1', 0),
  437. array('y-MMM-d', '1970-Jan-1', 0),
  438. array('y-MMMM-d', '1970-January-1', 0),
  439. // 1 char month
  440. array('y-MMMMM-d', '1970-J-1', false),
  441. array('y-MMMMM-d', '1970-S-1', false),
  442. // standalone months
  443. array('y-L-d', '1970-1-1', 0),
  444. array('y-LLL-d', '1970-Jan-1', 0),
  445. array('y-LLLL-d', '1970-January-1', 0),
  446. // standalone 1 char month
  447. array('y-LLLLL-d', '1970-J-1', false),
  448. array('y-LLLLL-d', '1970-S-1', false),
  449. // days
  450. array('y-M-d', '1970-1-1', 0),
  451. array('y-M-dd', '1970-1-01', 0),
  452. array('y-M-ddd', '1970-1-001', 0),
  453. // 12 hours (1-12)
  454. array('y-M-d h', '1970-1-1 1', 3600),
  455. array('y-M-d h', '1970-1-1 10', 36000),
  456. array('y-M-d hh', '1970-1-1 11', 39600),
  457. array('y-M-d hh', '1970-1-1 12', 0),
  458. array('y-M-d hh a', '1970-1-1 0 AM', 0),
  459. array('y-M-d hh a', '1970-1-1 1 AM', 3600),
  460. array('y-M-d hh a', '1970-1-1 10 AM', 36000),
  461. array('y-M-d hh a', '1970-1-1 11 AM', 39600),
  462. array('y-M-d hh a', '1970-1-1 12 AM', 0),
  463. array('y-M-d hh a', '1970-1-1 23 AM', 82800),
  464. array('y-M-d hh a', '1970-1-1 24 AM', 86400),
  465. array('y-M-d hh a', '1970-1-1 0 PM', 43200),
  466. array('y-M-d hh a', '1970-1-1 1 PM', 46800),
  467. array('y-M-d hh a', '1970-1-1 10 PM', 79200),
  468. array('y-M-d hh a', '1970-1-1 11 PM', 82800),
  469. array('y-M-d hh a', '1970-1-1 12 PM', 43200),
  470. array('y-M-d hh a', '1970-1-1 23 PM', 126000),
  471. array('y-M-d hh a', '1970-1-1 24 PM', 129600),
  472. // 12 hours (0-11)
  473. array('y-M-d K', '1970-1-1 1', 3600),
  474. array('y-M-d K', '1970-1-1 10', 36000),
  475. array('y-M-d KK', '1970-1-1 11', 39600),
  476. array('y-M-d KK', '1970-1-1 12', 43200),
  477. array('y-M-d KK a', '1970-1-1 0 AM', 0),
  478. array('y-M-d KK a', '1970-1-1 1 AM', 3600),
  479. array('y-M-d KK a', '1970-1-1 10 AM', 36000),
  480. array('y-M-d KK a', '1970-1-1 11 AM', 39600),
  481. array('y-M-d KK a', '1970-1-1 12 AM', 43200),
  482. array('y-M-d KK a', '1970-1-1 23 AM', 82800),
  483. array('y-M-d KK a', '1970-1-1 24 AM', 86400),
  484. array('y-M-d KK a', '1970-1-1 0 PM', 43200),
  485. array('y-M-d KK a', '1970-1-1 1 PM', 46800),
  486. array('y-M-d KK a', '1970-1-1 10 PM', 79200),
  487. array('y-M-d KK a', '1970-1-1 11 PM', 82800),
  488. array('y-M-d KK a', '1970-1-1 12 PM', 86400),
  489. array('y-M-d KK a', '1970-1-1 23 PM', 126000),
  490. array('y-M-d KK a', '1970-1-1 24 PM', 129600),
  491. // 24 hours (0-23)
  492. array('y-M-d H', '1970-1-1 0', 0),
  493. array('y-M-d H', '1970-1-1 1', 3600),
  494. array('y-M-d H', '1970-1-1 10', 36000),
  495. array('y-M-d HH', '1970-1-1 11', 39600),
  496. array('y-M-d HH', '1970-1-1 12', 43200),
  497. array('y-M-d HH', '1970-1-1 23', 82800),
  498. array('y-M-d HH a', '1970-1-1 0 AM', 0),
  499. array('y-M-d HH a', '1970-1-1 1 AM', 0),
  500. array('y-M-d HH a', '1970-1-1 10 AM', 0),
  501. array('y-M-d HH a', '1970-1-1 11 AM', 0),
  502. array('y-M-d HH a', '1970-1-1 12 AM', 0),
  503. array('y-M-d HH a', '1970-1-1 23 AM', 0),
  504. array('y-M-d HH a', '1970-1-1 24 AM', 0),
  505. array('y-M-d HH a', '1970-1-1 0 PM', 43200),
  506. array('y-M-d HH a', '1970-1-1 1 PM', 43200),
  507. array('y-M-d HH a', '1970-1-1 10 PM', 43200),
  508. array('y-M-d HH a', '1970-1-1 11 PM', 43200),
  509. array('y-M-d HH a', '1970-1-1 12 PM', 43200),
  510. array('y-M-d HH a', '1970-1-1 23 PM', 43200),
  511. array('y-M-d HH a', '1970-1-1 24 PM', 43200),
  512. // 24 hours (1-24)
  513. array('y-M-d k', '1970-1-1 1', 3600),
  514. array('y-M-d k', '1970-1-1 10', 36000),
  515. array('y-M-d kk', '1970-1-1 11', 39600),
  516. array('y-M-d kk', '1970-1-1 12', 43200),
  517. array('y-M-d kk', '1970-1-1 23', 82800),
  518. array('y-M-d kk', '1970-1-1 24', 0),
  519. array('y-M-d kk a', '1970-1-1 0 AM', 0),
  520. array('y-M-d kk a', '1970-1-1 1 AM', 0),
  521. array('y-M-d kk a', '1970-1-1 10 AM', 0),
  522. array('y-M-d kk a', '1970-1-1 11 AM', 0),
  523. array('y-M-d kk a', '1970-1-1 12 AM', 0),
  524. array('y-M-d kk a', '1970-1-1 23 AM', 0),
  525. array('y-M-d kk a', '1970-1-1 24 AM', 0),
  526. array('y-M-d kk a', '1970-1-1 0 PM', 43200),
  527. array('y-M-d kk a', '1970-1-1 1 PM', 43200),
  528. array('y-M-d kk a', '1970-1-1 10 PM', 43200),
  529. array('y-M-d kk a', '1970-1-1 11 PM', 43200),
  530. array('y-M-d kk a', '1970-1-1 12 PM', 43200),
  531. array('y-M-d kk a', '1970-1-1 23 PM', 43200),
  532. array('y-M-d kk a', '1970-1-1 24 PM', 43200),
  533. // minutes
  534. array('y-M-d HH:m', '1970-1-1 0:1', 60),
  535. array('y-M-d HH:mm', '1970-1-1 0:10', 600),
  536. // seconds
  537. array('y-M-d HH:mm:s', '1970-1-1 00:01:1', 61),
  538. array('y-M-d HH:mm:ss', '1970-1-1 00:01:10', 70),
  539. // timezone
  540. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT-03:00', 10800),
  541. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT-04:00', 14400),
  542. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT-00:00', 0),
  543. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT+03:00', -10800),
  544. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT+04:00', -14400),
  545. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT-0300', 10800),
  546. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT+0300', -10800),
  547. // a previous timezoned parsing should not change the timezone for the next parsing
  548. array('y-M-d HH:mm:ss', '1970-1-1 00:00:00', 0),
  549. // AM/PM (already covered by hours tests)
  550. array('y-M-d HH:mm:ss a', '1970-1-1 00:00:00 AM', 0),
  551. array('y-M-d HH:mm:ss a', '1970-1-1 00:00:00 PM', 43200),
  552. // regExp metachars in the pattern string
  553. array('y[M-d', '1970[1-1', 0),
  554. array('y[M/d', '1970[1/1', 0),
  555. // quote characters
  556. array("'M'", 'M', 0),
  557. array("'yy'", 'yy', 0),
  558. array("'''yy'", "'yy", 0),
  559. array("''y", "'1970", 0),
  560. array("H 'o'' clock'", "0 o' clock", 0),
  561. );
  562. }
  563. /**
  564. * Just to document the differences between the stub and the intl implementations. The intl can parse
  565. * any of the tested formats alone. The stub does not implement them as it would be needed to add more
  566. * abstraction, passing more context to the transformers objects. Any of the formats are ignored alone
  567. * or with date/time data (years, months, days, hours, minutes and seconds).
  568. *
  569. * Also in intl, format like 'ss E' for '10 2' (2nd day of year + 10 seconds) are added, then we have
  570. * 86,400 seconds (24h * 60min * 60s) + 10 seconds
  571. *
  572. * @dataProvider parseDifferences()
  573. */
  574. public function testParseDifferencesStub($pattern, $value, $stubExpected, $intlExpected)
  575. {
  576. $formatter = $this->createStubFormatter($pattern);
  577. $this->assertSame($stubExpected, $formatter->parse($value));
  578. }
  579. /**
  580. * @dataProvider parseDifferences()
  581. */
  582. public function testParseDifferencesIntl($pattern, $value, $stubExpected, $intlExpected)
  583. {
  584. $this->skipIfIntlExtensionIsNotLoaded();
  585. $formatter = $this->createIntlFormatter($pattern);
  586. $this->assertSame($intlExpected, $formatter->parse($value));
  587. }
  588. public function parseDifferences()
  589. {
  590. return array(
  591. // AM/PM, ignored if alone
  592. array('a', 'AM', 0, 0),
  593. array('a', 'PM', 0, 43200),
  594. // day of week
  595. array('E', 'Thu', 0, 0),
  596. array('EE', 'Thu', 0, 0),
  597. array('EEE', 'Thu', 0, 0),
  598. array('EEEE', 'Thursday', 0, 0),
  599. array('EEEEE', 'T', 0, 432000),
  600. array('EEEEEE', 'Thu', 0, 0),
  601. // day of year
  602. array('D', '1', 0, 0),
  603. array('D', '2', 0, 86400),
  604. // quarter
  605. array('Q', '1', 0, 0),
  606. array('QQ', '01', 0, 0),
  607. array('QQQ', 'Q1', 0, 0),
  608. array('QQQQ', '1st quarter', 0, 0),
  609. array('QQQQQ', '1st quarter', 0, 0),
  610. array('Q', '2', 0, 7776000),
  611. array('QQ', '02', 0, 7776000),
  612. array('QQQ', 'Q2', 0, 7776000),
  613. array('QQQQ', '2nd quarter', 0, 7776000),
  614. array('QQQQQ', '2nd quarter', 0, 7776000),
  615. array('q', '1', 0, 0),
  616. array('qq', '01', 0, 0),
  617. array('qqq', 'Q1', 0, 0),
  618. array('qqqq', '1st quarter', 0, 0),
  619. array('qqqqq', '1st quarter', 0, 0),
  620. );
  621. }
  622. public function testParseWithNullPositionValueStub()
  623. {
  624. $position = null;
  625. $formatter = $this->createStubFormatter('y');
  626. $this->assertSame(0, $formatter->parse('1970', $position));
  627. $this->assertNull($position);
  628. }
  629. /**
  630. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException
  631. */
  632. public function testParseWithNotNullPositionValueStub()
  633. {
  634. $position = 0;
  635. $formatter = $this->createStubFormatter('y');
  636. $this->assertSame(0, $formatter->parse('1970', $position));
  637. }
  638. /**
  639. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  640. */
  641. public function testSetCalendar()
  642. {
  643. $formatter = $this->createStubFormatter();
  644. $formatter->setCalendar(StubIntlDateFormatter::GREGORIAN);
  645. }
  646. /**
  647. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  648. */
  649. public function testSetLenient()
  650. {
  651. $formatter = $this->createStubFormatter();
  652. $formatter->setLenient(true);
  653. }
  654. public function testSetPattern()
  655. {
  656. $formatter = $this->createStubFormatter();
  657. $formatter->setPattern('yyyy-MM-dd');
  658. $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
  659. }
  660. /**
  661. * @covers Symfony\Component\Locale\Stub\StubIntlDateFormatter::getTimeZoneId
  662. * @dataProvider setTimeZoneIdProvider()
  663. */
  664. public function testSetTimeZoneIdStub($timeZoneId)
  665. {
  666. $formatter = $this->createStubFormatter();
  667. $formatter->setTimeZoneId($timeZoneId);
  668. $this->assertEquals($timeZoneId, $formatter->getTimeZoneId());
  669. }
  670. /**
  671. * @dataProvider setTimeZoneIdProvider()
  672. */
  673. public function testSetTimeZoneIdIntl($timeZoneId)
  674. {
  675. $this->skipIfIntlExtensionIsNotLoaded();
  676. $formatter = $this->createIntlFormatter();
  677. $formatter->setTimeZoneId($timeZoneId);
  678. $this->assertEquals($timeZoneId, $formatter->getTimeZoneId());
  679. }
  680. public function setTimeZoneIdProvider()
  681. {
  682. return array(
  683. array('UTC'),
  684. array('GMT'),
  685. array('GMT-03:00'),
  686. array('GMT-0300'),
  687. array('Europe/Zurich'),
  688. // When time zone not exists, uses UTC by default
  689. array('Foo/Bar'),
  690. array('GMT+00:AA'),
  691. array('GMT+00AA'),
  692. );
  693. }
  694. /**
  695. * @expectedException Symfony\Component\Locale\Exception\NotImplementedException
  696. */
  697. public function testSetTimeZoneIdWithGmtTimeZoneWithMinutesOffsetStub()
  698. {
  699. $formatter = $this->createStubFormatter();
  700. $formatter->setTimeZoneId('GMT+00:30');
  701. }
  702. public function testSetTimeZoneIdWithGmtTimeZoneWithMinutesOffsetIntl()
  703. {
  704. $this->skipIfIntlExtensionIsNotLoaded();
  705. $formatter = $this->createIntlFormatter();
  706. $formatter->setTimeZoneId('GMT+00:30');
  707. $this->assertEquals('GMT+00:30', $formatter->getTimeZoneId());
  708. }
  709. public function testStaticCreate()
  710. {
  711. $formatter = StubIntlDateFormatter::create('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  712. $this->assertInstanceOf('Symfony\Component\Locale\Stub\StubIntlDateFormatter', $formatter);
  713. }
  714. protected function createStubFormatter($pattern = null)
  715. {
  716. return new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  717. }
  718. protected function createIntlFormatter($pattern = null)
  719. {
  720. return new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
  721. }
  722. protected function createDateTime($timestamp = null, $timeZone = null)
  723. {
  724. $timestamp = is_null($timestamp) ? time() : $timestamp;
  725. $timeZone = is_null($timeZone) ? date_default_timezone_get() : $timeZone;
  726. $dateTime = new \DateTime();
  727. $dateTime->setTimestamp($timestamp);
  728. $dateTime->setTimeZone(new \DateTimeZone($timeZone));
  729. return $dateTime;
  730. }
  731. }