Skip to content

Commit 2fa35b4

Browse files
anzinxmav
authored andcommitted
Fixed code by recommendation
1 parent f9b9862 commit 2fa35b4

File tree

3 files changed

+58
-89
lines changed
  • app/code/Magento/Config/Model/Config/Source/Date
  • dev/tests/integration/testsuite/Magento/Cron/Model
  • lib/internal/Magento/Framework/View/Element/Html

3 files changed

+58
-89
lines changed

app/code/Magento/Config/Model/Config/Source/Date/Short.php

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,24 @@
55
*/
66
namespace Magento\Config\Model\Config\Source\Date;
77

8-
use IntlDateFormatter;
9-
use Magento\Framework\App\ObjectManager;
10-
use Magento\Framework\Locale\ResolverInterface;
11-
128
/**
139
* @api
1410
* @since 100.0.2
1511
*/
1612
class Short implements \Magento\Framework\Option\ArrayInterface
1713
{
18-
/**
19-
* @var IntlDateFormatter
20-
*/
21-
private $dateFormatter;
22-
2314
/**
2415
* @return array
2516
*/
2617
public function toOptionArray()
2718
{
2819
$arr = [];
20+
$dateTime = new \DateTime();
2921
$arr[] = ['label' => '', 'value' => ''];
30-
$arr[] = ['label' => 'MM/DD/YY ' . $this->getTimeFormat(time(), '(M/d/y)'), 'value' => 'M/d/y'];
31-
$arr[] = ['label' => 'MM/DD/YYYY '. $this->getTimeFormat(time(), '(M/d/Y)'), 'value' => 'M/d/Y'];
32-
$arr[] = ['label' => 'DD/MM/YY ' . $this->getTimeFormat(time(), '(d/m/y)'), 'value' => 'd/M/y'];
33-
$arr[] = ['label' => 'DD/MM/YYYY ' . $this->getTimeFormat(time(), '(d/m/Y)'), 'value' => 'd/M/Y'];
22+
$arr[] = ['label' => 'MM/DD/YY (' . $dateTime->format('m/d/y') . ')', 'value' => '%m/%d/%y'];
23+
$arr[] = ['label' => 'MM/DD/YYYY (' . $dateTime->format('m/d/Y') . ')', 'value' => '%m/%d/%Y'];
24+
$arr[] = ['label' => 'DD/MM/YY (' . $dateTime->format('d/m/y') . ')', 'value' => '%d/%m/%y'];
25+
$arr[] = ['label' => 'DD/MM/YYYY (' . $dateTime->format('d/m/Y') . ')', 'value' => '%d/%m/%Y'];
3426
return $arr;
3527
}
36-
37-
/**
38-
* This method format timestamp value.
39-
*
40-
* @param int $datetime
41-
* @param string $format
42-
*
43-
* @return string
44-
*/
45-
private function getTimeFormat(int $datetime, string $format = 'Y/M/d'): string
46-
{
47-
if (!$this->dateFormatter) {
48-
$localeResolver = ObjectManager::getInstance()->get(ResolverInterface::class);
49-
$this->dateFormatter = new \IntlDateFormatter(
50-
$localeResolver->getLocale(),
51-
IntlDateFormatter::SHORT,
52-
IntlDateFormatter::SHORT
53-
);
54-
}
55-
$this->dateFormatter->setPattern($format);
56-
57-
return $this->dateFormatter->format($datetime);
58-
}
5928
}

dev/tests/integration/testsuite/Magento/Cron/Model/ScheduleTest.php

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
namespace Magento\Cron\Model;
77

8-
use IntlDateFormatter;
9-
use Magento\Framework\Locale\ResolverInterface;
108
use Magento\Framework\Stdlib\DateTime\DateTime;
119
use \Magento\TestFramework\Helper\Bootstrap;
1210

@@ -27,11 +25,6 @@ class ScheduleTest extends \PHPUnit\Framework\TestCase
2725
*/
2826
protected $dateTime;
2927

30-
/**
31-
* @var IntlDateFormatter
32-
*/
33-
private $dateFormatter;
34-
3528
/**
3629
* @ingeritdoc
3730
*/
@@ -70,15 +63,14 @@ public function testTryLockJobAlreadyLockedFails()
7063
public function testTryLockJobAlreadyLockedSucceeds()
7164
{
7265
$offsetInThePast = 2*24*60*60;
73-
$gmtTimestamp = $this->dateTime->gmtTimestamp();
7466

7567
$oldSchedule = $this->scheduleFactory->create()
7668
->setCronExpr("* * * * *")
7769
->setJobCode("test_job")
7870
->setStatus(Schedule::STATUS_RUNNING)
79-
->setCreatedAt($this->getTimeFormat($gmtTimestamp - $offsetInThePast))
80-
->setScheduledAt($this->getTimeFormat($gmtTimestamp - $offsetInThePast + 60, 'Y-M-d HH:mm'))
81-
->setExecutedAt($this->getTimeFormat($gmtTimestamp - $offsetInThePast + 61, 'Y-M-d HH:mm'));
71+
->setCreatedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp() - $offsetInThePast))
72+
->setScheduledAt(date('Y-m-d H:i', $this->dateTime->gmtTimestamp() - $offsetInThePast + 60))
73+
->setExecutedAt(date('Y-m-d H:i', $this->dateTime->gmtTimestamp() - $offsetInThePast + 61));
8274
$oldSchedule->save();
8375

8476
$schedule = $this->createSchedule("test_job", Schedule::STATUS_PENDING);
@@ -118,39 +110,14 @@ public function testTryLockJobDifferentJobLocked()
118110
*/
119111
private function createSchedule($jobCode, $status, $timeOffset = 0)
120112
{
121-
$gmtTimestamp = $this->dateTime->gmtTimestamp();
122-
123113
$schedule = $this->scheduleFactory->create()
124114
->setCronExpr("* * * * *")
125115
->setJobCode($jobCode)
126116
->setStatus($status)
127-
->setCreatedAt($this->getTimeFormat($gmtTimestamp))
128-
->setScheduledAt($this->getTimeFormat($gmtTimestamp + $timeOffset, 'Y-M-d HH:mm'));
117+
->setCreatedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()))
118+
->setScheduledAt(date('Y-m-d H:i', $this->dateTime->gmtTimestamp() + $timeOffset));
129119
$schedule->save();
130120

131121
return $schedule;
132122
}
133-
134-
/**
135-
* This method format timestamp value.
136-
*
137-
* @param int $datetime
138-
* @param string $format
139-
*
140-
* @return string
141-
*/
142-
private function getTimeFormat(int $datetime, string $format = 'Y-M-d HH:mm:ss'): string
143-
{
144-
if (!$this->dateFormatter) {
145-
$localeResolver = Bootstrap::getObjectManager()->create(ResolverInterface::class);
146-
$this->dateFormatter = new IntlDateFormatter(
147-
$localeResolver->getLocale(),
148-
IntlDateFormatter::SHORT,
149-
IntlDateFormatter::SHORT
150-
);
151-
}
152-
$this->dateFormatter->setPattern($format);
153-
154-
return $this->dateFormatter->format($datetime);
155-
}
156123
}

lib/internal/Magento/Framework/View/Element/Html/Date.php

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,30 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento\Framework\View\Element\Html;
76

8-
use IntlDateFormatter;
9-
use Magento\Framework\App\ObjectManager;
10-
use Magento\Framework\Locale\ResolverInterface;
7+
namespace Magento\Framework\View\Element\Html;
118

129
/**
1310
* Date element block
1411
*/
1512
class Date extends \Magento\Framework\View\Element\Template
1613
{
14+
/**
15+
* Date format not supported function date().
16+
*/
17+
private const DATE_FORMAT_NOT_SUPPORTED = [
18+
'%a', '%A', '%d', '%e', '%u', '%w', '%W', '%b', '%h', '%B', '%m', '%y', '%Y', '%D', '%F', '%x', '%n', '%t',
19+
'%H', '%k', '%I', '%l', '%M', '%p', '%P', '%r', '%R', '%S', '%T', '%X', '%z', '%Z', '%c', '%s', '%g', '%G', '%%'
20+
];
21+
22+
/**
23+
* Date format supported by function date().
24+
*/
25+
private const DATE_FORMAT_SUPPORTED = [
26+
'D', 'l', 'd', 'j', 'N', 'w', 'W', 'M', 'M', 'F', 'm', 'y', 'Y', 'm/d/y', 'Y-m-d', 'm/d/y',"\n","\t", 'H', 'G',
27+
'h', 'g', 'i', 'A', 'a', 'h:i:s A', 'H:i', 's', 'H:i:s', 'H:i:s', 'O', 'T', 'D M j H:i:s Y', 'U', 'y', 'Y', '%'
28+
];
29+
1730
/**
1831
* Render block HTML
1932
*
@@ -80,28 +93,48 @@ protected function _toHtml()
8093
public function getEscapedValue()
8194
{
8295
if ($this->getFormat() && $this->getValue()) {
83-
$localeResolver = ObjectManager::getInstance()->get(ResolverInterface::class);
84-
$dateFormatter = new IntlDateFormatter(
85-
$localeResolver->getLocale(),
86-
IntlDateFormatter::SHORT,
87-
IntlDateFormatter::SHORT
88-
);
89-
$dateFormatter->setPattern($this->getFormat());
90-
91-
return $dateFormatter->format(strtotime($this->getValue()));
96+
return $this->getDateByFormat($this->getFormat(), strtotime($this->getValue()));
9297
}
9398
return $this->escapeHtml($this->getValue());
9499
}
95100

96101
/**
97102
* Produce and return block's html output
98103
*
99-
* {@inheritdoc}
100-
*
101104
* @return string
102105
*/
103106
public function getHtml()
104107
{
105108
return $this->toHtml();
106109
}
110+
111+
/**
112+
* Method to get date by format.
113+
*
114+
* @param string $format
115+
* @param int $timestamp
116+
*
117+
* @return string
118+
*/
119+
private function getDateByFormat(string $format, int $timestamp): string
120+
{
121+
$format = str_replace(self::DATE_FORMAT_NOT_SUPPORTED, self::DATE_FORMAT_SUPPORTED, $format);
122+
123+
if (strpos($format, '%') !== false) {
124+
$unsupportedData = ['%U', '%V', '%C'];
125+
126+
foreach ($unsupportedData as $unsupported) {
127+
if (strpos($format, $unsupported) !== false) {
128+
if ($unsupported === '%C') {
129+
$format = str_replace($unsupported, round(date("Y", $timestamp) / 100), $format);
130+
131+
continue;
132+
}
133+
$format = str_replace($unsupported, date("W", strtotime("-1 day", $timestamp)), $format);
134+
}
135+
}
136+
}
137+
138+
return date($format, $timestamp);
139+
}
107140
}

0 commit comments

Comments
 (0)