]> granicus.if.org Git - php/commitdiff
Fixed bug #69374 IntlDateFormatter formatObject returns wrong utf8 value
authorMic <lenhatanh86@gmail.com>
Thu, 2 Jun 2016 20:27:48 +0000 (22:27 +0200)
committerAnatol Belski <ab@php.net>
Thu, 2 Jun 2016 20:27:48 +0000 (22:27 +0200)
Relying on invariant strings is a mistake. Not only UTF-8, but also
many charsets are not single byte. Actual date formats can be mixed
with arbitrary strings, and this can bring erroneous results in the
out. Thus, instead it is more convenient to say, that a format string
can consist either on UTF-8 or on pure ASCII as its subset. This is
what is currently being done in other classes like Formatter, etc.
as well.

ext/intl/dateformat/dateformat_format_object.cpp
ext/intl/tests/bug69374.phpt [new file with mode: 0644]

index 3be76332a812be894a2a4abc76ead5da6cfaf70a..accb27e63d7bc35efc0d1f5779e6aec9c4e87b71 100644 (file)
@@ -188,11 +188,11 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
        }
 
        if (pattern) {
-                df = new SimpleDateFormat(
-                               UnicodeString(Z_STRVAL_P(format), Z_STRLEN_P(format),
-                                               UnicodeString::kInvariant),
-                               Locale::createFromName(locale_str),
-                               status);
+               StringPiece sp(Z_STRVAL_P(format));
+               df = new SimpleDateFormat(
+                       UnicodeString::fromUTF8(sp),
+                       Locale::createFromName(locale_str),
+                       status);
 
                if (U_FAILURE(status)) {
                        intl_error_set(NULL, status,
diff --git a/ext/intl/tests/bug69374.phpt b/ext/intl/tests/bug69374.phpt
new file mode 100644 (file)
index 0000000..4d9fffa
--- /dev/null
@@ -0,0 +1,24 @@
+--TEST--
+IntlDateFormatter::formatObject(): returns wrong utf8 value when $format param is utf8 string pattern.
+--SKIPIF--
+<?php
+if (!extension_loaded('intl')) die('skip intl extension not enabled'); ?>
+<?php if (version_compare(INTL_ICU_VERSION, '50.1.2') < 0) die('skip for ICU >= 51.1.2'); ?>
+--FILE--
+<?php
+$millitimestamp = 1428133423941.0; // 14:43:43 April 04 2015
+$pattern1 = '\'tháng\' MM, y'; // yMM format for Vietnamese
+$pattern2 = 'y년 MMM'; // yMM format for Korean
+$date = IntlCalendar::createInstance('Asia/Ho_Chi_Minh');
+$date->setTime($millitimestamp);
+echo IntlDateFormatter::formatObject($date, $pattern1, 'vi_VN'), "\n";
+echo IntlDateFormatter::formatObject ($date, $pattern2, 'ko_KR'), "\n";
+?>
+==DONE==
+
+--EXPECTF--
+tháng 04, 2015
+2015년 4월
+==DONE==
+
+