Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparing 2 high resolution points in time when more than a second has elapsed #17

Merged
merged 2 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Fixed

- When comparing 2 high resolution points in time, if the elapsed time between the 2 is higher than a second it could return that the start point is ahead of the end one.

## 4.0.1 - 2025-01-19

### Fixed
Expand Down
40 changes: 40 additions & 0 deletions proofs/pointInTime/highResolution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types = 1);

use Innmind\TimeContinuum\PointInTime\HighResolution;
use Innmind\BlackBox\Set;

return static function() {
yield proof(
'HighResolution::aheadOf() on different seconds',
given(
Set\Integers::above(0),
Set\Integers::above(0),
Set\Integers::between(0, 999_999_999),
Set\Integers::between(0, 999_999_999),
)->filter(static fn($start, $end) => $start < $end),
static function($assert, $start, $end, $startNanoseconds, $endNanoseconds) {
$start = HighResolution::of($start, $startNanoseconds);
$end = HighResolution::of($end, $endNanoseconds);

$assert->true($end->aheadOf($start));
$assert->false($start->aheadOf($end));
},
);

yield proof(
'HighResolution::aheadOf() in same second',
given(
Set\Integers::above(0),
Set\Integers::between(0, 999_999_999),
Set\Integers::between(0, 999_999_999),
)->filter(static fn($_, $start, $end) => $start < $end),
static function($assert, $second, $start, $end) {
$start = HighResolution::of($second, $start);
$end = HighResolution::of($second, $end);

$assert->true($end->aheadOf($start));
$assert->false($start->aheadOf($end));
},
);
};
4 changes: 4 additions & 0 deletions src/PointInTime/HighResolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public function aheadOf(self $other): bool
return true;
}

if ($this->seconds < $other->seconds) {
return false;
}

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

Expand Down