Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c173d8

Browse files
[Clock] Add get/setMicroseconds()
1 parent fd53091 commit 7c173d8

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Symfony/Component/Clock/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add `DatePoint::get/setMicroseconds()`
8+
49
6.4
510
---
611

src/Symfony/Component/Clock/DatePoint.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,27 @@ public function getTimezone(): \DateTimeZone
117117
{
118118
return parent::getTimezone() ?: throw new \DateInvalidTimeZoneException('The DatePoint object has no timezone.');
119119
}
120+
121+
public function setMicroseconds(int $microseconds): static
122+
{
123+
if ($microseconds < 0 || $microseconds > 999999) {
124+
throw new \DateRangeError('DatePoint::setMicroseconds(): Argument #1 ($microseconds) must be between 0 and 999999, '.$microseconds.' given');
125+
}
126+
127+
if (\PHP_VERSION_ID < 80400) {
128+
return $this->setTime(...explode('.', $this->format('H.i.s.'.$microseconds)));
129+
}
130+
131+
return parent::setMicroseconds($microseconds);
132+
}
133+
134+
135+
public function getMicroseconds(): int
136+
{
137+
if (\PHP_VERSION_ID >= 80400) {
138+
return parent::getMicroseconds();
139+
}
140+
141+
return $this->format('u');
142+
}
120143
}

src/Symfony/Component/Clock/Tests/DatePointTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,20 @@ public function testModify()
5757
$this->expectExceptionMessage('Failed to parse time string (Bad Date)');
5858
$date->modify('Bad Date');
5959
}
60+
61+
public function testMicroseconds()
62+
{
63+
$date = new DatePoint('2010-01-28 15:00:00.123456');
64+
65+
$this->assertSame('2010-01-28 15:00:00.123456', $date->format('Y-m-d H:i:s.u'));
66+
67+
$date = $date->setMicroseconds(789);
68+
69+
$this->assertSame('2010-01-28 15:00:00.000789', $date->format('Y-m-d H:i:s.u'));
70+
$this->assertSame(789, $date->getMicroseconds());
71+
72+
$this->expectException(\DateRangeError::class);
73+
$this->expectExceptionMessage('DatePoint::setMicroseconds(): Argument #1 ($microseconds) must be between 0 and 999999, 1000000 given');
74+
$date->setMicroseconds(1000000);
75+
}
6076
}

0 commit comments

Comments
 (0)