Skip to content

Commit

Permalink
Merge pull request #62 from PhpGt/61-get-date-time
Browse files Browse the repository at this point in the history
Get DateTime
  • Loading branch information
g105b authored Oct 19, 2023
2 parents 2b66808 + a84bccb commit 8026eb5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Unique, lexicographically sortable identifiers.
===============================================

Create a `new Ulid()` anywhere in your application, and you have a stringable object that can be used as the primary key in a database. Ulid strings look something like `01G2J6MYN0PGC5Q21W9C`. They are cryptographically pseudo-random, and sort so that newer Ulids compare "greater than" older Ulids.
Create a `new Ulid()` anywhere in your application, and you have a stringable object that can be used as the primary key in a database. Ulid strings look something like `01G2J6MYN0PGC5Q21W9C` or can be prefixed with a type like `CUSTOMER_01G2J6MYN0PGC5Q21W9C`. They are cryptographically pseudo-random, and sort so that newer Ulids compare "greater than" older Ulids.

This solves the problems exposed with working with auto-incrementing integer primary keys, which are predictable and difficult to work with in distributed databases.

Expand Down Expand Up @@ -29,7 +29,7 @@ This solves the problems exposed with working with auto-incrementing integer pri
use Gt\Ulid\Ulid;

$exampleDataSource->create(new Person(
new Ulid(),
new Ulid("pet"),
name: "Cody",
age: 5,
));
Expand Down
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);
}

}
19 changes: 19 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,22 @@ 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()
);
}

public function testGetDateTime_constructorInit():void {
$dateTimeString = "1988-05-04 17:24";
$knownUlid = new Ulid(timestamp: strtotime($dateTimeString) * 1000);

$sut = new Ulid(init: $knownUlid);
self::assertSame($dateTimeString, $sut->getDateTime()->format("Y-m-d H:i"));
}
}

0 comments on commit 8026eb5

Please sign in to comment.