Skip to content

Commit

Permalink
Merge pull request #183 from kunicmarko20/patch-1
Browse files Browse the repository at this point in the history
Include microseconds in Date objects
  • Loading branch information
Kharhamel authored Jan 9, 2020
2 parents 9acc5ae + 3e65b69 commit eb13cdb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions generator/tests/DateTimeImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,16 @@ public function testComparaison(): void
$b = $phpDateTime->modify('+3 hours') < $phpDateTime->add($timeLimit);
$this->assertEquals($b, $a);
}

public function testEquals(): void
{
$phpDateTime = new \DateTimeImmutable();

$safeDateTime1 = \Safe\DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $phpDateTime->format('Y-m-d H:i:s.u'));
$safeDateTime2 = new \Safe\DateTimeImmutable($safeDateTime1->format('Y-m-d H:i:s.u'));

$this->assertEquals($phpDateTime, $safeDateTime1);
$this->assertEquals($phpDateTime, $safeDateTime2);
$this->assertEquals($safeDateTime1, $safeDateTime2);
}
}
2 changes: 1 addition & 1 deletion lib/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DateTime extends \DateTime
//switch from regular datetime to safe version
private static function createFromRegular(\DateTime $datetime): self
{
return new self($datetime->format('Y-m-d H:i:s'), $datetime->getTimezone());
return new self($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/DateTimeImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct($time = 'now', $timezone = null)
//switch from regular datetime to safe version
private static function createFromRegular(\DateTimeImmutable $datetime): 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 '>'
$safeDatetime = new self($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone()); //we need to also update the wrapper to not break the operators '<' and '>'
$safeDatetime->innerDateTime = $datetime;
return $safeDatetime;
}
Expand Down

0 comments on commit eb13cdb

Please sign in to comment.