Skip to content

Commit 0957fca

Browse files
author
Stephan Wentz
committed
fix: Add possibility to override default timezone in asDateTimeImmutable()
1 parent 44d1cbd commit 0957fca

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

src/TypeGuard.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,22 @@ public function asString(mixed $value): string|null
118118
return (string) $value;
119119
}
120120

121-
/** @return DateTimeImmutable */
122-
public function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
121+
public function asDateTimeImmutable(mixed $value, DateTimeZone|string|null $timeZone = null): DateTimeImmutable|null
123122
{
124123
if ($value === null) {
125124
return null;
126125
}
127126

127+
$tz = $timeZone instanceof DateTimeZone
128+
? $timeZone
129+
: ($timeZone !== null ? new DateTimeZone($timeZone) : $this->timeZone());
130+
128131
if ($value instanceof DateTimeImmutable) {
129-
if ($value->getTimezone()->getName() === $this->timeZone()->getName()) {
132+
if ($value->getTimezone()->getName() === $tz->getName()) {
130133
return $value;
131134
}
132135

133-
return $value->setTimezone($this->timeZone());
136+
return $value->setTimezone($tz);
134137
}
135138

136139
if ($value instanceof Stringable) {
@@ -141,7 +144,7 @@ public function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
141144
throw NotConvertable::toDateTime($value);
142145
}
143146

144-
return new DateTimeImmutable(asString($value), $this->timeZone());
147+
return new DateTimeImmutable(asString($value), $tz);
145148
}
146149

147150
/** @return ($value is null ? null : string) */

src/functions.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Plook\TypeGuard;
66

77
use DateTimeImmutable;
8+
use DateTimeZone;
89

910
use function function_exists;
1011

@@ -49,9 +50,11 @@ function asString(mixed $value): string|null
4950
if (!function_exists('\Plook\TypeGuard\asDateTimeImmutable')) { // @codeCoverageIgnore
5051

5152
/** @return ($value is null ? null : DateTimeImmutable) */
52-
function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
53-
{
54-
return TypeGuard::instance()->asDateTimeImmutable($value);
53+
function asDateTimeImmutable(
54+
mixed $value,
55+
DateTimeZone|string|null $timeZone = null,
56+
): DateTimeImmutable|null {
57+
return TypeGuard::instance()->asDateTimeImmutable($value, $timeZone);
5558
}
5659
}
5760

tests/AsDateTimeImmutableTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)