Ver Fonte

Merge branch 'fixTimeZoneBC' into 1.0.x

* fixTimeZoneBC:
  Old mongoDate were always based on +0000 TZ, new ones use date.timezone
Andreas Braun há 9 anos atrás
pai
commit
85f223685c

+ 2 - 0
lib/Mongo/MongoDate.php

@@ -95,6 +95,8 @@ class MongoDate implements TypeInterface
             $datetime = \DateTime::createFromFormat('Y-m-d H:i:s.u', $datetime->format('Y-m-d H:i:s') . '.' . $microSeconds);
         }
 
+        $datetime->setTimezone(new \DateTimeZone("UTC"));
+
         return $datetime;
     }
 

+ 18 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDateTest.php

@@ -10,6 +10,24 @@ use Alcaeus\MongoDbAdapter\TypeInterface;
  */
 class MongoDateTest extends TestCase
 {
+    public function testTimeZoneDoesNotAlterReturnedDateTime()
+    {
+        $initialTZ = ini_get("date.timezone");
+
+        ini_set("date.timezone", "UTC");
+
+        // Today at 8h 8m 8s
+        $timestamp = mktime (8, 8, 8); $date = new \MongoDate($timestamp);
+
+        $this->assertSame('08:08:08', $date->toDateTime()->format("H:i:s"));
+
+        ini_set("date.timezone", "Europe/Paris");
+
+        $this->assertSame('08:08:08', $date->toDateTime()->format("H:i:s"));
+
+        ini_set("date.timezone", $initialTZ);
+    }
+
     public function testCreate()
     {
         $date = new \MongoDate(1234567890, 123456);