Skip to content

Commit

Permalink
fix: Fixes timezone formatting when inspecting KSUID.
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavofreze authored Jun 10, 2023
1 parent 10d6a5c commit 848e244
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $ksuid = Ksuid::inspectFrom(ksuid: '2QzPUGEaAKHhVcQYrqQodbiZat1');

print_r($ksuid); # Array
# (
# [time] => 2023-06-09 23:30:50 +0000 GMT+0000
# [time] => 2023-06-09 20:30:50 -0300 -03
# [payload] => 464932c1194da98e752145d72b8f0aab
# [timestamp] => 286353450
# )
Expand Down
31 changes: 17 additions & 14 deletions src/Internal/Timestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,54 @@
namespace TinyBlocks\Ksuid\Internal;

use DateTime;
use DateTimeZone;
use TinyBlocks\Encoder\Base62;

final class Timestamp
{
public const EPOCH = 1400000000;

private readonly int $time;

private function __construct(private readonly int $value, private readonly int $epoch)
private function __construct(private readonly int $value)
{
$this->time = $this->value - $this->epoch;
}

public static function from(int $value): Timestamp
{
return new Timestamp(value: $value, epoch: 0);
return new Timestamp(value: $value);
}

public static function fromBytes(string $value): Timestamp
{
$bytes = Base62::decode(value: $value);
$timestamp = substr($bytes, 0, -16);
$timestamp = substr($timestamp, -4);
$timestamp = (array)unpack("Nuint", $timestamp);
$timestamp = (array)unpack('Nuint', $timestamp);

return new Timestamp(value: $timestamp["uint"], epoch: 0);
return new Timestamp(value: $timestamp['uint']);
}

public static function fromAdjustedCurrentTime(): Timestamp
{
return new Timestamp(value: time(), epoch: self::EPOCH);
return new Timestamp(value: time() - self::EPOCH);
}

public static function format(int $timestamp): string
public function getValue(): int
{
return (new DateTime("@$timestamp"))->format('Y-m-d H:i:s O T');
return $this->value;
}

public function getValue(): int
public function getUnixTime(): int
{
return $this->time;
return $this->value + self::EPOCH;
}

public function getUnixTime(): int
public function toUnixTimeFormatted(): string
{
return $this->time + self::EPOCH;
$timezone = new DateTimeZone(timezone: date_default_timezone_get());

return (new DateTime())
->setTimezone(timezone: $timezone)
->setTimestamp(timestamp: $this->getUnixTime())
->format(format: 'Y-m-d H:i:s O T');
}
}
2 changes: 1 addition & 1 deletion src/Ksuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function inspectFrom(string $ksuid): array
$ksuid = self::fromPayload(value: $ksuid);

return [
'time' => Timestamp::format(timestamp: $ksuid->getUnixTime()),
'time' => $ksuid->timestamp->toUnixTimeFormatted(),
'payload' => $ksuid->getPayload(),
'timestamp' => $ksuid->getTimestamp()
];
Expand Down
43 changes: 43 additions & 0 deletions tests/Internal/TimestampTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace TinyBlocks\Ksuid\Internal;

use PHPUnit\Framework\TestCase;

class TimestampTest extends TestCase
{
/**
* @dataProvider providerForTestFormatWithDifferentTimezones
*/
public function testFormatWithDifferentTimezones(string $timezone, string $expected): void
{
$default = date_default_timezone_get();
date_default_timezone_set($timezone);

$timestamp = Timestamp::from(value: 107608047);

$actual = $timestamp->toUnixTimeFormatted();

self::assertEquals($expected, $actual);

date_default_timezone_set($default);
}

public function providerForTestFormatWithDifferentTimezones(): array
{
return [
[
'timezone' => 'America/Sao_Paulo',
'expected' => '2017-10-10 01:00:47 -0300 -03'
],
[
'timezone' => 'America/New_York',
'expected' => '2017-10-10 00:00:47 -0400 EDT'
],
[
'timezone' => 'Europe/London',
'expected' => '2017-10-10 05:00:47 +0100 BST'
]
];
}
}
6 changes: 4 additions & 2 deletions tests/KsuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use PHPUnit\Framework\TestCase;
use TinyBlocks\Ksuid\Internal\Exceptions\InvalidKsuidForInspection;
use TinyBlocks\Ksuid\Internal\Timestamp;

class KsuidTest extends TestCase
{
Expand Down Expand Up @@ -39,6 +40,7 @@ public function testFromTimestamp(): void

/** @Then a KSUID must be generated */
self::assertEquals($value, $ksuid->getTimestamp());
self::assertEquals($value + Timestamp::EPOCH, $ksuid->getUnixTime());
self::assertEquals(20, strlen($ksuid->getBytes()));
self::assertEquals(Ksuid::ENCODED_SIZE, strlen($ksuid->getValue()));
}
Expand Down Expand Up @@ -94,15 +96,15 @@ public function providerForTestInspectFrom(): array
[
'ksuid' => '2QzPUGEaAKHhVcQYrqQodbiZat1',
'expected' => [
'time' => '2023-06-09 23:30:50 +0000 GMT+0000',
'time' => '2023-06-09 20:30:50 -0300 -03',
'payload' => '464932c1194da98e752145d72b8f0aab',
'timestamp' => 286353450
]
],
[
'ksuid' => '0ujzPyRiIAffKhBux4PvQdDqMHY',
'expected' => [
'time' => '2017-10-10 04:46:20 +0000 GMT+0000',
'time' => '2017-10-10 01:46:20 -0300 -03',
'payload' => '73fc1aa3b2446246d6e89fcd909e8fe8',
'timestamp' => 107610780
]
Expand Down

0 comments on commit 848e244

Please sign in to comment.