Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilwylegala committed May 30, 2024
1 parent 14de593 commit 2bc0902
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 50 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"require": {
"php": "^8.0",
"ext-mbstring": "*",
"symfony/polyfill-intl-icu": "^1.29"
"symfony/polyfill-intl-icu": "^1.29",
"php81_bc/strftime": "^0.7.5"
},
"suggest": {
"ext-openssl": "You need to install ext-openssl or ext-mcrypt to use AES-256 encryption",
Expand Down
24 changes: 13 additions & 11 deletions lib/Cake/Test/Case/Utility/CakeTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,7 @@ public function testTimeAgoInWordsWithFormat() {
$this->assertEquals('on 2007-09-25', $result);

$result = $this->Time->timeAgoInWords('2007-9-25', '%x');
// @codingStandardsIgnoreStart
$this->assertEquals('on ' . @strftime('%x', strtotime('2007-9-25')), $result);
// @codingStandardsIgnoreEnd
$this->assertEquals('on ' . PHP81_BC\strftime('%x', strtotime('2007-9-25')), $result);

$result = $this->Time->timeAgoInWords(
strtotime('+2 weeks +2 days'),
Expand All @@ -306,7 +304,7 @@ public function testTimeAgoInWordsWithFormat() {
array('end' => '1 month', 'format' => '%x')
);
// @codingStandardsIgnoreStart
$this->assertEquals('on ' . @strftime('%x', strtotime('+2 months +2 days')), $result);
$this->assertEquals('on ' . PHP81_BC\strftime('%x', strtotime('+2 months +2 days')), $result);
// @codingStandardsIgnoreEnd
}

Expand Down Expand Up @@ -476,11 +474,11 @@ public function testNiceShort() {
* @return void
*/
public function testNiceShortI18n() {
$restore = setlocale(LC_ALL, 0);
setlocale(LC_ALL, 'es_ES');
$restore = Locale::getDefault();
Locale::setDefault('es_ES');
$time = strtotime('2015-01-07 03:05:00');
$this->assertEquals('ene 7th 2015, 03:05', $this->Time->niceShort($time));
setlocale(LC_ALL, $restore);
Locale::setDefault($restore);
}

/**
Expand Down Expand Up @@ -1166,20 +1164,23 @@ public function testConvertPercentE() {
* @return void
*/
public function testI18nFormat() {
$resetLocale = Locale::getDefault();
App::build(array(
'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
), App::RESET);
Configure::write('Config.language', 'time_test');

$time = strtotime('Thu Jan 14 13:59:28 2010');


Locale::setDefault('en_US');
$result = $this->Time->i18nFormat($time);
$expected = '14/01/10';
$this->assertEquals($expected, $result);

$result = $this->Time->i18nFormat($time, '%c');
// @codingStandardsIgnoreStart
$expected = 'jue 14 ene 2010 13:59:28 ' . mb_convert_encoding(@strftime('%Z', $time), 'UTF-8', 'ISO-8859-1');
$expected = 'jue 14 ene 2010 13:59:28 ' . mb_convert_encoding(PHP81_BC\strftime('%Z', $time), 'UTF-8', 'ISO-8859-1');
// @codingStandardsIgnoreEnd
$this->assertEquals($expected, $result);

Expand All @@ -1193,10 +1194,9 @@ public function testI18nFormat() {
$expected = '13/01/10';
$this->assertEquals($expected, $result);

Locale::setDefault('es_ES');
$result = $this->Time->i18nFormat($time, '%c');
// @codingStandardsIgnoreStart
$expected = 'mié 13 ene 2010 13:59:28 ' . mb_convert_encoding(@strftime('%Z', $time), 'UTF-8', 'ISO-8859-1');
// @codingStandardsIgnoreEnd
$expected = 'mié 13 ene 2010 13:59:28 ' . mb_convert_encoding(PHP81_BC\strftime('%Z', $time), 'UTF-8', 'ISO-8859-1');
$this->assertEquals($expected, $result);

$result = $this->Time->i18nFormat($time, 'Time is %r, and date is %x');
Expand All @@ -1206,6 +1206,8 @@ public function testI18nFormat() {
$result = $this->Time->i18nFormat('invalid date', '%x', 'Date invalid');
$expected = 'Date invalid';
$this->assertEquals($expected, $result);

Locale::setDefault($resetLocale);
}

public function testI18nFormatTimezoneConversionToUTC() {
Expand Down
43 changes: 5 additions & 38 deletions lib/Cake/Utility/CakeTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -1167,55 +1167,22 @@ public static function listTimezones($filter = null, $country = null, $options =
* @return string formatted string with correct encoding.
*/
protected static function _strftime($format, $timestamp) {
$intlFormat = self::convertStrftimeFormatToIntl($format);
$locale = setlocale(LC_ALL, 0);
$dateFormatter = new IntlDateFormatter(
$locale,
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
date_default_timezone_get(),
IntlDateFormatter::GREGORIAN,
$intlFormat
);
$formatted = $dateFormatter->format($timestamp);
$format = PHP81_BC\strftime($format, $timestamp);

$encoding = Configure::read('App.encoding');
if (!empty($encoding) && $encoding === 'UTF-8') {
if (function_exists('mb_check_encoding')) {
$valid = mb_check_encoding($formatted, $encoding);
$valid = mb_check_encoding($format, $encoding);
} else {
$valid = Multibyte::checkMultibyte($formatted);
$valid = Multibyte::checkMultibyte($format);
}
if (!$valid) {
$formatted = mb_convert_encoding($formatted, 'UTF-8', 'ISO-8859-1');
$formatted = mb_convert_encoding($format, 'UTF-8', 'ISO-8859-1');
}
}
return $formatted;
return $format;
}

private static function convertStrftimeFormatToIntl($format) {
$conversion = array(
'%a' => 'eee', // abbreviated weekday name
'%A' => 'eeee', // full weekday name
'%w' => 'e', // day of the week
'%d' => 'dd', // day of the month
'%b' => 'MMM', // abbreviated month name
'%B' => 'MMMM', // full month name
'%m' => 'MM', // month of the year
'%y' => 'yy', // year without century
'%Y' => 'yyyy', // year with century
'%H' => 'HH', // hour (24-hour clock)
'%I' => 'hh', // hour (12-hour clock)
'%p' => 'a', // AM or PM designation
'%M' => 'mm', // minute
'%S' => 'ss', // second
'%z' => 'ZZZZ', // time zone offset
'%Z' => 'zzzz', // time zone abbreviation
'%%' => '%' // literal percentage sign
);

return strtr($format, $conversion);
}

/**
* Multibyte wrapper for strftime.
Expand Down

0 comments on commit 2bc0902

Please sign in to comment.