Commit c2d814cc by Carsten Brandt

first draft of a fix for #5448

only method stubs so far
parent d44c23bb
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\i18n;
/**
* DateTimeExtended is an extended version of the [PHP DateTime class](http://php.net/manual/en/class.datetime.php).
*
* It provides more accurate handling of date-only values which can not be converted between timezone as
* they do not include any time information.
*
* **Important Note:** This implementation was created to be used internally of [[Formatter]], it may not behave
* as expected when used directly in your code. You should normally not need to use this class in your application code.
* Use the original PHP [DateTime](http://php.net/manual/en/class.datetime.php) class instead.
*
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class DateTimeExtended extends \DateTime
{
private $_isDateOnly = false;
/**
* The DateTimeExtended constructor.
*
* @param string $time
* @param \DateTimeZone $timezone
* @return DateTimeExtended
* @see http://php.net/manual/en/datetime.construct.php
*/
public function __construct ($time = 'now', \DateTimeZone $timezone = null)
{
// TODO get date info
$this->_isDateOnly = false;
parent::__construct($time, $timezone);
}
/**
* Parse a string into a new DateTime object according to the specified format
* @param string $format Format accepted by date().
* @param string $time String representing the time.
* @param \DateTimeZone $timezone A DateTimeZone object representing the desired time zone.
* @return DateTimeExtended
* @link http://php.net/manual/en/datetime.createfromformat.php
*/
public static function createFromFormat ($format, $time, \DateTimeZone $timezone=null)
{
$dateTime = parent::createFromFormat($format, $time, $timezone);
// TODO turn object into instance of $this
// TODO get date info
// $dateTime->_isDateOnly = false;
return $dateTime;
}
public function isDateOnly()
{
return $this->_isDateOnly;
}
}
\ No newline at end of file
......@@ -532,20 +532,27 @@ class Formatter extends Component
return $this->nullDisplay;
}
// avoid time zone conversion for date-only values
if ($type === 'date' && $timestamp->isDateOnly()) {
$timeZone = $this->defaultTimeZone; // TODO maybe just NULL?
} else {
$timeZone = $this->timeZone;
}
if ($this->_intlLoaded) {
if (strncmp($format, 'php:', 4) === 0) {
$format = FormatConverter::convertDatePhpToIcu(substr($format, 4));
}
if (isset($this->_dateFormats[$format])) {
if ($type === 'date') {
$formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], IntlDateFormatter::NONE, $this->timeZone);
$formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], IntlDateFormatter::NONE, $timeZone);
} elseif ($type === 'time') {
$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, $this->_dateFormats[$format], $this->timeZone);
$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, $this->_dateFormats[$format], $timeZone);
} else {
$formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], $this->_dateFormats[$format], $this->timeZone);
$formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], $this->_dateFormats[$format], $timeZone);
}
} else {
$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, $this->timeZone, null, $format);
$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, $timeZone, null, $format);
}
if ($formatter === null) {
throw new InvalidConfigException(intl_get_error_message());
......@@ -557,8 +564,8 @@ class Formatter extends Component
} else {
$format = FormatConverter::convertDateIcuToPhp($format, $type, $this->locale);
}
if ($this->timeZone != null) {
$timestamp->setTimezone(new DateTimeZone($this->timeZone));
if ($timeZone != null) {
$timestamp->setTimezone(new DateTimeZone($timeZone));
}
return $timestamp->format($format);
}
......@@ -575,7 +582,7 @@ class Formatter extends Component
* The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given.
* - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object
*
* @return DateTime the normalized datetime value
* @return DateTimeExtended the normalized datetime value
* @throws InvalidParamException if the input value can not be evaluated as a date value.
*/
protected function normalizeDatetimeValue($value)
......@@ -589,17 +596,17 @@ class Formatter extends Component
}
try {
if (is_numeric($value)) { // process as unix timestamp, which is always in UTC
if (($timestamp = DateTime::createFromFormat('U', $value, new DateTimeZone('UTC'))) === false) {
if (($timestamp = DateTimeExtended::createFromFormat('U', $value, new DateTimeZone('UTC'))) === false) {
throw new InvalidParamException("Failed to parse '$value' as a UNIX timestamp.");
}
return $timestamp;
} elseif (($timestamp = DateTime::createFromFormat('Y-m-d', $value, new DateTimeZone($this->defaultTimeZone))) !== false) { // try Y-m-d format (support invalid dates like 2012-13-01)
} elseif (($timestamp = DateTimeExtended::createFromFormat('Y-m-d', $value, new DateTimeZone($this->defaultTimeZone))) !== false) { // try Y-m-d format (support invalid dates like 2012-13-01)
return $timestamp;
} elseif (($timestamp = DateTime::createFromFormat('Y-m-d H:i:s', $value, new DateTimeZone($this->defaultTimeZone))) !== false) { // try Y-m-d H:i:s format (support invalid dates like 2012-13-01 12:63:12)
} elseif (($timestamp = DateTimeExtended::createFromFormat('Y-m-d H:i:s', $value, new DateTimeZone($this->defaultTimeZone))) !== false) { // try Y-m-d H:i:s format (support invalid dates like 2012-13-01 12:63:12)
return $timestamp;
}
// finally try to create a DateTime object with the value
$timestamp = new DateTime($value, new DateTimeZone($this->defaultTimeZone));
$timestamp = new DateTimeExtended($value, new DateTimeZone($this->defaultTimeZone));
return $timestamp;
} catch(\Exception $e) {
throw new InvalidParamException("'$value' is not a valid date time value: " . $e->getMessage()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment