Skip to content

Commit b7a0b58

Browse files
committed
Merge remote-tracking branch 'origin/MC-37369' into 2.4-develop-pr41
2 parents 0520960 + 050b46f commit b7a0b58

File tree

6 files changed

+264
-114
lines changed

6 files changed

+264
-114
lines changed

app/code/Magento/Catalog/Test/Unit/Model/Product/Filter/DateTimeTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Catalog\Model\Product\Filter\DateTime;
1111
use Magento\Framework\Locale\Resolver;
1212
use Magento\Framework\Locale\ResolverInterface;
13+
use Magento\Framework\Stdlib\DateTime\Intl\DateFormatterFactory;
1314
use Magento\Framework\Stdlib\DateTime\Timezone;
1415
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1516
use PHPUnit\Framework\TestCase;
@@ -43,7 +44,7 @@ function () {
4344
);
4445
$timezone = $objectManager->getObject(
4546
Timezone::class,
46-
['localeResolver' => $localeResolver]
47+
['localeResolver' => $localeResolver, 'dateFormatterFactory' => new DateFormatterFactory()]
4748
);
4849
$stdlibDateTimeFilter = $objectManager->getObject(
4950
\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class,

app/code/Magento/Customer/Test/Unit/Block/Widget/DobTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\Framework\Exception\NoSuchEntityException;
2020
use Magento\Framework\Locale\Resolver;
2121
use Magento\Framework\Locale\ResolverInterface;
22+
use Magento\Framework\Stdlib\DateTime\Intl\DateFormatterFactory;
2223
use Magento\Framework\Stdlib\DateTime\Timezone;
2324
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
2425
use Magento\Framework\View\Element\Html\Date;
@@ -50,7 +51,7 @@ class DobTest extends TestCase
5051
const YEAR = '2014';
5152

5253
// Value of date('Y', strtotime(self::DATE))
53-
const DATE_FORMAT = 'M/d/Y';
54+
const DATE_FORMAT = 'M/d/y';
5455

5556
/** Constants used by Dob::setDateInput($code, $html) */
5657
const DAY_HTML =
@@ -119,7 +120,7 @@ function () {
119120
);
120121
$timezone = $objectManager->getObject(
121122
Timezone::class,
122-
['localeResolver' => $localeResolver]
123+
['localeResolver' => $localeResolver, 'dateFormatterFactory' => new DateFormatterFactory()]
123124
);
124125

125126
$this->_locale = Resolver::DEFAULT_LOCALE;
@@ -357,7 +358,8 @@ public function getDateFormatDataProvider(): array
357358
preg_replace(
358359
'/[^MmDdYy\/\.\-]/',
359360
'',
360-
(new \IntlDateFormatter('ar_SA', \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE))
361+
(new DateFormatterFactory())
362+
->create('ar_SA', \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE)
361363
->getPattern()
362364
)
363365
],

app/code/Magento/Ui/Component/Form/Element/DataType/Date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function getComponentName()
111111
public function convertDate($date, $hour = 0, $minute = 0, $second = 0, $setUtcTimeZone = true)
112112
{
113113
try {
114-
$dateObj = $this->localeDate->date($date, $this->getLocale(), false);
114+
$dateObj = $this->localeDate->date($date, $this->getLocale(), false, false);
115115
$dateObj->setTime($hour, $minute, $second);
116116
//convert store date to default date in UTC timezone without DST
117117
if ($setUtcTimeZone) {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Stdlib\DateTime\Intl;
9+
10+
/**
11+
* Class to get Intl date formatter by locale
12+
*/
13+
class DateFormatterFactory
14+
{
15+
/**
16+
* Custom date formats by locale
17+
*/
18+
private const CUSTOM_DATE_FORMATS = [
19+
'ar_SA' => [
20+
\IntlDateFormatter::SHORT => 'd/MM/y',
21+
]
22+
];
23+
24+
/**
25+
* Create Intl Date formatter
26+
*
27+
* The Intl Date formatter gives date formats by ICU standard.
28+
* http://userguide.icu-project.org/formatparse/datetime
29+
*
30+
* @param string $locale
31+
* @param int $dateStyle
32+
* @param int $timeStyle
33+
* @param string|null $timeZone
34+
* @param bool $useFourDigitsForYear
35+
* @return \IntlDateFormatter
36+
*/
37+
public function create(
38+
string $locale,
39+
int $dateStyle,
40+
int $timeStyle,
41+
?string $timeZone = null,
42+
bool $useFourDigitsForYear = true
43+
): \IntlDateFormatter {
44+
$formatter = new \IntlDateFormatter(
45+
$locale,
46+
$dateStyle,
47+
$timeStyle,
48+
$timeZone
49+
);
50+
/**
51+
* Process custom date formats
52+
*/
53+
$customDateFormat = $this->getCustomDateFormat($locale, $dateStyle, $timeStyle);
54+
if ($customDateFormat !== null) {
55+
$formatter->setPattern($customDateFormat);
56+
} elseif ($dateStyle === \IntlDateFormatter::SHORT && $useFourDigitsForYear) {
57+
/**
58+
* Gives 4 places for year value in short style
59+
*/
60+
$longYearPattern = $this->setFourYearPlaces((string)$formatter->getPattern());
61+
$formatter->setPattern($longYearPattern);
62+
}
63+
64+
return $formatter;
65+
}
66+
67+
/**
68+
* Get custom date format if it exists
69+
*
70+
* @param string $locale
71+
* @param int $dateStyle
72+
* @param int $timeStyle
73+
* @return string
74+
*/
75+
private function getCustomDateFormat(string $locale, int $dateStyle, int $timeStyle): ?string
76+
{
77+
$customDateFormat = null;
78+
if ($dateStyle !== \IntlDateFormatter::NONE && isset(self::CUSTOM_DATE_FORMATS[$locale][$dateStyle])) {
79+
$customDateFormat = self::CUSTOM_DATE_FORMATS[$locale][$dateStyle];
80+
if ($timeStyle !== \IntlDateFormatter::NONE) {
81+
$timeFormat = (new \IntlDateFormatter($locale, \IntlDateFormatter::NONE, $timeStyle))
82+
->getPattern();
83+
$customDateFormat .= ' ' . $timeFormat;
84+
}
85+
}
86+
87+
return $customDateFormat;
88+
}
89+
90+
/**
91+
* Set 4 places for year value in format string
92+
*
93+
* @param string $format
94+
* @return string
95+
*/
96+
private function setFourYearPlaces(string $format): string
97+
{
98+
return preg_replace(
99+
'/(?<!y)yy(?!y)/',
100+
'y',
101+
$format
102+
);
103+
}
104+
}

0 commit comments

Comments
 (0)