diff options
author | sgiehl <stefan@piwik.org> | 2015-10-25 16:03:38 +0300 |
---|---|---|
committer | sgiehl <stefan@piwik.org> | 2015-10-28 00:31:59 +0300 |
commit | 53645fadd46ab0499ae558027f5a4789cbe79f35 (patch) | |
tree | d679cb7a338af5c16c7987dde44e396fad2639cb /core/Date.php | |
parent | 4c2da288d20341adebc953c1d0703c18fcb55487 (diff) |
code improvements / added tests
Diffstat (limited to 'core/Date.php')
-rw-r--r-- | core/Date.php | 57 |
1 files changed, 34 insertions, 23 deletions
diff --git a/core/Date.php b/core/Date.php index eac218eb9e..849f50973a 100644 --- a/core/Date.php +++ b/core/Date.php @@ -11,7 +11,7 @@ namespace Piwik; use Exception; use Piwik\Container\StaticContainer; -use Piwik\Plugins\LanguagesManager\Model as LanguagesManagerModel; +use Piwik\Plugins\LanguagesManager\LanguagesManager; /** * Utility class that wraps date/time related PHP functions. Using this class can @@ -612,6 +612,38 @@ class Date 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. @@ -629,30 +661,9 @@ class Date } if (strpos($template, '{time}') !== false) { - - static $use12HourClock = null; - - if (is_null($use12HourClock)) { - - $model = new LanguagesManagerModel(); - - $use12HourClock = $model->uses12HourClock(Piwik::getCurrentUserLogin()); - } - - $timeFormat = 'Intl_Format_Time_24'; - - if ($use12HourClock) { - $timeFormat = 'Intl_Format_Time_12'; - - } - - $translator = StaticContainer::get('Piwik\Translation\Translator'); - $replacement = $translator->translate($timeFormat); - - $template = str_replace('{time}', $replacement, $template); + $template = str_replace('{time}', $this->getTimeFormat(), $template); } - $tokens = self::parseFormat($template); $out = ''; |