@@ -369,7 +369,14 @@ public function formatNumberStyle(string $style, $number, array $attrs = [], str
369369 public function formatDateTime (Environment $ env , $ date , ?string $ dateFormat = 'medium ' , ?string $ timeFormat = 'medium ' , string $ pattern = '' , $ timezone = null , string $ calendar = 'gregorian ' , string $ locale = null ): string
370370 {
371371 $ date = twig_date_converter ($ env , $ date , $ timezone );
372- $ formatter = $ this ->createDateFormatter ($ locale , $ dateFormat , $ timeFormat , $ pattern , $ date ->getTimezone (), $ calendar );
372+
373+ $ formatterTimezone = $ timezone ;
374+ if (false === $ formatterTimezone ) {
375+ $ formatterTimezone = $ date ->getTimezone ();
376+ } elseif (\is_string ($ formatterTimezone )) {
377+ $ formatterTimezone = new \DateTimeZone ($ timezone );
378+ }
379+ $ formatter = $ this ->createDateFormatter ($ locale , $ dateFormat , $ timeFormat , $ pattern , $ formatterTimezone , $ calendar );
373380
374381 if (false === $ ret = $ formatter ->format ($ date )) {
375382 throw new RuntimeError ('Unable to format the given date. ' );
@@ -396,7 +403,7 @@ public function formatTime(Environment $env, $date, ?string $timeFormat = 'mediu
396403 return $ this ->formatDateTime ($ env , $ date , 'none ' , $ timeFormat , $ pattern , $ timezone , $ calendar , $ locale );
397404 }
398405
399- private function createDateFormatter (?string $ locale , ?string $ dateFormat , ?string $ timeFormat , string $ pattern , \DateTimeZone $ timezone , string $ calendar ): \IntlDateFormatter
406+ private function createDateFormatter (?string $ locale , ?string $ dateFormat , ?string $ timeFormat , string $ pattern , ? \DateTimeZone $ timezone , string $ calendar ): \IntlDateFormatter
400407 {
401408 $ dateFormats = self ::availableDateFormats ();
402409
@@ -409,7 +416,10 @@ private function createDateFormatter(?string $locale, ?string $dateFormat, ?stri
409416 }
410417
411418 if (null === $ locale ) {
412- $ locale = \Locale::getDefault ();
419+ if ($ this ->dateFormatterPrototype ) {
420+ $ locale = $ this ->dateFormatterPrototype ->getLocale ();
421+ }
422+ $ locale = $ locale ?: \Locale::getDefault ();
413423 }
414424
415425 $ calendar = 'gregorian ' === $ calendar ? \IntlDateFormatter::GREGORIAN : \IntlDateFormatter::TRADITIONAL ;
@@ -420,12 +430,14 @@ private function createDateFormatter(?string $locale, ?string $dateFormat, ?stri
420430 if ($ this ->dateFormatterPrototype ) {
421431 $ dateFormatValue = $ dateFormatValue ?: $ this ->dateFormatterPrototype ->getDateType ();
422432 $ timeFormatValue = $ timeFormatValue ?: $ this ->dateFormatterPrototype ->getTimeType ();
423- $ timezone = $ timezone ?: $ this ->dateFormatterPrototype ->getTimeType ();
433+ $ timezone = $ timezone ?: $ this ->dateFormatterPrototype ->getTimeZone ()-> toDateTimeZone ();
424434 $ calendar = $ calendar ?: $ this ->dateFormatterPrototype ->getCalendar ();
425435 $ pattern = $ pattern ?: $ this ->dateFormatterPrototype ->getPattern ();
426436 }
427437
428- $ hash = $ locale .'| ' .$ dateFormatValue .'| ' .$ timeFormatValue .'| ' .$ timezone ->getName ().'| ' .$ calendar .'| ' .$ pattern ;
438+ $ timezoneName = $ timezone ? $ timezone ->getName () : '(none) ' ;
439+
440+ $ hash = $ locale .'| ' .$ dateFormatValue .'| ' .$ timeFormatValue .'| ' .$ timezoneName .'| ' .$ calendar .'| ' .$ pattern ;
429441
430442 if (!isset ($ this ->dateFormatters [$ hash ])) {
431443 $ this ->dateFormatters [$ hash ] = new \IntlDateFormatter ($ locale , $ dateFormatValue , $ timeFormatValue , $ timezone , $ calendar , $ pattern );
0 commit comments