Skip to content

Commit

Permalink
Fixing bug in the Date::parseDate() method
Browse files Browse the repository at this point in the history
When using a different timezone it would return different dates
because of the timezone changes.
  • Loading branch information
lorenzo committed Feb 16, 2016
1 parent 04c5cbf commit 0aaf232
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
16 changes: 0 additions & 16 deletions src/Database/Type/DateType.php
Expand Up @@ -99,20 +99,4 @@ protected function _parseValue($value)
$class = $this->_className;
return $class::parseDate($value, $this->_localeFormat);
}

/**
* Test that toImmutable changes all the methods to create frozen time instances.
*
* @return void
*/
public function testToImmutableAndToMutable()
{
$this->type->useImmutable();
$this->assertInstanceOf('DateTimeImmutable', $this->type->marshal('2015-11-01'));
$this->assertInstanceOf('DateTimeImmutable', $this->type->toPhp('2015-11-01', $this->driver));

$this->type->useMutable();
$this->assertInstanceOf('DateTime', $this->type->marshal('2015-11-01'));
$this->assertInstanceOf('DateTime', $this->type->toPhp('2015-11-01', $this->driver));
}
}
20 changes: 18 additions & 2 deletions src/I18n/DateFormatTrait.php
Expand Up @@ -14,6 +14,8 @@
*/
namespace Cake\I18n;

use Cake\Chronos\Date as ChronosDate;
use Cake\Chronos\MutableDate;
use IntlDateFormatter;

/**
Expand Down Expand Up @@ -54,6 +56,13 @@ trait DateFormatTrait
*/
protected static $_jsonEncodeFormat = "yyyy-MM-dd'T'HH:mm:ssZ";

/**
* Caches whehter or not this class is a subclass of a Date or MutableDate
*
* @var boolean
*/
protected static $_isDateInstance;

/**
* Returns a nicely formatted date string for this object.
*
Expand Down Expand Up @@ -263,18 +272,25 @@ public static function parseDateTime($time, $format = null)
$dateFormat = null;
}

if (static::$_isDateInstance === null) {
static::$_isDateInstance =
is_subclass_of(static::class, ChronosDate::class) ||
is_subclass_of(static::class, MutableDate::class);
}

$defaultTimezone = static::$_isDateInstance ? 'UTC' : date_default_timezone_get();
$formatter = datefmt_create(
static::$defaultLocale,
$dateFormat,
$timeFormat,
date_default_timezone_get(),
$defaultTimezone,
null,
$pattern
);
$time = $formatter->parse($time);
if ($time !== false) {
$result = new static('@' . $time);
return $result->setTimezone(date_default_timezone_get());
return static::$_isDateInstance ? $result : $result->setTimezone($defaultTimezone);
}
return null;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/I18n/DateTest.php
Expand Up @@ -52,6 +52,7 @@ public function tearDown()
parent::tearDown();
Date::$defaultLocale = $this->locale;
FrozenDate::$defaultLocale = $this->locale;
date_default_timezone_set('UTC');
}

/**
Expand Down Expand Up @@ -489,4 +490,18 @@ public function testDateAgoInWordsNegativeValues($class)
$result = $date->timeAgoInWords(['accuracy' => 'day']);
$this->assertEquals('today', $result);
}

/**
* Tests that parsing a date in a timezone other than UTC
* will not alter the date
*
* @dataProvider classNameProvider
* @return void
*/
public function testParseDateDifferentTimezone($class)
{
date_default_timezone_set('Europe/Paris');
$result = $class::parseDate('25-02-2016', 'd-M-y');
$this->assertEquals('25-02-2016', $result->format('d-m-Y'));
}
}

0 comments on commit 0aaf232

Please sign in to comment.