Skip to content

Commit

Permalink
Merge pull request #59 from PhpGt/getDatetime
Browse files Browse the repository at this point in the history
feature: getDateTime
  • Loading branch information
g105b authored Sep 13, 2023
2 parents 7540b26 + 182c837 commit 55ce1f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Ulid.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Gt\Ulid;

use DateTime;
use Stringable;

class Ulid implements Stringable {
Expand Down Expand Up @@ -95,4 +96,10 @@ private function base32(int $number):string {

return strtoupper($converted);
}

public function getDateTime():DateTime {
$dateTime = new DateTime();
$dateTime->setTimestamp($this->timestamp);
return $dateTime;
}
}
8 changes: 8 additions & 0 deletions test/phpunit/UlidTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Gt\Ulid\Test;

use DateTime;
use Gt\Ulid\Ulid;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -85,4 +86,11 @@ public function testConstruct_prefix():void {
self::assertStringStartsWith("customer_", $sut);
self::assertGreaterThan(strlen("customer_") + 10, strlen($sut));
}

public function testGetDateTime():void {
$sut = new Ulid();
$dateTime = $sut->getDateTime();
$now = new DateTime();
self::assertSame($now->format("Y-m-d H:i:s"), $dateTime->format("Y-m-d H:i:s"));
}
}

0 comments on commit 55ce1f5

Please sign in to comment.