Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/Api/DateTimeResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Aws\Api;

use Aws\Api\Parser\Exception\ParserException;
use DateTime;
use DateTimeZone;
use Exception;

/**
Expand All @@ -16,36 +18,50 @@ class DateTimeResult extends \DateTime implements \JsonSerializable
* The Unix epoch (or Unix time or POSIX time or Unix
* timestamp) is the number of seconds that have elapsed since
* January 1, 1970 (midnight UTC/GMT).
* @param $unixTimestamp
*
* @return DateTimeResult
* @throws Exception
*/
public static function fromEpoch($unixTimestamp)
{
return new self(gmdate('c', $unixTimestamp));
if (!is_numeric($unixTimestamp)) {
throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromEpoch');
}

// PHP 5.5 does not support sub-second precision
if (\PHP_VERSION_ID < 56000) {
return new self(gmdate('c', $unixTimestamp));
}

$dateTime = DateTime::createFromFormat('U.u', sprintf('%0.6f', $unixTimestamp), new DateTimeZone('UTC'));

if (false === $dateTime) {
throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromEpoch');
}

return new self(
$dateTime->format('Y-m-d H:i:s.u'),
new DateTimeZone('UTC')
);
}

/**
* @param $iso8601Timestamp
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should rather be @param string $iso8601Timestamp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems to accept non-string params too, otherwise the is_string check would be dead code. adding propper type doc is out of scope here. I've just removed some invalid syntax.

*
* @return DateTimeResult
*/
public static function fromISO8601($iso8601Timestamp)
{
if (is_numeric($iso8601Timestamp) || !is_string($iso8601Timestamp)) {
throw new ParserException('Invalid timestamp value passed to DateTimeResult::fromISO8601');
}

return new DateTimeResult($iso8601Timestamp);
}

/**
* Create a new DateTimeResult from an unknown timestamp.
*
* @param $timestamp
*
* @return DateTimeResult
* @throws ParserException|Exception
* @throws Exception
*/
public static function fromTimestamp($timestamp, $expectedFormat = null)
{
Expand Down Expand Up @@ -92,7 +108,7 @@ public function __toString()
/**
* Serialize the date as an ISO 8601 date when serializing as JSON.
*
* @return mixed|string
* @return string
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
Expand Down
10 changes: 9 additions & 1 deletion tests/Api/DateTimeResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ public function testCreatesFromEpoch()
{
$t = time();
$d = DateTimeResult::fromEpoch($t);
$this->assertSame((string)$t, $d->format('U'));
$this->assertSame((string) $t, $d->format('U'));
}

public function testCreatesFromEpochFloat()
{
$t = 16344171.123432;
$d = DateTimeResult::fromEpoch($t);
$this->assertSame('16344171', $d->format('U'));
$this->assertSame(\PHP_VERSION_ID < 56000 ? '16344171.000000' : '16344171.123432', $d->format('U.u'));
}

public function testCastToIso8601String()
Expand Down