Skip to content

Commit

Permalink
Merge pull request #15 from Innmind/fix-pit-comparisons
Browse files Browse the repository at this point in the history
Fix `PointInTime::equals()` and `::aheadOf()`
  • Loading branch information
Baptouuuu authored Nov 28, 2024
2 parents 6c678f6 + 45ba4a6 commit 339a278
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 17 deletions.
29 changes: 29 additions & 0 deletions proofs/clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,33 @@ static function($assert, $point) {
);
},
);

yield proof(
'Each call to Clock::now() is ahead of the previous',
given(Set\Nullable::of(Set\Integers::between(1, 2_000_000))), // up to 2 seconds
static function($assert, $microsecond) {
$clock = Clock::live();
$start = $clock->now();

if (\is_int($microsecond)) {
\usleep($microsecond);
}

$assert->true(
$clock->now()->aheadOf(
$start,
),
);
},
);

yield test(
'Clock::now() is equal to iteself',
static function($assert) {
$clock = Clock::live();
$now = $clock->now();

$assert->true($now->equals($now));
},
);
};
65 changes: 65 additions & 0 deletions proofs/pointInTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
declare(strict_types = 1);

use Innmind\TimeContinuum\Period;
use Fixtures\Innmind\TimeContinuum as Fixtures;
use Innmind\BlackBox\Set;

return static function() {
yield proof(
'PointInTime::equals()',
given(
Fixtures\PointInTime::any(),
Set\Either::any(
Set\Integers::above(0)->map(Period::microsecond(...)),
Set\Integers::above(0)->map(Period::millisecond(...)),
),
),
static function($assert, $point, $period) {
$assert->true(
$point
->goForward($period)
->goBack($period)
->equals($point),
);
$assert->true(
$point
->goBack($period)
->goForward($period)
->equals($point),
);
$assert->false(
$point
->goBack($period)
->equals($point),
);
$assert->false(
$point
->goForward($period)
->equals($point),
);
},
);

yield proof(
'PointInTime::aheadOf()',
given(
Fixtures\PointInTime::any(),
Fixtures\Period::any()->filter(
static fn($period) => !$period->equals(Period::microsecond(0)),
),
),
static function($assert, $point, $period) {
$assert->true(
$point
->goForward($period)
->aheadOf($point),
);
$assert->false(
$point
->goBack($period)
->aheadOf($point),
);
},
);
};
24 changes: 7 additions & 17 deletions src/PointInTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,37 +213,27 @@ public function goForward(Period $period): self

public function equals(self $point): bool
{
[$self, $other] = self::compare($this, $point);
$format = Format::of('Y-m-dTH:i:s.u');
$self = $this->changeOffset(Offset::utc())->format($format);
$other = $point->changeOffset(Offset::utc())->format($format);

return $self === $other;
}

public function aheadOf(self $point): bool
{
[$self, $other] = self::compare($this, $point);
if (!\is_null($this->highResolution) && !\is_null($point->highResolution)) {
return $this->highResolution->aheadOf($point->highResolution);
}

return $self > $other;
return $this->date > $point->date;
}

public function toString(): string
{
return $this->date->format('Y-m-d\TH:i:s.uP');
}

/**
* @psalm-pure
* @return array{string, string}
*/
private static function compare(self $self, self $other): array
{
$format = Format::of('Y-m-dTH:i:s.u');

return [
$self->changeOffset(Offset::utc())->format($format),
$other->changeOffset(Offset::utc())->format($format),
];
}

/**
* @psalm-pure
*/
Expand Down
9 changes: 9 additions & 0 deletions src/PointInTime/HighResolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public static function of(int $seconds, int $nanoseconds): self
return new self($seconds, $nanoseconds);
}

public function aheadOf(self $other): bool
{
if ($this->seconds > $other->seconds) {
return true;
}

return $this->nanoseconds > $other->nanoseconds;
}

public function elapsedSince(self $other): ElapsedPeriod
{
$seconds = $this->seconds - $other->seconds;
Expand Down

0 comments on commit 339a278

Please sign in to comment.