Skip to content

Commit

Permalink
Merge eaeb601 into 82abdea
Browse files Browse the repository at this point in the history
  • Loading branch information
z38 committed Jun 17, 2016
2 parents 82abdea + eaeb601 commit baeea2f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
52 changes: 30 additions & 22 deletions Templating/Helper/DateTimeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* DateHelper displays culture information. More information here
* http://userguide.icu-project.org/formatparse/datetime.
*
* NEXT_MAJOR: Remove all \DateTime hints from PHPDoc
*
* @author Thomas Rabaix <thomas.rabaix@ekino.com>
* @author Alexander <iam.asm89@gmail.com>
*/
Expand Down Expand Up @@ -46,10 +48,10 @@ public function __construct(TimezoneDetectorInterface $timezoneDetector, $charse
}

/**
* @param \Datetime|string|int $date
* @param null|string $locale
* @param null|string timezone
* @param null|int dateType See \IntlDateFormatter::getDateType
* @param \DateTime|\DateTimeInterface|string|int $date
* @param null|string $locale
* @param null|string $timezone
* @param null|int $dateType See \IntlDateFormatter::getDateType
*
* @return string
*/
Expand All @@ -69,11 +71,11 @@ public function formatDate($date, $locale = null, $timezone = null, $dateType =
}

/**
* @param \Datetime|string|int $datetime
* @param null|string $locale
* @param null|string timezone
* @param null|int dateType See \IntlDateFormatter::getDateType
* @param null|int timeType See \IntlDateFormatter::getTimeType
* @param \DateTime|\DateTimeInterface|string|int $datetime
* @param null|string $locale
* @param null|string $timezone
* @param null|int $dateType See \IntlDateFormatter::getDateType
* @param null|int $timeType See \IntlDateFormatter::getTimeType
*
* @return string
*/
Expand All @@ -93,10 +95,10 @@ public function formatDateTime($datetime, $locale = null, $timezone = null, $dat
}

/**
* @param \Datetime|string|int $time
* @param null|string $locale
* @param null|string timezone
* @param null|int timeType See \IntlDateFormatter::getTimeType
* @param \DateTime|\DateTimeInterface|string|int $time
* @param null|string $locale
* @param null|string $timezone
* @param null|int $timeType See \IntlDateFormatter::getTimeType
*
* @return string
*/
Expand All @@ -116,10 +118,10 @@ public function formatTime($time, $locale = null, $timezone = null, $timeType =
}

/**
* @param \Datetime|string|int $datetime
* @param $pattern
* @param null|string $locale
* @param null|string timezone
* @param \DateTime|\DateTimeInterface|string|int $datetime
* @param $pattern
* @param null|string $locale
* @param null|string $timezone
*
* @return string
*/
Expand All @@ -140,12 +142,14 @@ public function format($datetime, $pattern, $locale = null, $timezone = null)
}

/**
* NEXT_MAJOR: Change to $date to \DateTimeInterface.
*
* @param \IntlDateFormatter $formatter
* @param \Datetime $date
* @param \DateTime $date
*
* @return string
*/
public function process(\IntlDateFormatter $formatter, \Datetime $date)
public function process(\IntlDateFormatter $formatter, \DateTime $date)
{
// strange bug with PHP 5.3.3-7+squeeze14 with Suhosin-Patch
// getTimestamp() method alters the object...
Expand All @@ -155,17 +159,21 @@ public function process(\IntlDateFormatter $formatter, \Datetime $date)
/**
* Gets a date time instance by a given data and timezone.
*
* @param \Datetime|string|int $data Value representing date
* @param null|string $timezone Timezone of the date
* @param \DateTime|\DateTimeInterface|string|int $data Value representing date
* @param null|string $timezone Timezone of the date
*
* @return \Datetime
* @return \DateTime
*/
public function getDatetime($data, $timezone = null)
{
if ($data instanceof \DateTime) {
return $data;
}

if ($data instanceof \DateTimeImmutable) {
return \DateTime::createFromFormat(\DateTime::ATOM, $data->format(\DateTime::ATOM));
}

// the format method accept array or integer
if (is_numeric($data)) {
$data = (int) $data;
Expand Down
20 changes: 20 additions & 0 deletions Tests/Helper/DateTimeHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,24 @@ public function testLocaleTimezones()
$this->assertEquals('12:37', $helper->format($dateParis, 'HH:mm'), 'A date in the Europe/Paris timezone, should be corrected when formatted with timezone Europe/London.');
$this->assertEquals('13:37', $helperWithMapping->format($dateParis, 'HH:mm'), 'A date in the Europe/Paris timezone, should be corrected when formatted with timezone Europe/Paris.');
}

/**
* @requires PHP 5.5
*/
public function testImmutable()
{
$localeDetector = $this->getMock('Sonata\IntlBundle\Locale\LocaleDetectorInterface');
$localeDetector->expects($this->any())
->method('getLocale')->will($this->returnValue('fr'));

$timezoneDetector = $this->getMock('Sonata\IntlBundle\Timezone\TimezoneDetectorInterface');
$timezoneDetector->expects($this->any())
->method('getTimezone')->will($this->returnValue('Europe/Paris'));

$helper = new DateTimeHelper($timezoneDetector, 'UTF-8', $localeDetector);

$date = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s T', '2009-02-15 15:16:17 HKT');

$this->assertEquals('08:16', $helper->format($date, 'HH:mm'));
}
}

0 comments on commit baeea2f

Please sign in to comment.