File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed
src/Symfony/Component/Clock Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 11CHANGELOG
22=========
33
4+ 7.1
5+ ---
6+
7+ * Add ` DatePoint::get/setMicroseconds() `
8+
496.4
510---
611
Original file line number Diff line number Diff line change @@ -117,4 +117,26 @@ 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+ public function getMicroseconds (): int
135+ {
136+ if (\PHP_VERSION_ID >= 80400 ) {
137+ return parent ::getMicroseconds ();
138+ }
139+
140+ return $ this ->format ('u ' );
141+ }
120142}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments