Skip to content
Permalink
Browse files

Merge pull request #1438 from albe/json-datetime-fix

BUGFIX: Fix format for json_encoded DateTime conversion
  • Loading branch information...
jonnitto committed Nov 25, 2018
2 parents e761859 + b5c1307 commit a00a7512f50a3806857223e8d61d69c86232cdb2
@@ -161,7 +161,7 @@ public function convertFrom($source, $targetType, array $convertedChildPropertie
}
if (isset($source['timezone_type'])) {
// DateTime internal format when being serialized with json_encode
$dateFormat = "Y-m-d\TH:i:s.v";
$dateFormat = "Y-m-d H:i:s.u";
}
$date = $targetType::createFromFormat($dateFormat, $dateAsString, $timezone);
} else {
@@ -531,4 +531,17 @@ public function foo() { return "Bar"; }
$this->assertInstanceOf($className, $date);
$this->assertSame('Bar', $date->foo());
}
/**
* @test
*/
public function canConvertFromJsonSerializedDateTime()
{
$sourceDate = new \DateTime('2005-08-15T15:52:01+00:00');
// Serialize to an array with json_decode from an json_encoded string
$source = json_decode(json_encode($sourceDate), true);
$convertedDate = $this->converter->convertFrom($source, 'DateTime');
$this->assertInstanceOf('DateTime', $convertedDate);
$this->assertSame($sourceDate->getTimestamp(), $convertedDate->getTimestamp());
}
}

0 comments on commit a00a751

Please sign in to comment.
You can’t perform that action at this time.