@@ -81,10 +81,10 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc
8181 if ($ time === '' && static ::$ testNow instanceof self) {
8282 if ($ timezone !== null ) {
8383 $ testNow = static ::$ testNow ->setTimezone ($ timezone );
84- $ time = $ testNow ->format ('Y-m-d H:i:s ' );
84+ $ time = $ testNow ->format ('Y-m-d H:i:s.u ' );
8585 } else {
8686 $ timezone = static ::$ testNow ->getTimezone ();
87- $ time = static ::$ testNow ->format ('Y-m-d H:i:s ' );
87+ $ time = static ::$ testNow ->format ('Y-m-d H:i:s.u ' );
8888 }
8989 }
9090
@@ -97,7 +97,7 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc
9797 if ($ time !== '' && static ::hasRelativeKeywords ($ time )) {
9898 $ instance = new DateTime ('now ' , $ this ->timezone );
9999 $ instance ->modify ($ time );
100- $ time = $ instance ->format ('Y-m-d H:i:s ' );
100+ $ time = $ instance ->format ('Y-m-d H:i:s.u ' );
101101 }
102102
103103 parent ::__construct ($ time , $ this ->timezone );
@@ -253,7 +253,7 @@ public static function createFromFormat($format, $datetime, $timezone = null)
253253 throw I18nException::forInvalidFormat ($ format );
254254 }
255255
256- return new self ($ date ->format ('Y-m-d H:i:s ' ), $ timezone );
256+ return new self ($ date ->format ('Y-m-d H:i:s.u ' ), $ timezone );
257257 }
258258
259259 /**
@@ -283,7 +283,7 @@ public static function createFromTimestamp(int $timestamp, $timezone = null, ?st
283283 */
284284 public static function createFromInstance (DateTimeInterface $ dateTime , ?string $ locale = null )
285285 {
286- $ date = $ dateTime ->format ('Y-m-d H:i:s ' );
286+ $ date = $ dateTime ->format ('Y-m-d H:i:s.u ' );
287287 $ timezone = $ dateTime ->getTimezone ();
288288
289289 return new self ($ date , $ timezone , $ locale );
@@ -314,10 +314,11 @@ public static function instance(DateTime $dateTime, ?string $locale = null)
314314 */
315315 public function toDateTime ()
316316 {
317- $ dateTime = new DateTime ('' , $ this ->getTimezone ());
318- $ dateTime ->setTimestamp (parent ::getTimestamp ());
319-
320- return $ dateTime ;
317+ return DateTime::createFromFormat (
318+ 'Y-m-d H:i:s.u ' ,
319+ $ this ->format ('Y-m-d H:i:s.u ' ),
320+ $ this ->getTimezone ()
321+ );
321322 }
322323
323324 // --------------------------------------------------------------------
@@ -348,7 +349,7 @@ public static function setTestNow($datetime = null, $timezone = null, ?string $l
348349 if (is_string ($ datetime )) {
349350 $ datetime = new self ($ datetime , $ timezone , $ locale );
350351 } elseif ($ datetime instanceof DateTimeInterface && ! $ datetime instanceof self) {
351- $ datetime = new self ($ datetime ->format ('Y-m-d H:i:s ' ), $ timezone );
352+ $ datetime = new self ($ datetime ->format ('Y-m-d H:i:s.u ' ), $ timezone );
352353 }
353354
354355 static ::$ testNow = $ datetime ;
@@ -941,9 +942,9 @@ public function equals($testTime, ?string $timezone = null): bool
941942
942943 $ ourTime = $ this ->toDateTime ()
943944 ->setTimezone (new DateTimeZone ('UTC ' ))
944- ->format ('Y-m-d H:i:s ' );
945+ ->format ('Y-m-d H:i:s.u ' );
945946
946- return $ testTime ->format ('Y-m-d H:i:s ' ) === $ ourTime ;
947+ return $ testTime ->format ('Y-m-d H:i:s.u ' ) === $ ourTime ;
947948 }
948949
949950 /**
@@ -956,15 +957,15 @@ public function equals($testTime, ?string $timezone = null): bool
956957 public function sameAs ($ testTime , ?string $ timezone = null ): bool
957958 {
958959 if ($ testTime instanceof DateTimeInterface) {
959- $ testTime = $ testTime ->format ('Y-m-d H:i:s ' );
960+ $ testTime = $ testTime ->format ('Y-m-d H:i:s.u O ' );
960961 } elseif (is_string ($ testTime )) {
961962 $ timezone = $ timezone ?: $ this ->timezone ;
962963 $ timezone = $ timezone instanceof DateTimeZone ? $ timezone : new DateTimeZone ($ timezone );
963964 $ testTime = new DateTime ($ testTime , $ timezone );
964- $ testTime = $ testTime ->format ('Y-m-d H:i:s ' );
965+ $ testTime = $ testTime ->format ('Y-m-d H:i:s.u O ' );
965966 }
966967
967- $ ourTime = $ this ->toDateTimeString ( );
968+ $ ourTime = $ this ->format ( ' Y-m-d H:i:s.u O ' );
968969
969970 return $ testTime === $ ourTime ;
970971 }
@@ -979,10 +980,16 @@ public function sameAs($testTime, ?string $timezone = null): bool
979980 */
980981 public function isBefore ($ testTime , ?string $ timezone = null ): bool
981982 {
982- $ testTime = $ this ->getUTCObject ($ testTime , $ timezone )->getTimestamp ();
983- $ ourTime = $ this ->getTimestamp ();
983+ $ testTime = $ this ->getUTCObject ($ testTime , $ timezone );
984+
985+ $ testTimestamp = $ testTime ->getTimestamp ();
986+ $ ourTimestamp = $ this ->getTimestamp ();
987+
988+ if ($ ourTimestamp === $ testTimestamp ) {
989+ return $ this ->format ('u ' ) < $ testTime ->format ('u ' );
990+ }
984991
985- return $ ourTime < $ testTime ;
992+ return $ ourTimestamp < $ testTimestamp ;
986993 }
987994
988995 /**
@@ -995,10 +1002,16 @@ public function isBefore($testTime, ?string $timezone = null): bool
9951002 */
9961003 public function isAfter ($ testTime , ?string $ timezone = null ): bool
9971004 {
998- $ testTime = $ this ->getUTCObject ($ testTime , $ timezone )->getTimestamp ();
999- $ ourTime = $ this ->getTimestamp ();
1005+ $ testTime = $ this ->getUTCObject ($ testTime , $ timezone );
1006+
1007+ $ testTimestamp = $ testTime ->getTimestamp ();
1008+ $ ourTimestamp = $ this ->getTimestamp ();
1009+
1010+ if ($ ourTimestamp === $ testTimestamp ) {
1011+ return $ this ->format ('u ' ) > $ testTime ->format ('u ' );
1012+ }
10001013
1001- return $ ourTime > $ testTime ;
1014+ return $ ourTimestamp > $ testTimestamp ;
10021015 }
10031016
10041017 // --------------------------------------------------------------------
0 commit comments