Browse Source

Fixed #12822: Fixed datetime creation from microseconds, added tests to \i18n\Formatter::asTimestamp

tags/2.0.11
Anton Kozlov 8 years ago committed by SilverFire - Dmitry Naumenko
parent
commit
20a411d8ab
  1. 2
      framework/i18n/Formatter.php
  2. 19
      tests/framework/i18n/FormatterTest.php

2
framework/i18n/Formatter.php

@ -672,7 +672,7 @@ class Formatter extends Component
}
try {
if (is_numeric($value)) { // process as unix timestamp, which is always in UTC
$timestamp = new DateTime('@' . $value, new DateTimeZone('UTC'));
$timestamp = new DateTime('@' . (int)$value, new DateTimeZone('UTC'));
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;

19
tests/framework/i18n/FormatterTest.php

@ -191,4 +191,23 @@ class FormatterTest extends TestCase
// null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asBoolean(null));
}
public function testAsTimestamp()
{
$this->assertSame('1451606400', $this->formatter->asTimestamp(1451606400));
$this->assertSame('1451606400', $this->formatter->asTimestamp(1451606400.1234));
$this->assertSame('1451606400', $this->formatter->asTimestamp(1451606400.0000));
$this->assertSame('1451606400', $this->formatter->asTimestamp('1451606400'));
$this->assertSame('1451606400', $this->formatter->asTimestamp('1451606400.1234'));
$this->assertSame('1451606400', $this->formatter->asTimestamp('1451606400.0000'));
$this->assertSame('1451606400', $this->formatter->asTimestamp('2016-01-01 00:00:00'));
$dateTime = new \DateTime('2016-01-01 00:00:00.000');
$this->assertSame('1451606400', $this->formatter->asTimestamp($dateTime));
$dateTime = new \DateTime('2016-01-01 00:00:00.000', new \DateTimeZone('Europe/Berlin'));
$this->assertSame('1451602800', $this->formatter->asTimestamp($dateTime));
}
}

Loading…
Cancel
Save