]> source.dussan.org Git - nextcloud-server.git/commitdiff
Deprecate Util::formatDate()
authorJoas Schilling <nickvergessen@gmx.de>
Mon, 24 Nov 2014 15:37:04 +0000 (16:37 +0100)
committerJoas Schilling <nickvergessen@gmx.de>
Wed, 10 Dec 2014 10:58:56 +0000 (11:58 +0100)
Make DateTimeFormatter a service and adjust tests that have been inaccurate

lib/private/server.php
lib/private/template/functions.php
lib/private/util.php
lib/public/template.php
lib/public/util.php
tests/lib/template.php
tests/lib/util.php

index ce21980c532b4d80247cd31ed642147569702986..e0105506970e7e90cbb014d6be2796012eaaf704 100644 (file)
@@ -275,6 +275,12 @@ class Server extends SimpleContainer implements IServerContainer {
                        $groupManager = $c->getGroupManager();
                        return new \OC\App\AppManager($userSession, $appConfig, $groupManager);
                });
+               $this->registerService('DateTimeFormatter', function(Server $c) {
+                       $timeZone = $c->getTimeZone();
+                       $language = $c->getConfig()->getUserValue($c->getSession()->get('user_id'), 'core', 'lang', null);
+
+                       return new \OC\DateTimeFormatter($timeZone, $c->getL10N('lib', $language));
+               });
                $this->registerService('MountConfigManager', function () {
                        $loader = \OC\Files\Filesystem::getLoader();
                        return new \OC\Files\Config\MountProviderCollection($loader);
@@ -686,6 +692,31 @@ class Server extends SimpleContainer implements IServerContainer {
                return $this->webRoot;
        }
 
+       /**
+        * Get the timezone of the current user, based on his session information and config data
+        *
+        * @return \DateTimeZone
+        */
+       public function getTimeZone() {
+               $timeZone = $this->getConfig()->getUserValue($this->getSession()->get('user_id'), 'core', 'timezone', null);
+               if ($timeZone === null) {
+                       if ($this->getSession()->exists('timezone')) {
+                               $offsetHours = $this->getSession()->get('timezone');
+                               // Note: the timeZone name is the inverse to the offset,
+                               // so a positive offset means negative timeZone
+                               // and the other way around.
+                               if ($offsetHours > 0) {
+                                       return new \DateTimeZone('Etc/GMT-' . $offsetHours);
+                               } else {
+                                       return new \DateTimeZone('Etc/GMT+' . abs($offsetHours));
+                               }
+                       } else {
+                               return new \DateTimeZone('UTC');
+                       }
+               }
+               return new \DateTimeZone($timeZone);
+       }
+
        /**
         * @return \OCP\Files\Config\IMountProviderCollection
         */
index 4172d47ba611da7d11f68ccbdf052e15439855bb..288b7838ca2254887843135f44d5fdac8905cf66 100644 (file)
@@ -205,36 +205,16 @@ function strip_time($timestamp){
  * @param int $timestamp timestamp to format
  * @param int $fromTime timestamp to compare from, defaults to current time
  * @param bool $dateOnly whether to strip time information
- * @return OC_L10N_String timestamp
+ * @return string timestamp
  */
 function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
-       $l = \OC::$server->getL10N('lib');
-       if (!isset($fromTime) || $fromTime === null){
-               $fromTime = time();
-       }
+       /** @var \OC\DateTimeFormatter $formatter */
+       $formatter = \OC::$server->query('DateTimeFormatter');
+
        if ($dateOnly){
-               $fromTime = strip_time($fromTime);
-               $timestamp = strip_time($timestamp);
+               return $formatter->formatDateSpan($timestamp, $fromTime);
        }
-       $timediff = $fromTime - $timestamp;
-       $diffminutes = round($timediff/60);
-       $diffhours = round($diffminutes/60);
-       $diffdays = round($diffhours/24);
-       $diffmonths = round($diffdays/31);
-
-       if(!$dateOnly && $timediff < 60) { return $l->t('seconds ago'); }
-       else if(!$dateOnly && $timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); }
-       else if(!$dateOnly && $timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); }
-       else if((date('G', $fromTime)-$diffhours) >= 0) { return $l->t('today'); }
-       else if((date('G', $fromTime)-$diffhours) >= -24) { return $l->t('yesterday'); }
-       // 86400 * 31 days = 2678400
-       else if($timediff < 2678400) { return $l->n('%n day go', '%n days ago', $diffdays); }
-       // 86400 * 60 days = 518400
-       else if($timediff < 5184000) { return $l->t('last month'); }
-       else if((date('n', $fromTime)-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); }
-       // 86400 * 365.25 days * 2 = 63113852
-       else if($timediff < 63113852) { return $l->t('last year'); }
-       else { return $l->t('years ago'); }
+       return $formatter->formatTimeSpan($timestamp, $fromTime);
 }
 
 function html_select_options($options, $selected, $params=array()) {
index d28fa80160c6f72985c0dce8671cd9f50545edf7..6ccb9dba0870cd08e9c0f95a6af6f3bca27e24fb 100644 (file)
@@ -435,27 +435,20 @@ class OC_Util {
         * @param bool $dateOnly option to omit time from the result
         * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to
         * @return string timestamp
-        * @description adjust to clients timezone if we know it
+        *
+        * @deprecated Use \OC::$server->query('DateTimeFormatter') instead
         */
        public static function formatDate($timestamp, $dateOnly = false, $timeZone = null) {
-               if (is_null($timeZone)) {
-                       if (\OC::$server->getSession()->exists('timezone')) {
-                               $systemTimeZone = intval(date('O'));
-                               $systemTimeZone = (round($systemTimeZone / 100, 0) * 60) + ($systemTimeZone % 100);
-                               $clientTimeZone = \OC::$server->getSession()->get('timezone') * 60;
-                               $offset = $clientTimeZone - $systemTimeZone;
-                               $timestamp = $timestamp + $offset * 60;
-                       }
-               } else {
-                       if (!$timeZone instanceof DateTimeZone) {
-                               $timeZone = new DateTimeZone($timeZone);
-                       }
-                       $dt = new DateTime("@$timestamp");
-                       $offset = $timeZone->getOffset($dt);
-                       $timestamp += $offset;
+               if ($timeZone !== null && !$timeZone instanceof \DateTimeZone) {
+                       $timeZone = new \DateTimeZone($timeZone);
                }
-               $l = \OC::$server->getL10N('lib');
-               return $l->l($dateOnly ? 'date' : 'datetime', $timestamp);
+
+               /** @var \OC\DateTimeFormatter $formatter */
+               $formatter = \OC::$server->query('DateTimeFormatter');
+               if ($dateOnly) {
+                       return $formatter->formatDate($timestamp, 'long', $timeZone);
+               }
+               return $formatter->formatDateTime($timestamp, 'long', 'long', $timeZone);
        }
 
        /**
index 93af794ba62c638b3775d50c8146ae65d45742a8..89934a842edc07a03993a0b3ffd9368544dafe08 100644 (file)
@@ -94,6 +94,7 @@ function human_file_size( $bytes ) {
  * @param int $timestamp unix timestamp
  * @param boolean $dateOnly
  * @return \OC_L10N_String human readable interpretation of the timestamp
+ *
  * @deprecated Use \OCP\Template::relative_modified_date() instead
  */
 function relative_modified_date( $timestamp, $dateOnly = false ) {
@@ -188,12 +189,12 @@ class Template extends \OC_Template {
        }
 
        /**
-        * Return the relative date in relation to today. Returns something like "last hour" or "two month ago"
-        *
-        * @param int $timestamp unix timestamp
-        * @param boolean $dateOnly
-        * @return \OC_L10N_String human readable interpretation of the timestamp
-        */
+       * Return the relative date in relation to today. Returns something like "last hour" or "two month ago"
+       *
+       * @param int $timestamp unix timestamp
+       * @param boolean $dateOnly
+       * @return string human readable interpretation of the timestamp
+       */
        public static function relative_modified_date($timestamp, $dateOnly = false) {
                return \relative_modified_date($timestamp, null, $dateOnly);
        }
index 793a16c4d841c08035328deb94d406806314aed2..7f1974a421d108c01f65da951ee64df8b41f5b7c 100644 (file)
@@ -163,6 +163,8 @@ class Util {
         * @param bool $dateOnly option to omit time from the result
         * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to
         * @return string timestamp
+        *
+        * @deprecated Use \OC::$server->query('DateTimeFormatter') instead
         */
        public static function formatDate($timestamp, $dateOnly=false, $timeZone = null) {
                return(\OC_Util::formatDate($timestamp, $dateOnly, $timeZone));
index d77284a5bf263800ef9a0dd420d491b31d44a9f2..db58238eae8dcad26b8207a007be019503ae5ea5 100644 (file)
@@ -117,15 +117,15 @@ class Test_TemplateFunctions extends \Test\TestCase {
 
        public function testRelativeDateMonthsAgo(){
                $currentTime = 1380703592;
-               $elementTime = $currentTime - 86400 * 60;
+               $elementTime = $currentTime - 86400 * 65;
                $result = (string)relative_modified_date($elementTime, $currentTime, true);
 
                $this->assertEquals('2 months ago', $result);
 
-               $elementTime = $currentTime - 86400 * 65;
+               $elementTime = $currentTime - 86400 * 130;
                $result = (string)relative_modified_date($elementTime, $currentTime, true);
 
-               $this->assertEquals('2 months ago', $result);
+               $this->assertEquals('4 months ago', $result);
        }
 
        public function testRelativeDateLastYear(){
@@ -146,12 +146,12 @@ class Test_TemplateFunctions extends \Test\TestCase {
                $elementTime = $currentTime - 86400 * 365.25 * 2;
                $result = (string)relative_modified_date($elementTime, $currentTime, true);
 
-               $this->assertEquals('years ago', $result);
+               $this->assertEquals('years ago', $result);
 
                $elementTime = $currentTime - 86400 * 365.25 * 3;
                $result = (string)relative_modified_date($elementTime, $currentTime, true);
 
-               $this->assertEquals('years ago', $result);
+               $this->assertEquals('years ago', $result);
        }
 
        // ---------------------------------------------------------------------------
@@ -211,15 +211,15 @@ class Test_TemplateFunctions extends \Test\TestCase {
 
        public function testRelativeTimeMonthsAgo(){
                $currentTime = 1380703592;
-               $elementTime = $currentTime - 86400 * 60;
+               $elementTime = $currentTime - 86400 * 65;
                $result = (string)relative_modified_date($elementTime, $currentTime, false);
 
                $this->assertEquals('2 months ago', $result);
 
-               $elementTime = $currentTime - 86400 * 65;
+               $elementTime = $currentTime - 86400 * 130;
                $result = (string)relative_modified_date($elementTime, $currentTime, false);
 
-               $this->assertEquals('2 months ago', $result);
+               $this->assertEquals('4 months ago', $result);
        }
 
        public function testRelativeTimeLastYear(){
@@ -240,11 +240,11 @@ class Test_TemplateFunctions extends \Test\TestCase {
                $elementTime = $currentTime - 86400 * 365.25 * 2;
                $result = (string)relative_modified_date($elementTime, $currentTime, false);
 
-               $this->assertEquals('years ago', $result);
+               $this->assertEquals('years ago', $result);
 
                $elementTime = $currentTime - 86400 * 365.25 * 3;
                $result = (string)relative_modified_date($elementTime, $currentTime, false);
 
-               $this->assertEquals('years ago', $result);
+               $this->assertEquals('years ago', $result);
        }
 }
index 3bb4b47c09cb39cd4405cf2962ab40df07fb8f95..1a2cb09f38dab7bca3f45cd6cdc4709250b1a816 100644 (file)
@@ -41,7 +41,7 @@ class Test_Util extends \Test\TestCase {
                date_default_timezone_set("UTC");
 
                $result = OC_Util::formatDate(1350129205, false, 'Europe/Berlin');
-               $expected = 'October 13, 2012 at 1:53:25 PM GMT+0';
+               $expected = 'October 13, 2012 at 1:53:25 PM GMT+2';
                $this->assertEquals($expected, $result);
        }
 
@@ -55,10 +55,22 @@ class Test_Util extends \Test\TestCase {
        function testFormatDateWithTZFromSession() {
                date_default_timezone_set("UTC");
 
+               $oldDateTimeFormatter = \OC::$server->query('DateTimeFormatter');
                \OC::$server->getSession()->set('timezone', 3);
+               $newDateTimeFormatter = new \OC\DateTimeFormatter(\OC::$server->getTimeZone(), new \OC_L10N('lib', 'en'));
+               $this->setDateFormatter($newDateTimeFormatter);
+
                $result = OC_Util::formatDate(1350129205, false);
-               $expected = 'October 13, 2012 at 2:53:25 PM GMT+0';
+               $expected = 'October 13, 2012 at 2:53:25 PM GMT+3';
                $this->assertEquals($expected, $result);
+
+               $this->setDateFormatter($oldDateTimeFormatter);
+       }
+
+       protected function setDateFormatter($formatter) {
+               \OC::$server->registerService('DateTimeFormatter', function ($c) use ($formatter) {
+                       return $formatter;
+               });
        }
 
        function testCallRegister() {