Skip to content

Commit

Permalink
added new DateTimeFormatProvider to provide date, time and interval f…
Browse files Browse the repository at this point in the history
…ormats
  • Loading branch information
sgiehl committed Oct 27, 2015
1 parent 4251fad commit db969cd
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 55 deletions.
60 changes: 12 additions & 48 deletions core/Date.php
Expand Up @@ -11,6 +11,7 @@

use Exception;
use Piwik\Container\StaticContainer;
use Piwik\Intl\Data\Provider\DateTimeFormatProvider;
use Piwik\Plugins\LanguagesManager\LanguagesManager;

/**
Expand Down Expand Up @@ -42,15 +43,15 @@ class Date
/** The default date time string format. */
const DATE_TIME_FORMAT = 'Y-m-d H:i:s';

const DATETIME_FORMAT_LONG = 'Intl_Format_DateTime_Long';
const DATETIME_FORMAT_SHORT = 'Intl_Format_DateTime_Short';
const DATE_FORMAT_LONG = 'Intl_Format_Date_Long';
const DATE_FORMAT_DAY_MONTH = 'Intl_Format_Date_Day_Month';
const DATE_FORMAT_SHORT = 'Intl_Format_Date_Short';
const DATE_FORMAT_MONTH_SHORT = 'Intl_Format_Month_Short';
const DATE_FORMAT_MONTH_LONG = 'Intl_Format_Month_Long';
const DATE_FORMAT_YEAR = 'Intl_Format_Year';
const TIME_FORMAT = 'Intl_Format_Time';
const DATETIME_FORMAT_LONG = DateTimeFormatProvider::DATE_FORMAT_LONG;
const DATETIME_FORMAT_SHORT = DateTimeFormatProvider::DATETIME_FORMAT_SHORT;
const DATE_FORMAT_LONG = DateTimeFormatProvider::DATE_FORMAT_LONG;
const DATE_FORMAT_DAY_MONTH = DateTimeFormatProvider::DATE_FORMAT_DAY_MONTH;
const DATE_FORMAT_SHORT = DateTimeFormatProvider::DATE_FORMAT_SHORT;
const DATE_FORMAT_MONTH_SHORT = DateTimeFormatProvider::DATE_FORMAT_MONTH_SHORT;
const DATE_FORMAT_MONTH_LONG = DateTimeFormatProvider::DATE_FORMAT_MONTH_LONG;
const DATE_FORMAT_YEAR = DateTimeFormatProvider::DATE_FORMAT_YEAR;
const TIME_FORMAT = DateTimeFormatProvider::TIME_FORMAT;

/**
* Max days for months (non-leap-year). See {@link addPeriod()} implementation.
Expand Down Expand Up @@ -612,38 +613,6 @@ public function subYear($n)
return new Date($result, $this->timezone);
}

protected static $use12HourClock;

protected function getTimeFormat()
{
if (is_null(self::$use12HourClock)) {
self::$use12HourClock = LanguagesManager::uses12HourClockForCurrentUser();
}

$timeFormat = 'Intl_Format_Time_24';

if (self::$use12HourClock) {
$timeFormat = 'Intl_Format_Time_12';

}

$translator = StaticContainer::get('Piwik\Translation\Translator');
$template = $translator->translate($timeFormat);

return $template;
}

/**
* For testing purpose only: Overwrites time format
*
* @param bool $use12HourClock
*/
public static function forceTimeFormat($use12HourClock = false)
{
self::$use12HourClock = $use12HourClock;
}


/**
* Returns a localized date string using the given template.
* The template should contain tags that will be replaced with localized date strings.
Expand All @@ -655,14 +624,9 @@ public function getLocalized($template)
{
$template = $this->replaceLegacyPlaceholders($template);

if (substr($template, 0, 5) == 'Intl_') {
$translator = StaticContainer::get('Piwik\Translation\Translator');
$template = $translator->translate($template);
}
$dateTimeFormatProvider = StaticContainer::get('Piwik\Intl\Data\Provider\DateTimeFormatProvider');

if (strpos($template, '{time}') !== false) {
$template = str_replace('{time}', $this->getTimeFormat(), $template);
}
$template = $dateTimeFormatProvider->getFormatPattern($template);

$tokens = self::parseFormat($template);

Expand Down
83 changes: 83 additions & 0 deletions core/Intl/Data/Provider/DateTimeFormatProvider.php
@@ -0,0 +1,83 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Intl\Data\Provider;

/**
* Provides date and time formats.
*/
class DateTimeFormatProvider
{
const DATETIME_FORMAT_LONG = 1;
const DATETIME_FORMAT_SHORT = 2;
const DATE_FORMAT_LONG = 10;
const DATE_FORMAT_DAY_MONTH = 11;
const DATE_FORMAT_SHORT = 12;
const DATE_FORMAT_MONTH_SHORT = 13;
const DATE_FORMAT_MONTH_LONG = 14;
const DATE_FORMAT_YEAR = 15;
const TIME_FORMAT = 20;

/**
* Returns the format pattern for the given format type
*
* @param int $format one of the format constants
*
* @return string
*/
public function getFormatPattern($format)
{
switch ($format) {
case self::DATETIME_FORMAT_LONG:
return 'EEEE, MMMM d, y HH:mm:ss';

case self::DATETIME_FORMAT_SHORT:
return 'MMM d, y HH:mm:ss';

case self::DATE_FORMAT_LONG:
return 'EEEE, MMMM d, y';

case self::DATE_FORMAT_DAY_MONTH:
return 'E, MMM d';

case self::DATE_FORMAT_SHORT:
return 'MMM d, y';

case self::DATE_FORMAT_MONTH_SHORT:
return 'MMM y';

case self::DATE_FORMAT_MONTH_LONG:
return 'MMMM y';

case self::DATE_FORMAT_YEAR:
return 'y';

case self::TIME_FORMAT:
return 'HH:mm:ss';
}

return $format;
}

/**
* Returns interval format pattern for the given format type
*
* @param bool $short whether to return short or long format pattern
* @param string $maxDifference maximal difference in interval dates (Y, M or D)
*
* @return string
*/
public function getRangeFormatPattern($short=false, $maxDifference='Y')
{
if ($short) {
return 'MMM d, y – MMM d, y';
}

return 'MMMM d, y – MMMM d, y';
}
}
9 changes: 3 additions & 6 deletions core/Period.php
Expand Up @@ -371,12 +371,9 @@ protected function getRangeFormat($short = false)
$maxDifference = 'M';
}

return $this->translator->translate(
sprintf(
'Intl_Format_Interval_%s_%s',
$short ? 'Short' : 'Long',
$maxDifference
));
$dateTimeFormatProvider = StaticContainer::get('Piwik\Intl\Data\Provider\DateTimeFormatProvider');

return $dateTimeFormatProvider->getRangeFormatPattern($short, $maxDifference);
}

/**
Expand Down
125 changes: 125 additions & 0 deletions plugins/Intl/DateTimeFormatProvider.php
@@ -0,0 +1,125 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\Intl;

use Piwik\Container\StaticContainer;
use Piwik\Plugins\LanguagesManager\LanguagesManager;

/**
* Provides date and time formats.
*/
class DateTimeFormatProvider extends \Piwik\Intl\Data\Provider\DateTimeFormatProvider
{
protected static $use12HourClock;

This comment has been minimized.

Copy link
@diosmosis

diosmosis Oct 28, 2015

Member

Since DateTimeFormatProvider will be stored in DI, there will only be one DateTimeFormatProvider in the container at a time. So this variable does not need to be static. Also, being static could cause problems in the future, since if multiple containers are created, this value will be the same for both, even if one container targets an instance w/ a different setting value.

This comment has been minimized.

Copy link
@sgiehl

sgiehl Oct 28, 2015

Author Member

Sure. I'll change that


/**
* Returns the format pattern for the given format type
*
* @param int $format one of the format constants
*
* @return string
*/
public function getFormatPattern($format)
{
$translator = StaticContainer::get('Piwik\Translation\Translator');

This comment has been minimized.

Copy link
@diosmosis

diosmosis Oct 28, 2015

Member

Since DateTimeFormatProvider is stored in DI, you can use constructor injection here instead of using StaticContainer.

FYI, I changed the docs here: matomo-org/developer-documentation#67 to describe how DI should be used. It's not yet published since it is too targeted towards core developers. Since you're a core developer, you may find it useful.

This comment has been minimized.

Copy link
@sgiehl

sgiehl Oct 28, 2015

Author Member

Good point. Will change...


switch ($format) {
case self::DATETIME_FORMAT_LONG:
$pattern = $translator->translate('Intl_Format_DateTime_Long');
break;

case self::DATETIME_FORMAT_SHORT:
$pattern = $translator->translate('Intl_Format_DateTime_Short');
break;

case self::DATE_FORMAT_LONG:
$pattern = $translator->translate('Intl_Format_Date_Long');
break;

case self::DATE_FORMAT_DAY_MONTH:
$pattern = $translator->translate('Intl_Format_Date_Day_Month');
break;

case self::DATE_FORMAT_SHORT:
$pattern = $translator->translate('Intl_Format_Date_Short');
break;

case self::DATE_FORMAT_MONTH_SHORT:
$pattern = $translator->translate('Intl_Format_Month_Short');
break;

case self::DATE_FORMAT_MONTH_LONG:
$pattern = $translator->translate('Intl_Format_Month_Long');
break;

case self::DATE_FORMAT_YEAR:
$pattern = $translator->translate('Intl_Format_Year');
break;

case self::TIME_FORMAT:
$pattern = $translator->translate('Intl_Format_Time');
break;

default:
$pattern = $format;
}

if (strpos($pattern, '{time}') !== false) {
$pattern = str_replace('{time}', $this->getTimeFormat(), $pattern);
}

return $pattern;
}

/**
* Returns interval format pattern for the given format type
*
* @param bool $short whether to return short or long format pattern
* @param string $maxDifference maximal difference in interval dates (Y, M or D)
*
* @return string
*/
public function getRangeFormatPattern($short=false, $maxDifference='Y')
{
return StaticContainer::get('Piwik\Translation\Translator')->translate(
sprintf(
'Intl_Format_Interval_%s_%s',
$short ? 'Short' : 'Long',
$maxDifference
));
}

protected function getTimeFormat()
{
if (is_null(self::$use12HourClock)) {
self::$use12HourClock = LanguagesManager::uses12HourClockForCurrentUser();
}

$timeFormat = 'Intl_Format_Time_24';

if (self::$use12HourClock) {
$timeFormat = 'Intl_Format_Time_12';
}

$translator = StaticContainer::get('Piwik\Translation\Translator');
$template = $translator->translate($timeFormat);

return $template;
}

/**
* For testing purpose only: Overwrites time format
*
* @param bool $use12HourClock
*/
public function forceTimeFormat($use12HourClock = false)
{
self::$use12HourClock = $use12HourClock;
}
}
5 changes: 5 additions & 0 deletions plugins/Intl/config/config.php
@@ -0,0 +1,5 @@
<?php

return array(
'Piwik\Intl\Data\Provider\DateTimeFormatProvider' => DI\object('Piwik\Plugins\Intl\DateTimeFormatProvider')
);
2 changes: 1 addition & 1 deletion tests/PHPUnit/Unit/DateTest.php
Expand Up @@ -356,7 +356,7 @@ public function testGetLocalizedTimeFormats($language, $use12HourClock, $time, $
{
Translate::loadAllTranslations();
StaticContainer::get('Piwik\Translation\Translator')->setCurrentLanguage($language);
Date::forceTimeFormat($use12HourClock);
StaticContainer::get('Piwik\Intl\Data\Provider\DateTimeFormatProvider')->forceTimeFormat($use12HourClock);

$date = Date::factory($time);

Expand Down

0 comments on commit db969cd

Please sign in to comment.