Fix timezone adjustment in CakeTime::nice and CakeTime::niceShort

This commit is contained in:
bancer 2019-01-25 13:56:50 +01:00
parent e845e8876d
commit ec5bac5e52

View file

@ -354,22 +354,22 @@ class CakeTime {
* See http://php.net/manual/en/function.strftime.php for information on formatting * See http://php.net/manual/en/function.strftime.php for information on formatting
* using locale strings. * using locale strings.
* *
* @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object * @param int|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object
* @param string|DateTimeZone $timezone Timezone string or DateTimeZone object * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object
* @param string $format The format to use. If null, `CakeTime::$niceFormat` is used * @param string $format The format to use. If null, `CakeTime::$niceFormat` is used
* @return string Formatted date string * @return string Formatted date string
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::nice * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::nice
*/ */
public static function nice($dateString = null, $timezone = null, $format = null) { public static function nice($date = null, $timezone = null, $format = null) {
if (!$dateString) { if (!$date) {
$dateString = time(); $date = time();
} }
$date = static::fromString($dateString, $timezone); $timestamp = static::fromString($date, $timezone);
if (!$format) { if (!$format) {
$format = static::$niceFormat; $format = static::$niceFormat;
} }
return static::_strftime(static::convertSpecifiers($format, $date), $date); $convertedFormat = static::convertSpecifiers($format, $timestamp);
return static::__strftime($convertedFormat, $timestamp, $date, $timezone);
} }
/** /**
@ -382,28 +382,31 @@ class CakeTime {
* If $dateString's year is the current year, the returned string does not * If $dateString's year is the current year, the returned string does not
* include mention of the year. * include mention of the year.
* *
* @param int|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object * @param int|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object
* @param string|DateTimeZone $timezone Timezone string or DateTimeZone object * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object
* @return string Described, relative date string * @return string Described, relative date string
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::niceShort * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::niceShort
*/ */
public static function niceShort($dateString = null, $timezone = null) { public static function niceShort($date = null, $timezone = null) {
if (!$dateString) { if (!$date) {
$dateString = time(); $date = time();
} }
$date = static::fromString($dateString, $timezone); $timestamp = static::fromString($date, $timezone);
if (static::isToday($dateString, $timezone)) { if (static::isToday($date, $timezone)) {
return __d('cake', 'Today, %s', static::_strftime("%H:%M", $date)); $formattedDate = static::__strftime("%H:%M", $timestamp, $date, $timezone);
return __d('cake', 'Today, %s', $formattedDate);
} }
if (static::wasYesterday($dateString, $timezone)) { if (static::wasYesterday($date, $timezone)) {
return __d('cake', 'Yesterday, %s', static::_strftime("%H:%M", $date)); $formattedDate = static::__strftime("%H:%M", $timestamp, $date, $timezone);
return __d('cake', 'Yesterday, %s', $formattedDate);
} }
if (static::isTomorrow($dateString, $timezone)) { if (static::isTomorrow($date, $timezone)) {
return __d('cake', 'Tomorrow, %s', static::_strftime("%H:%M", $date)); $formattedDate = static::__strftime("%H:%M", $timestamp, $date, $timezone);
return __d('cake', 'Tomorrow, %s', $formattedDate);
} }
$d = static::_strftime("%w", $date); $d = static::__strftime("%w", $timestamp, $date, $timezone);
$day = array( $day = array(
__d('cake', 'Sunday'), __d('cake', 'Sunday'),
__d('cake', 'Monday'), __d('cake', 'Monday'),
@ -413,18 +416,21 @@ class CakeTime {
__d('cake', 'Friday'), __d('cake', 'Friday'),
__d('cake', 'Saturday') __d('cake', 'Saturday')
); );
if (static::wasWithinLast('7 days', $dateString, $timezone)) { if (static::wasWithinLast('7 days', $date, $timezone)) {
return sprintf('%s %s', $day[$d], static::_strftime(static::$niceShortFormat, $date)); $formattedDate = static::__strftime(static::$niceShortFormat, $timestamp, $date, $timezone);
return sprintf('%s %s', $day[$d], $formattedDate);
} }
if (static::isWithinNext('7 days', $dateString, $timezone)) { if (static::isWithinNext('7 days', $date, $timezone)) {
return __d('cake', 'On %s %s', $day[$d], static::_strftime(static::$niceShortFormat, $date)); $formattedDate = static::__strftime(static::$niceShortFormat, $timestamp, $date, $timezone);
return __d('cake', 'On %s %s', $day[$d], $formattedDate);
} }
$y = ''; $y = '';
if (!static::isThisYear($date)) { if (!static::isThisYear($timestamp)) {
$y = ' %Y'; $y = ' %Y';
} }
return static::_strftime(static::convertSpecifiers("%b %eS{$y}, %H:%M", $date), $date); $format = static::convertSpecifiers("%b %eS{$y}, %H:%M", $timestamp);
return static::__strftime($format, $timestamp, $date, $timezone);
} }
/** /**
@ -1065,17 +1071,8 @@ class CakeTime {
if (empty($format)) { if (empty($format)) {
$format = '%x'; $format = '%x';
} }
$serverTimeZone = date_default_timezone_get(); $convertedFormat = static::convertSpecifiers($format, $timestamp);
if ( return static::__strftime($convertedFormat, $timestamp, $date, $timezone);
!empty($timezone) &&
$date instanceof DateTime &&
$date->getTimezone()->getName() != $serverTimeZone
) {
date_default_timezone_set($timezone);
}
$result = static::_strftime(static::convertSpecifiers($format, $timestamp), $timestamp);
date_default_timezone_set($serverTimeZone);
return $result;
} }
/** /**
@ -1166,13 +1163,12 @@ class CakeTime {
* Handles utf8_encoding the result of strftime when necessary. * Handles utf8_encoding the result of strftime when necessary.
* *
* @param string $format Format string. * @param string $format Format string.
* @param int $date Timestamp to format. * @param int $timestamp Timestamp to format.
* @return string formatted string with correct encoding. * @return string formatted string with correct encoding.
*/ */
protected static function _strftime($format, $date) { protected static function _strftime($format, $timestamp) {
$format = strftime($format, $date); $format = strftime($format, $timestamp);
$encoding = Configure::read('App.encoding'); $encoding = Configure::read('App.encoding');
if (!empty($encoding) && $encoding === 'UTF-8') { if (!empty($encoding) && $encoding === 'UTF-8') {
if (function_exists('mb_check_encoding')) { if (function_exists('mb_check_encoding')) {
$valid = mb_check_encoding($format, $encoding); $valid = mb_check_encoding($format, $encoding);
@ -1186,4 +1182,29 @@ class CakeTime {
return $format; return $format;
} }
/**
* Multibyte wrapper for strftime.
*
* Adjusts the timezone when necessary before formatting the time.
*
* @param string $format Format string.
* @param int $timestamp Timestamp to format.
* @param int|string|DateTime $date Timestamp, strtotime() valid string or DateTime object.
* @param string|DateTimeZone $timezone Timezone string or DateTimeZone object.
* @return string Formatted date string with correct encoding.
*/
private static function __strftime($format, $timestamp, $date, $timezone) {
$serverTimeZone = date_default_timezone_get();
if (
!empty($timezone) &&
$date instanceof DateTime &&
$date->getTimezone()->getName() != $serverTimeZone
) {
date_default_timezone_set($timezone);
}
$result = static::_strftime($format, $timestamp);
date_default_timezone_set($serverTimeZone);
return $result;
}
} }