File tree Expand file tree Collapse file tree 3 files changed +44
-0
lines changed
src/Symfony/Component/Clock Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 1
1
CHANGELOG
2
2
=========
3
3
4
+ 7.1
5
+ ---
6
+
7
+ * Add ` DatePoint::get/setMicroseconds() `
8
+
4
9
6.4
5
10
---
6
11
Original file line number Diff line number Diff line change @@ -117,4 +117,27 @@ public function getTimezone(): \DateTimeZone
117
117
{
118
118
return parent ::getTimezone () ?: throw new \DateInvalidTimeZoneException ('The DatePoint object has no timezone. ' );
119
119
}
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
+ }
120
143
}
Original file line number Diff line number Diff line change @@ -57,4 +57,20 @@ public function testModify()
57
57
$ this ->expectExceptionMessage ('Failed to parse time string (Bad Date) ' );
58
58
$ date ->modify ('Bad Date ' );
59
59
}
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
+ }
60
76
}
You can’t perform that action at this time.
0 commit comments