Skip to content

Commit

Permalink
(fix) Datetime test
Browse files Browse the repository at this point in the history
  • Loading branch information
juvenn committed Mar 17, 2018
1 parent f5aeeb3 commit c9ef636
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/LeanCloud/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,8 @@ public static function formatDate($date) {
$utc = clone $date;
$utc->setTimezone(new \DateTimezone("UTC"));
$iso = $utc->format("Y-m-d\TH:i:s.u");
// chops 3 zeros of microseconds to comply with cloud date format
// Milliseconds precision is required for server to correctly parse time,
// thus we have to chop off last 3 microseconds to milliseconds.
$iso = substr($iso, 0, 23) . "Z";
return $iso;
}
Expand Down Expand Up @@ -652,7 +653,9 @@ public static function decode($value, $key) {

if ($type === "Date") {
// return time in default time zone
return new \DateTime($value["iso"]);
$date = new \DateTime($value["iso"]);
$date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
return $date;
}
if ($type === "Bytes") {
return Bytes::createFromBase64Data($value["base64"]);
Expand Down
8 changes: 6 additions & 2 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ public function testDecodeDate() {
$date = new DateTime();
$type = array("__type" => "Date",
"iso" => Client::formatDate($date));
$this->assertEquals($date, Client::decode($type, null));
$date2 = Client::decode($type, null);
$this->assertEquals($date->getTimestamp(),
$date2->getTimestamp());
}

public function testDecodeDateWithTimeZone() {
Expand All @@ -185,7 +187,9 @@ public function testDecodeDateWithTimeZone() {
$date = new DateTime("now", new DateTimeZone($zone));
$type = array("__type" => "Date",
"iso" => Client::formatDate($date));
$this->assertEquals($date, Client::decode($type, null));
$date2 = Client::decode($type, null);
$this->assertEquals($date->getTimestamp(),
$date2->getTimestamp());
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ public function testGetDateShouldReturnDateTime() {
$obj2 = new Object("TestObject", $obj->getObjectId());
$obj2->fetch();
$this->assertTrue($obj2->get("release") instanceof DateTime);
$this->assertEquals($obj->get("release"), $obj2->get("release"));
$this->assertEquals($obj->get("release")->getTimestamp(),
$obj2->get("release")->getTimestamp());

$obj2->destroy();
}
Expand Down
6 changes: 4 additions & 2 deletions test/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public function testSetPushTime() {
$time = new DateTime();
$push->setPushTime($time);
$out = $push->encode();
$this->assertEquals($time, new DateTime($out["push_time"]));
$time2 = new DateTime($out["push_time"]);
$this->assertEquals($time->getTimestamp(), $time2->getTimestamp());
}

public function testSetExpirationInterval() {
Expand All @@ -116,7 +117,8 @@ public function testSetExpirationTime() {
$date = new DateTime();
$push->setExpirationTime($date);
$out = $push->encode();
$this->assertEquals($date, new DateTime($out["expiration_time"]));
$date2 = new DateTime($out["expiration_time"]);
$this->assertEquals($date->getTimestamp(), $date2->getTimestamp());
}

public function testSetWhere() {
Expand Down

0 comments on commit c9ef636

Please sign in to comment.