浏览代码

merged branch eriksencosta/locale (PR #3840)

Commits
-------

31dde14 [Locale] updated StubIntlDateFormatter::format() behavior for PHP >= 5.3.4

Discussion
----------

[Locale] updated StubIntlDateFormatter::format() behavior for PHP >= 5.3.4

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -

[![Build Status](https://secure.travis-ci.org/eriksencosta/symfony.png?branch=locale)](http://travis-ci.org/eriksencosta/symfony)

---------------------------------------------------------------------------

by drak at 2012-04-08T23:20:20Z

This looks like a feature addition as opposed to a bug fix and if so, should probably go in the master branch rather than 2.0.

---------------------------------------------------------------------------

by stof at 2012-04-08T23:30:25Z

@drak it is a bug fix as the stub is meant to have the same behavior than the real intl

---------------------------------------------------------------------------

by drak at 2012-04-08T23:36:08Z

ok, thanks for the clarification, I wasnt sure.

---------------------------------------------------------------------------

by eriksencosta at 2012-04-08T23:44:22Z

@stof is right. I myself fixed it in master and then remembered that bug fixes should be made in 2.0.
Fabien Potencier 13 年之前
父节点
当前提交
d8652785cf

+ 7 - 2
src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php

@@ -174,13 +174,18 @@ class StubIntlDateFormatter
             throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, 'Only integer unix timestamps are supported');
         }
 
-        if (!is_int($timestamp)) {
-            // behave like the intl extension
+        // behave like the intl extension
+        if (!is_int($timestamp) && version_compare(\PHP_VERSION, '5.3.4', '<')) {
             StubIntl::setErrorCode(StubIntl::U_ILLEGAL_ARGUMENT_ERROR);
 
             return false;
         }
 
+        // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances
+        if ($timestamp instanceOf \DateTime && version_compare(\PHP_VERSION, '5.3.4', '>=')) {
+            $timestamp = $timestamp->getTimestamp();
+        }
+
         $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
         $formatted = $transformer->format($this->createDateTime($timestamp));
 

+ 13 - 0
tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php

@@ -276,6 +276,19 @@ class StubIntlDateFormatterTest extends LocaleTestCase
             array('zzzzz', 0, 'GMT+00:00'),
         );
 
+        // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances
+        if (version_compare(\PHP_VERSION, '5.3.4', '>=')) {
+            $dateTime = new \DateTime('@0');
+
+            /* general, DateTime */
+            $formatData[] = array('y-M-d', $dateTime, '1970-1-1');
+            $formatData[] = array("yyyy.MM.dd 'at' HH:mm:ss zzz", $dateTime, '1970.01.01 at 00:00:00 GMT+00:00');
+            $formatData[] = array("EEE, MMM d, ''yy", $dateTime, "Thu, Jan 1, '70");
+            $formatData[] = array('h:mm a', $dateTime, '12:00 AM');
+            $formatData[] = array('K:mm a, z', $dateTime, '0:00 AM, GMT+00:00');
+            $formatData[] = array('yyyyy.MMMM.dd hh:mm aaa', $dateTime, '01970.January.01 12:00 AM');
+        }
+
         return $formatData;
     }