Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new DateTimeFormatProvider to provide date, time and interval f…
…ormats
- Loading branch information
Showing
6 changed files
with
229 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
/** | ||
* 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.
Sorry, something went wrong.
diosmosis
Member
|
||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
return array( | ||
'Piwik\Intl\Data\Provider\DateTimeFormatProvider' => DI\object('Piwik\Plugins\Intl\DateTimeFormatProvider') | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.