This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
|
||
namespace Litipk\Jiffy; | ||
|
||
|
||
/** | ||
* Trait MongodbAdapter: Only to be used inside UniversalTimestamp. | ||
* @package Litipk\Jiffy | ||
*/ | ||
trait MongodbAdapter | ||
{ | ||
/** | ||
* @param \MongoDB\BSON\UTCDatetime $mongoDate | ||
* @return UniversalTimestamp | ||
*/ | ||
public static function fromMongodbUTCDateTime(\MongoDB\BSON\UTCDatetime $mongoDate) | ||
{ | ||
return UniversalTimestamp::fromMillisecondsTimestamp((int)$mongoDate->__toString()); | ||
} | ||
|
||
/** | ||
* @return \MongoDate | ||
*/ | ||
public function asMongodbUTCDateTime() | ||
{ | ||
return new \MongoDB\BSON\UTCDatetime($this->millis); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
|
||
use Litipk\Jiffy\UniversalTimestamp; | ||
|
||
|
||
class MongodbAdapterTests extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testFromMongoDate() | ||
{ | ||
if (!extension_loaded('mongodb')) return; | ||
|
||
$uTs_A = UniversalTimestamp::now(); | ||
$uTs_B = UniversalTimestamp::fromMongodbUTCDateTime( | ||
new \MongoDB\BSON\UTCDatetime($uTs_A->asMilliseconds()+1) | ||
); | ||
$uTs_C = UniversalTimestamp::fromMillisecondsTimestamp($uTs_A->asMilliseconds()+2); | ||
|
||
$this->assertGreaterThanOrEqual(4, 5); | ||
|
||
$this->assertGreaterThanOrEqual($uTs_A->asMilliseconds(), $uTs_B->asMilliseconds()); | ||
$this->assertGreaterThanOrEqual($uTs_B->asMilliseconds(), $uTs_C->asMilliseconds()); | ||
$this->assertGreaterThanOrEqual($uTs_A->asMilliseconds(), $uTs_C->asMilliseconds()); | ||
} | ||
|
||
public function testAsMongoDate() | ||
{ | ||
if (!extension_loaded('mongodb')) return; | ||
|
||
$ts1 = UniversalTimestamp::fromMongodbUTCDateTime( | ||
new \MongoDB\BSON\UTCDatetime(UniversalTimestamp::now()->asMilliseconds()) | ||
); | ||
$md1 = $ts1->asMongodbUTCDateTime(); | ||
|
||
$this->assertTrue($md1 instanceof \MongoDB\BSON\UTCDatetime); | ||
$this->assertEquals($ts1->asSeconds(), (int)floor(((int)$md1->__toString())/1000)); | ||
$this->assertEquals($ts1->asMilliseconds()%1000, ((int)$md1->__toString())%1000); | ||
} | ||
} |