diff options
author | Matthieu Aubry <matt@piwik.org> | 2015-10-30 02:42:52 +0300 |
---|---|---|
committer | Matthieu Aubry <matt@piwik.org> | 2015-10-30 02:42:52 +0300 |
commit | fe36f931407ba99f3f8f708143e283f6846bb2f7 (patch) | |
tree | 155a0c9afc0cfddd98eddabe00024ac955ea15b0 /core | |
parent | 2de7fef4d2176dc435a7dd9c1932b7cf6fdcf77c (diff) | |
parent | c485c086be538e87ba92f26c1f3edd585c55bd46 (diff) |
Merge pull request #9093 from piwik/timeformats
Allow user to change time formats (12/24-hour clock)
Diffstat (limited to 'core')
-rw-r--r-- | core/Date.php | 27 | ||||
-rw-r--r-- | core/Intl/Data/Provider/DateTimeFormatProvider.php | 83 | ||||
-rw-r--r-- | core/Period.php | 9 | ||||
-rw-r--r-- | core/Version.php | 2 |
4 files changed, 101 insertions, 20 deletions
diff --git a/core/Date.php b/core/Date.php index bac8ce7db2..cbbe866708 100644 --- a/core/Date.php +++ b/core/Date.php @@ -11,6 +11,8 @@ namespace Piwik; use Exception; use Piwik\Container\StaticContainer; +use Piwik\Intl\Data\Provider\DateTimeFormatProvider; +use Piwik\Plugins\LanguagesManager\LanguagesManager; /** * Utility class that wraps date/time related PHP functions. Using this class can @@ -41,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. @@ -622,10 +624,9 @@ class Date { $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'); + + $template = $dateTimeFormatProvider->getFormatPattern($template); $tokens = self::parseFormat($template); diff --git a/core/Intl/Data/Provider/DateTimeFormatProvider.php b/core/Intl/Data/Provider/DateTimeFormatProvider.php new file mode 100644 index 0000000000..90ffad609f --- /dev/null +++ b/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'; + } +} diff --git a/core/Period.php b/core/Period.php index 70a47d2905..aecf6362bf 100644 --- a/core/Period.php +++ b/core/Period.php @@ -371,12 +371,9 @@ abstract class Period $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); } /** diff --git a/core/Version.php b/core/Version.php index ee5bad9caa..77b3d42f1c 100644 --- a/core/Version.php +++ b/core/Version.php @@ -20,7 +20,7 @@ final class Version * The current Piwik version. * @var string */ - const VERSION = '2.15.0'; + const VERSION = '2.15.1-b1'; public function isStableVersion($version) { |