Skip to content

Commit 527ddac

Browse files
mictiweltling
authored andcommitted
Fixed bug #69374 IntlDateFormatter formatObject returns wrong utf8 value
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.
1 parent 4b2fb0b commit 527ddac

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

ext/intl/dateformat/dateformat_format_object.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
188188
}
189189

190190
if (pattern) {
191-
df = new SimpleDateFormat(
192-
UnicodeString(Z_STRVAL_P(format), Z_STRLEN_P(format),
193-
UnicodeString::kInvariant),
194-
Locale::createFromName(locale_str),
195-
status);
191+
StringPiece sp(Z_STRVAL_P(format));
192+
df = new SimpleDateFormat(
193+
UnicodeString::fromUTF8(sp),
194+
Locale::createFromName(locale_str),
195+
status);
196196

197197
if (U_FAILURE(status)) {
198198
intl_error_set(NULL, status,

ext/intl/tests/bug69374.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
IntlDateFormatter::formatObject(): returns wrong utf8 value when $format param is utf8 string pattern.
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('intl')) die('skip intl extension not enabled'); ?>
6+
<?php if (version_compare(INTL_ICU_VERSION, '50.1.2') < 0) die('skip for ICU >= 51.1.2'); ?>
7+
--FILE--
8+
<?php
9+
$millitimestamp = 1428133423941.0; // 14:43:43 April 04 2015
10+
$pattern1 = '\'tháng\' MM, y'; // yMM format for Vietnamese
11+
$pattern2 = 'y년 MMM'; // yMM format for Korean
12+
$date = IntlCalendar::createInstance('Asia/Ho_Chi_Minh');
13+
$date->setTime($millitimestamp);
14+
echo IntlDateFormatter::formatObject($date, $pattern1, 'vi_VN'), "\n";
15+
echo IntlDateFormatter::formatObject ($date, $pattern2, 'ko_KR'), "\n";
16+
?>
17+
==DONE==
18+
19+
--EXPECTF--
20+
tháng 04, 2015
21+
2015년 4월
22+
==DONE==
23+
24+

0 commit comments

Comments
 (0)