Skip to content

Commit 02c6312

Browse files
committed
fix: Convert to timezone with own utility function
1 parent 75c6020 commit 02c6312

2 files changed

Lines changed: 5 additions & 7 deletions

File tree

src/TypeGuard.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,14 @@ public function asDateTimeImmutable(mixed $value, DateTimeZone|string|null $time
124124
return null;
125125
}
126126

127-
$tz = $timeZone instanceof DateTimeZone
128-
? $timeZone
129-
: ($timeZone !== null ? new DateTimeZone($timeZone) : $this->timeZone());
127+
$timeZone = $this->asDateTimeZone($timeZone) ?? $this->timeZone();
130128

131129
if ($value instanceof DateTimeImmutable) {
132-
if ($value->getTimezone()->getName() === $tz->getName()) {
130+
if ($value->getTimezone()->getName() === $timeZone->getName()) {
133131
return $value;
134132
}
135133

136-
return $value->setTimezone($tz);
134+
return $value->setTimezone($timeZone);
137135
}
138136

139137
if ($value instanceof Stringable) {
@@ -144,7 +142,7 @@ public function asDateTimeImmutable(mixed $value, DateTimeZone|string|null $time
144142
throw NotConvertable::toDateTime($value);
145143
}
146144

147-
return new DateTimeImmutable(asString($value), $tz);
145+
return new DateTimeImmutable(asString($value), $timeZone);
148146
}
149147

150148
/** @return ($value is null ? null : DateTimeZone) */

tests/AsDateTimeImmutableTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testNullTimezoneParameterUsesDefault(): void
145145
{
146146
TypeGuard::instance()->timeZone('Australia/Adelaide');
147147

148-
$result = asDateTimeImmutable('2010-09-08 07:06:05', null);
148+
$result = asDateTimeImmutable('2010-09-08 07:06:05');
149149

150150
self::assertInstanceOf(DateTimeImmutable::class, $result);
151151
self::assertSame('2010-09-08T07:06:05+09:30', $result->format('c'));

0 commit comments

Comments
 (0)