Browse Source

Fixing issue when parsing dates and not using UTC as timezone.

Fixes #6095
Jose Lorenzo Rodriguez 11 years ago
parent
commit
137b04f77e
2 changed files with 18 additions and 1 deletions
  1. 3 1
      src/I18n/Time.php
  2. 15 0
      tests/TestCase/I18n/TimeTest.php

+ 3 - 1
src/I18n/Time.php

@@ -712,7 +712,9 @@ class Time extends Carbon implements JsonSerializable
         );
         $time = $formatter->parse($time);
         if ($time) {
-            return new static('@' . $time);
+            $result = new static('@' . $time);
+            $result->setTimezone(date_default_timezone_get());
+            return $result;
         }
         return null;
     }

+ 15 - 0
tests/TestCase/I18n/TimeTest.php

@@ -50,6 +50,7 @@ class TimeTest extends TestCase
         Time::setTestNow($this->now);
         Time::$defaultLocale = $this->locale;
         Time::resetToStringFormat();
+        date_default_timezone_set('UTC');
     }
 
     /**
@@ -733,6 +734,20 @@ class TimeTest extends TestCase
     }
 
     /**
+     * Tests that parsing a date respects de default timezone in PHP.
+     *
+     * @return void
+     */
+    public function testParseDateDifferentTimezone()
+    {
+        date_default_timezone_set('Europe/Paris');
+        Time::$defaultLocale = 'fr-FR';
+        $result = Time::parseDate('12/03/2015');
+        $this->assertEquals('2015-03-12', $result->format('Y-m-d'));
+        $this->assertEquals(new \DateTimeZone('Europe/Paris'), $result->tz);
+    }
+
+    /**
      * Custom assert to allow for variation in the version of the intl library, where
      * some translations contain a few extra commas.
      *