@@ -94,6 +94,63 @@ public function testDoesNotTouchNull(): void
9494 self ::assertNull (asDateTimeImmutable (null ));
9595 }
9696
97+ public function testTimezoneParameterOverridesDefaultWithDateTimeZone (): void
98+ {
99+ $ result = asDateTimeImmutable ('2010-09-08 07:06:05 ' , new DateTimeZone ('Australia/Adelaide ' ));
100+
101+ self ::assertInstanceOf (DateTimeImmutable::class, $ result );
102+ self ::assertSame ('2010-09-08T07:06:05+09:30 ' , $ result ->format ('c ' ));
103+ }
104+
105+ public function testTimezoneParameterOverridesDefaultWithString (): void
106+ {
107+ $ result = asDateTimeImmutable ('2010-09-08 07:06:05 ' , 'Australia/Adelaide ' );
108+
109+ self ::assertInstanceOf (DateTimeImmutable::class, $ result );
110+ self ::assertSame ('2010-09-08T07:06:05+09:30 ' , $ result ->format ('c ' ));
111+ }
112+
113+ public function testTimezoneParameterOverridesChangedDefault (): void
114+ {
115+ TypeGuard::instance ()->timeZone ('Europe/Berlin ' );
116+
117+ $ result = asDateTimeImmutable ('2010-09-08 07:06:05 ' , 'Australia/Adelaide ' );
118+
119+ self ::assertInstanceOf (DateTimeImmutable::class, $ result );
120+ self ::assertSame ('2010-09-08T07:06:05+09:30 ' , $ result ->format ('c ' ));
121+ }
122+
123+ public function testTimezoneParameterConvertsSameDateTimeImmutable (): void
124+ {
125+ $ input = new DateTimeImmutable ('2010-09-08T07:06:05 ' , new DateTimeZone ('Australia/Adelaide ' ));
126+
127+ $ result = asDateTimeImmutable ($ input , 'Australia/Adelaide ' );
128+
129+ self ::assertInstanceOf (DateTimeImmutable::class, $ result );
130+ self ::assertSame ('Australia/Adelaide ' , $ result ->getTimezone ()->getName ());
131+ self ::assertSame ('2010-09-08T07:06:05+09:30 ' , $ result ->format ('c ' ));
132+ }
133+
134+ public function testTimezoneParameterConvertsDifferentDateTimeImmutable (): void
135+ {
136+ $ input = new DateTimeImmutable ('2010-09-08T07:06:05+00:00 ' );
137+
138+ $ result = asDateTimeImmutable ($ input , new DateTimeZone ('Australia/Adelaide ' ));
139+
140+ self ::assertInstanceOf (DateTimeImmutable::class, $ result );
141+ self ::assertSame ('2010-09-08T16:36:05+09:30 ' , $ result ->format ('c ' ));
142+ }
143+
144+ public function testNullTimezoneParameterUsesDefault (): void
145+ {
146+ TypeGuard::instance ()->timeZone ('Australia/Adelaide ' );
147+
148+ $ result = asDateTimeImmutable ('2010-09-08 07:06:05 ' , null );
149+
150+ self ::assertInstanceOf (DateTimeImmutable::class, $ result );
151+ self ::assertSame ('2010-09-08T07:06:05+09:30 ' , $ result ->format ('c ' ));
152+ }
153+
97154 public function testOnlyScalarsAreConvertable (): void
98155 {
99156 $ this ->expectException (NotConvertable::class);
0 commit comments