Skip to content

Commit eb13cdb

Browse files
authored
Merge pull request #183 from kunicmarko20/patch-1
Include microseconds in Date objects
2 parents 9acc5ae + 3e65b69 commit eb13cdb

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

generator/tests/DateTimeImmutableTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,16 @@ public function testComparaison(): void
153153
$b = $phpDateTime->modify('+3 hours') < $phpDateTime->add($timeLimit);
154154
$this->assertEquals($b, $a);
155155
}
156+
157+
public function testEquals(): void
158+
{
159+
$phpDateTime = new \DateTimeImmutable();
160+
161+
$safeDateTime1 = \Safe\DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $phpDateTime->format('Y-m-d H:i:s.u'));
162+
$safeDateTime2 = new \Safe\DateTimeImmutable($safeDateTime1->format('Y-m-d H:i:s.u'));
163+
164+
$this->assertEquals($phpDateTime, $safeDateTime1);
165+
$this->assertEquals($phpDateTime, $safeDateTime2);
166+
$this->assertEquals($safeDateTime1, $safeDateTime2);
167+
}
156168
}

lib/DateTime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class DateTime extends \DateTime
1313
//switch from regular datetime to safe version
1414
private static function createFromRegular(\DateTime $datetime): self
1515
{
16-
return new self($datetime->format('Y-m-d H:i:s'), $datetime->getTimezone());
16+
return new self($datetime->format('Y-m-d H:i:s.u'), $datetime->getTimezone());
1717
}
1818

1919
/**

lib/DateTimeImmutable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct($time = 'now', $timezone = null)
3535
//switch from regular datetime to safe version
3636
private static function createFromRegular(\DateTimeImmutable $datetime): self
3737
{
38-
$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 '>'
38+
$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 '>'
3939
$safeDatetime->innerDateTime = $datetime;
4040
return $safeDatetime;
4141
}

0 commit comments

Comments
 (0)