forked from aws/aws-sdk-php
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now converting timestamps to DateTimeResult objects
- Loading branch information
Showing
7 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
namespace Aws\Api; | ||
|
||
/** | ||
* DateTime overrides that make DateTime work more seamlessly as a string, | ||
* with JSON documents, and with JMESPath. | ||
*/ | ||
class DateTimeResult extends \DateTime implements \JsonSerializable | ||
{ | ||
/** | ||
* Create a new DateTimeResult from a unix timestamp. | ||
* | ||
* @param $unixTimestamp | ||
* | ||
* @return DateTimeResult | ||
*/ | ||
public static function fromEpoch($unixTimestamp) | ||
{ | ||
return new self(gmdate('c', $unixTimestamp)); | ||
} | ||
|
||
/** | ||
* Serialize the DateTimeResult as an ISO 8601 date string. | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->format('c'); | ||
} | ||
|
||
/** | ||
* Serialize the date as an ISO 8601 date when serializing as JSON. | ||
* | ||
* @return mixed|string | ||
*/ | ||
public function jsonSerialize() | ||
{ | ||
return (string) $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
namespace Aws\Test\Api; | ||
|
||
use Aws\Api\DateTimeResult; | ||
|
||
/** | ||
* @covers \Aws\Api\DateTimeResult | ||
*/ | ||
class DateTimeResultTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testCreatesFromEpoch() | ||
{ | ||
$t = time(); | ||
$d = DateTimeResult::fromEpoch($t); | ||
$this->assertEquals($t, $d->format('U')); | ||
} | ||
|
||
public function testCastToIso8601String() | ||
{ | ||
$t = time(); | ||
$d = DateTimeResult::fromEpoch($t); | ||
$this->assertSame(gmdate('c', $t), (string) $d); | ||
} | ||
|
||
public function testJsonSerialzesAsIso8601() | ||
{ | ||
$t = time(); | ||
$d = DateTimeResult::fromEpoch($t); | ||
$this->assertSame('"' . gmdate('c', $t). '"', json_encode($d)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters