diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php index 52565f3879455..51d42494d1def 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php @@ -47,12 +47,14 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer * @param string|null $inputTimezone The name of the input timezone * @param string|null $outputTimezone The name of the output timezone * @param string $format The date format + * @param string|null $parseFormat The parse format when different from $format */ - public function __construct(string $inputTimezone = null, string $outputTimezone = null, string $format = 'Y-m-d H:i:s') + public function __construct(string $inputTimezone = null, string $outputTimezone = null, string $format = 'Y-m-d H:i:s', string $parseFormat = null) { parent::__construct($inputTimezone, $outputTimezone); - $this->generateFormat = $this->parseFormat = $format; + $this->generateFormat = $format; + $this->parseFormat = $parseFormat ?? $format; // See https://php.net/datetime.createfromformat // The character "|" in the format makes sure that the parts of a date diff --git a/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php index 85b00db57c091..e5953bf9edc85 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php @@ -73,8 +73,10 @@ public function buildForm(FormBuilderInterface $builder, array $options) } }); + $parseFormat = null; + if (null !== $options['reference_date']) { - $format = 'Y-m-d '.$format; + $parseFormat = 'Y-m-d '.$format; $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) { $data = $event->getData(); @@ -85,7 +87,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) }); } - $builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format)); + $builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format, $parseFormat)); } else { $hourOptions = $minuteOptions = $secondOptions = [ 'error_bubbling' => true, diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php index 8da1e2dd5c35e..8fa764f6f76f6 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php @@ -279,6 +279,76 @@ public function testSubmitWithSecondsAndBrowserOmissionSeconds() $this->assertEquals('03:04:00', $form->getViewData()); } + public function testPreSetDataDifferentTimezones() + { + $form = $this->factory->create(static::TESTED_TYPE, null, [ + 'model_timezone' => 'UTC', + 'view_timezone' => 'Europe/Berlin', + 'input' => 'datetime', + 'with_seconds' => true, + 'reference_date' => new \DateTimeImmutable('2019-01-01', new \DateTimeZone('UTC')), + ]); + $form->setData(new \DateTime('2022-01-01 15:09:10', new \DateTimeZone('UTC'))); + + $this->assertSame('15:09:10', $form->getData()->format('H:i:s')); + $this->assertSame([ + 'hour' => '16', + 'minute' => '9', + 'second' => '10', + ], $form->getViewData()); + } + + public function testPreSetDataDifferentTimezonesDuringDaylightSavingTime() + { + $form = $this->factory->create(static::TESTED_TYPE, null, [ + 'model_timezone' => 'UTC', + 'view_timezone' => 'Europe/Berlin', + 'input' => 'datetime', + 'with_seconds' => true, + 'reference_date' => new \DateTimeImmutable('2019-07-12', new \DateTimeZone('UTC')), + ]); + $form->setData(new \DateTime('2022-04-29 15:09:10', new \DateTimeZone('UTC'))); + + $this->assertSame('15:09:10', $form->getData()->format('H:i:s')); + $this->assertSame([ + 'hour' => '17', + 'minute' => '9', + 'second' => '10', + ], $form->getViewData()); + } + + public function testPreSetDataDifferentTimezonesUsingSingleTextWidget() + { + $form = $this->factory->create(static::TESTED_TYPE, null, [ + 'model_timezone' => 'UTC', + 'view_timezone' => 'Europe/Berlin', + 'input' => 'datetime', + 'with_seconds' => true, + 'reference_date' => new \DateTimeImmutable('2019-01-01', new \DateTimeZone('UTC')), + 'widget' => 'single_text', + ]); + $form->setData(new \DateTime('2022-01-01 15:09:10', new \DateTimeZone('UTC'))); + + $this->assertSame('15:09:10', $form->getData()->format('H:i:s')); + $this->assertSame('16:09:10', $form->getViewData()); + } + + public function testPreSetDataDifferentTimezonesDuringDaylightSavingTimeUsingSingleTextWidget() + { + $form = $this->factory->create(static::TESTED_TYPE, null, [ + 'model_timezone' => 'UTC', + 'view_timezone' => 'Europe/Berlin', + 'input' => 'datetime', + 'with_seconds' => true, + 'reference_date' => new \DateTimeImmutable('2019-07-12', new \DateTimeZone('UTC')), + 'widget' => 'single_text', + ]); + $form->setData(new \DateTime('2022-04-29 15:09:10', new \DateTimeZone('UTC'))); + + $this->assertSame('15:09:10', $form->getData()->format('H:i:s')); + $this->assertSame('17:09:10', $form->getViewData()); + } + public function testSubmitDifferentTimezones() { $form = $this->factory->create(static::TESTED_TYPE, null, [