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

fixed an issue with datetimeimmutable comparaison #180

Merged
merged 2 commits into from
Jan 6, 2020
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
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
/CONTRIBUTING.md export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon export-ignore
/benchmark export-ignore
/performance export-ignore
/generator export-ignore
11 changes: 11 additions & 0 deletions generator/tests/DateTimeImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,15 @@ public function testSerialize()
$this->assertEquals($safeDatetime->getTimestamp(), $newDatetime->getTimestamp());
$this->assertEquals($safeDatetime->getTimezone(), $newDatetime->getTimezone());
}

public function testComparaison(): void
{
$safeDateTime = new \Safe\DateTimeImmutable();
$phpDateTime = new \DateTimeImmutable();
$timeLimit = \DateInterval::createFromDateString('2 hours');

$a = $safeDateTime->modify('+3 hours') < $safeDateTime->add($timeLimit);
$b = $phpDateTime->modify('+3 hours') < $phpDateTime->add($timeLimit);
$this->assertEquals($b, $a);
}
}
4 changes: 2 additions & 2 deletions lib/DateTimeImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class DateTimeImmutable extends \DateTimeImmutable
*/
public function __construct($time = 'now', $timezone = null)
{
parent::__construct();
parent::__construct($time, $timezone);
$this->innerDateTime = new parent($time, $timezone);
}

//switch from regular datetime to safe version
private static function createFromRegular(\DateTimeImmutable $datetime): self
{
$safeDatetime = new self();
$safeDatetime = new self($datetime->format('Y-m-d H:i:s'), $datetime->getTimezone()); //we need to also update the wrapper to not break the operators '<' and '>'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about microseconds?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn i forgot about theses

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So milliseconds can't be used with < 7.4 because ::createFromFormat doesn't accept it, microseconds should be used.

$safeDatetime->innerDateTime = $datetime;
return $safeDatetime;
}
Expand Down