File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -239,6 +239,16 @@ public static function createFromMutable($dateTime): self
239239 return self ::createFromRegular ($ date );
240240 }
241241
242+ public static function createFromInterface (\DateTimeInterface $ object ): self
243+ {
244+ if ($ object instanceof \DateTime) {
245+ $ object = self ::createFromMutable ($ object );
246+ } elseif ($ object instanceof DateTimeImmutable) {
247+ $ object = $ object ->getInnerDateTime ();
248+ }
249+ return self ::createFromRegular ($ object );
250+ }
251+
242252 /**
243253 * @param mixed[] $array
244254 * @return DateTimeImmutable
Original file line number Diff line number Diff line change @@ -18,4 +18,24 @@ public function testCreateFromMutable(): void
1818
1919 self ::assertSame ($ safeDate ->format (\DateTimeInterface::ATOM ), $ safeImmutableDate ->format (\DateTimeInterface::ATOM ));
2020 }
21+
22+ /**
23+ * @dataProvider createFromInterfaces
24+ */
25+ public function testCreateFromInterface (\DateTimeInterface $ dateTime ): void
26+ {
27+ $ safeImmutableDate = \Safe \DateTimeImmutable::createFromInterface ($ dateTime );
28+
29+ self ::assertSame ($ dateTime ->format (\DATE_ATOM ), $ safeImmutableDate ->format (\DATE_ATOM ));
30+ }
31+
32+ public function createFromInterfaces (): array
33+ {
34+ return [
35+ [new \DateTime ('2022-11-29T14:17:34+00:00 ' )],
36+ [new \Safe \DateTime ('2022-11-29T14:17:34+00:00 ' )],
37+ [new \DateTimeImmutable ('2022-11-29T14:17:34+00:00 ' )],
38+ [new \Safe \DateTimeImmutable ('2022-11-29T14:17:34+00:00 ' )],
39+ ];
40+ }
2141}
You can’t perform that action at this time.
0 commit comments