Skip to content

Commit

Permalink
allow negative dates for date formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed May 18, 2015
1 parent e3cf8e2 commit 3c2bd76
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Yii Framework 2 Change Log
- Bug #7305: Logging of Exception objects resulted in failure of the logger i.e. no logs being written (cebe)
- Bug #7707: client-side `trim` validator now passes the trimmed value to subsequent validators (nkovacs)
- Bug #8322: `yii\behaviors\TimestampBehavior::touch()` now throws an exception if owner is new record (klimov-paul)
- Bug #8451: `yii\i18n\Formatter` did not allow negative unix timestamps as input for date formatting (cebe)
- Bug: Fixed string comparison in `BaseActiveRecord::unlink()` which may result in wrong comparison result for hash valued primary keys starting with `0e` (cebe)
- Enh #8070: `yii\console\controllers\MessageController` now sorts created messages, even if there is no new one, while saving to PHP file (klimov-paul)
- Enh #8286: `yii\console\controllers\MessageController` improved allowing extraction of nested translator calls (klimov-paul)
Expand Down
6 changes: 3 additions & 3 deletions framework/i18n/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,9 @@ protected function normalizeDatetimeValue($value, $checkTimeInfo = false)
}
try {
if (is_numeric($value)) { // process as unix timestamp, which is always in UTC
if (($timestamp = DateTime::createFromFormat('U', $value, new DateTimeZone('UTC'))) === false) {
throw new InvalidParamException("Failed to parse '$value' as a UNIX timestamp.");
}
$timestamp = new DateTime();
$timestamp->setTimezone(new DateTimeZone('UTC'));
$timestamp->setTimestamp($value);
return $checkTimeInfo ? [$timestamp, true] : $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)
return $checkTimeInfo ? [$timestamp, false] : $timestamp;
Expand Down
26 changes: 26 additions & 0 deletions tests/framework/i18n/FormatterDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,4 +597,30 @@ public function testInputFractionSeconds()
$this->assertEquals('2015-04-28 12:06:15+0200', $this->formatter->asDateTime($timeStamp, 'yyyy-MM-dd HH:mm:ssZ'));
}


public function testInputUnixTimestamp()
{
$this->formatter->defaultTimeZone = 'UTC';
$timeStamp = 1431907200;
$this->formatter->timeZone = 'UTC';
$this->assertEquals('2015-05-18 00:00:00+0000', $this->formatter->asDateTime($timeStamp, 'yyyy-MM-dd HH:mm:ssZ'));
$this->formatter->timeZone = 'Europe/Berlin';
$this->assertEquals('2015-05-18 02:00:00+0200', $this->formatter->asDateTime($timeStamp, 'yyyy-MM-dd HH:mm:ssZ'));

$this->formatter->defaultTimeZone = 'Europe/Berlin';
$timeStamp = 1431907200;
$this->formatter->timeZone = 'UTC';
$this->assertEquals('2015-05-18 00:00:00+0000', $this->formatter->asDateTime($timeStamp, 'yyyy-MM-dd HH:mm:ssZ'));
$this->formatter->timeZone = 'Europe/Berlin';
$this->assertEquals('2015-05-18 02:00:00+0200', $this->formatter->asDateTime($timeStamp, 'yyyy-MM-dd HH:mm:ssZ'));

$this->formatter->defaultTimeZone = 'UTC';
$timeStamp = -1431907200;
$this->formatter->timeZone = 'UTC';
$this->assertEquals('1924-08-17 00:00:00+0000', $this->formatter->asDateTime($timeStamp, 'yyyy-MM-dd HH:mm:ssZ'));
$this->formatter->timeZone = 'Europe/Berlin';
$this->assertEquals('1924-08-17 01:00:00+0100', $this->formatter->asDateTime($timeStamp, 'yyyy-MM-dd HH:mm:ssZ'));

}

}

0 comments on commit 3c2bd76

Please sign in to comment.