Skip to content

Commit 7a094be

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-68976-Date-Format' into Okapis-develop-pr
2 parents 2779172 + e5f46b0 commit 7a094be

File tree

5 files changed

+83
-7
lines changed

5 files changed

+83
-7
lines changed

app/code/Magento/Catalog/Block/Product/View/Options/Type/Date.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public function getCalendarDateHtml()
8181
$yearStart = $this->_catalogProductOptionTypeDate->getYearStart();
8282
$yearEnd = $this->_catalogProductOptionTypeDate->getYearEnd();
8383

84+
$dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
85+
/** Escape RTL characters which are present in some locales and corrupt formatting */
86+
$escapedDateFormat = preg_replace('/[^MmDdYy\/\.\-]/', '', $dateFormat);
8487
$calendar = $this->getLayout()->createBlock(
8588
\Magento\Framework\View\Element\Html\Date::class
8689
)->setId(
@@ -92,7 +95,7 @@ public function getCalendarDateHtml()
9295
)->setImage(
9396
$this->getViewFileUrl('Magento_Theme::calendar.png')
9497
)->setDateFormat(
95-
$this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT)
98+
$escapedDateFormat
9699
)->setValue(
97100
$value
98101
)->setYearsRange(

app/code/Magento/Catalog/Model/Product/Option/Type/Date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function prepareForCart()
155155

156156
if ($this->_dateExists()) {
157157
if ($this->useCalendar()) {
158-
$timestamp += (new \DateTime($value['date']))->getTimestamp();
158+
$timestamp += $this->_localeDate->date($value['date'], null, true, false)->getTimestamp();
159159
} else {
160160
$timestamp += mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
161161
}

lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function getDateTimeFormat($type)
153153
* {@inheritdoc}
154154
* @SuppressWarnings(PHPMD.NPathComplexity)
155155
*/
156-
public function date($date = null, $locale = null, $useTimezone = true)
156+
public function date($date = null, $locale = null, $useTimezone = true, $includeTime = true)
157157
{
158158
$locale = $locale ?: $this->_localeResolver->getLocale();
159159
$timezone = $useTimezone
@@ -165,10 +165,11 @@ public function date($date = null, $locale = null, $useTimezone = true)
165165
} elseif ($date instanceof \DateTime) {
166166
return $date->setTimezone(new \DateTimeZone($timezone));
167167
} elseif (!is_numeric($date)) {
168+
$timeType = $includeTime ? \IntlDateFormatter::SHORT : \IntlDateFormatter::NONE;
168169
$formatter = new \IntlDateFormatter(
169170
$locale,
170171
\IntlDateFormatter::SHORT,
171-
\IntlDateFormatter::SHORT,
172+
$timeType,
172173
new \DateTimeZone($timezone)
173174
);
174175
$date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp();

lib/internal/Magento/Framework/Stdlib/DateTime/TimezoneInterface.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ public function getDateTimeFormat($type);
6060
/**
6161
* Create \DateTime object for current locale
6262
*
63-
* @param mixed $date
63+
* @param mixed $date
6464
* @param string $locale
65-
* @param bool $useTimezone
65+
* @param bool $useTimezone
66+
* @param bool $includeTime
6667
* @return \DateTime
6768
*/
68-
public function date($date = null, $locale = null, $useTimezone = true);
69+
public function date($date = null, $locale = null, $useTimezone = true, $includeTime = true);
6970

7071
/**
7172
* Create \DateTime object with date converted to scope timezone and scope Locale
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Stdlib\Test\Unit\DateTime;
7+
8+
use Magento\Framework\Stdlib\DateTime\Timezone;
9+
10+
/**
11+
* Test for @see Timezone
12+
*/
13+
class TimezoneTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
17+
*/
18+
private $objectManager;
19+
20+
/**
21+
* @var \PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $scopeConfigMock;
24+
25+
protected function setUp()
26+
{
27+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
28+
$this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
29+
->disableOriginalConstructor()
30+
->getMock();
31+
parent::setUp();
32+
}
33+
34+
/**
35+
* Test date parsing with different includeTime options
36+
*
37+
* @param string $date
38+
* @param string $locale
39+
* @param bool $includeTime
40+
* @param int $expectedTimestamp
41+
* @dataProvider dateIncludeTimeDataProvider
42+
*/
43+
public function testDateIncludeTime($date, $locale, $includeTime, $expectedTimestamp)
44+
{
45+
$this->scopeConfigMock->method('getValue')->willReturn('America/Chicago');
46+
/** @var Timezone $timezone */
47+
$timezone = $this->objectManager->getObject(Timezone::class, ['scopeConfig' => $this->scopeConfigMock]);
48+
49+
/** @var \DateTime $dateTime */
50+
$dateTime = $timezone->date($date, $locale, true, $includeTime);
51+
$this->assertEquals($expectedTimestamp, $dateTime->getTimestamp());
52+
}
53+
54+
public function dateIncludeTimeDataProvider()
55+
{
56+
return [
57+
'Parse date without time' => [
58+
'19/05/2017', // date
59+
'ar_KW', // locale
60+
false, // include time
61+
1495170000 // expected timestamp
62+
],
63+
'Parse date with time' => [
64+
'05/19/2017 00:01 am', // date
65+
'en_US', // locale
66+
true, // include time
67+
1495170060 // expected timestamp
68+
],
69+
];
70+
}
71+
}

0 commit comments

Comments
 (0)