Skip to content

Commit

Permalink
feature: implement getTimeStamp
Browse files Browse the repository at this point in the history
closes #61
  • Loading branch information
g105b committed Oct 8, 2023
1 parent 4bc609e commit e48efdc
Show file tree
Hide file tree
Showing 2 changed files with 18 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 @@ -89,4 +90,10 @@ public function getTimestampString():string {
public function getRandomString():string {
return $this->randomString;
}

public function getDateTime():DateTime {
$timestamp = $this->getTimestamp() / 1000;
return new DateTime("@" . $timestamp);
}

}
11 changes: 11 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 @@ -97,4 +98,14 @@ public function testConstruct_existingUlid():void {
$sut = new Ulid(init: $existingString);
self::assertSame($existingString, (string)$sut);
}

public function testGetDateTime():void {
$now = new DateTime();
$sut = new Ulid();

self::assertSame(
$now->getTimestamp(),
$sut->getDateTime()->getTimestamp()
);
}
}

0 comments on commit e48efdc

Please sign in to comment.