Skip to content

Fix #22964 #26270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ public function date($date = null, $locale = null, $useTimezone = true, $include
$timeType = $includeTime ? \IntlDateFormatter::SHORT : \IntlDateFormatter::NONE;
$formatter = new \IntlDateFormatter(
$locale,
\IntlDateFormatter::SHORT,
\IntlDateFormatter::MEDIUM,
$timeType,
new \DateTimeZone($timezone)
);

$date = $this->appendTimeIfNeeded($date, $includeTime);
$date = $this->appendTimeIfNeeded($date, $includeTime, $timezone, $locale);
$date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp();
break;
}
Expand Down Expand Up @@ -347,16 +347,44 @@ public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s')
}

/**
* Retrieve date with time
* Append time to DateTime
*
* @param string $date
* @param bool $includeTime
* @param boolean $includeTime
* @param string $timezone
* @param string $locale
* @return string
* @throws LocalizedException
*/
private function appendTimeIfNeeded($date, $includeTime)
private function appendTimeIfNeeded($date, $includeTime, $timezone, $locale)
{
if ($includeTime && !preg_match('/\d{1}:\d{2}/', $date)) {
$date .= " 0:00am";

$formatterWithoutHour = new \IntlDateFormatter(
$locale,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::NONE,
new \DateTimeZone($timezone)
);
$convertedDate = $formatterWithoutHour->parse($date);

if (!$convertedDate) {
throw new LocalizedException(
new Phrase(
'Could not append time to DateTime'
)
);

}

$formatterWithHour = new \IntlDateFormatter(
$locale,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::SHORT,
new \DateTimeZone($timezone)
);

$date = $formatterWithHour->format($convertedDate);
}
return $date;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ public function dateIncludeTimeDataProvider(): array
true, // include time
1495170060 // expected timestamp
],
'Parse greek d/m/y date without time' => [
'30/10/2021', // datetime
'el_GR', // locale
false, // include time
1635570000 // expected timestamp
],
'Parse greek d/m/y date with time' => [
'30/10/2021, 12:01 π.μ.', // datetime
'el_GR', // locale
true, // include time
1635570060 // expected timestamp
],
];
}

Expand Down