Skip to content

Commit 8890b13

Browse files
committed
Cache DateTimeZone instances in DateHandler
Avoid creating new DateTimeZone objects on every deserialization call for the same timezone string. Instance-level caching.
1 parent b6333e2 commit 8890b13

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/Handler/DateHandler.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ final class DateHandler implements SubscribingHandlerInterface
3737
*/
3838
private $xmlCData;
3939

40+
/**
41+
* @var array<string, \DateTimeZone>
42+
*/
43+
private $timezoneCache = [];
44+
4045
/**
4146
* {@inheritdoc}
4247
*/
@@ -250,7 +255,7 @@ public function deserializeDateIntervalFromJson(DeserializationVisitorInterface
250255
*/
251256
private function parseDateTime($data, array $type, bool $immutable = false): \DateTimeInterface
252257
{
253-
$timezone = !empty($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone;
258+
$timezone = !empty($type['params'][1]) ? $this->resolveTimezone($type['params'][1]) : $this->defaultTimezone;
254259
$formats = $this->getDeserializationFormats($type);
255260

256261
$formatTried = [];
@@ -298,6 +303,15 @@ private function parseDateInterval(string $data): \DateInterval
298303
return $dateInterval;
299304
}
300305

306+
private function resolveTimezone(string $timezone): \DateTimeZone
307+
{
308+
if (!isset($this->timezoneCache[$timezone])) {
309+
$this->timezoneCache[$timezone] = new \DateTimeZone($timezone);
310+
}
311+
312+
return $this->timezoneCache[$timezone];
313+
}
314+
301315
/**
302316
* @param TypeArray $type
303317
*/

0 commit comments

Comments
 (0)