Skip to content

Add millisecond support for Date mapping type #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/Doctrine/ODM/MongoDB/Types/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public function convertToDatabaseValue($value)
if ($value instanceof \MongoDate) {
return $value;
}
$timestamp = false;
if ($value instanceof \DateTime) {
$timestamp = $value->format('U');
} elseif (is_numeric($value)) {
return new \MongoDate($value->format('U'), $value->format('u'));
}
$timestamp = false;
if (is_numeric($value)) {
$timestamp = $value;
} elseif (is_string($value)) {
$timestamp = strtotime($value);
Expand All @@ -58,6 +59,7 @@ public function convertToPHPValue($value)
if ($value instanceof \MongoDate) {
$date = new \DateTime();
$date->setTimestamp($value->sec);
$date = \DateTime::createFromFormat('Y-m-d H:i:s.u', sprintf('%s.%06d', $date->format('Y-m-d H:i:s'), $value->usec));
} elseif (is_numeric($value)) {
$date = new \DateTime();
$date->setTimestamp($value);
Expand All @@ -71,11 +73,11 @@ public function convertToPHPValue($value)

public function closureToMongo()
{
return 'if ($value instanceof \DateTime) { $value = $value->getTimestamp(); } elseif (is_string($value)) { $value = strtotime($value); } $return = new \MongoDate($value);';
return 'if ($value instanceof \DateTime) { $return = new \MongoDate($value->format(\'U\'), $value->format(\'u\')); } elseif (is_string($value)) { $return = new \MongoDate(strtotime($value)); } else { $return = new \MongoDate($value); }';
}

public function closureToPHP()
{
return 'if ($value instanceof \MongoDate) { $return = new \DateTime(); $return->setTimestamp($value->sec); } elseif (is_numeric($value)) { $return = new \DateTime(); $return->setTimestamp($value); } elseif ($value instanceof \DateTime) { $return = $value; } else { $return = new \DateTime($value); }';
return 'if ($value instanceof \MongoDate) { $return = new \DateTime(); $return->setTimestamp($value->sec); $return = \DateTime::createFromFormat(\'Y-m-d H:i:s.u\', sprintf(\'%s.%06d\', $return->format(\'Y-m-d H:i:s\'), $value->usec)); } elseif (is_numeric($value)) { $return = new \DateTime(); $return->setTimestamp($value); } elseif ($value instanceof \DateTime) { $return = $value; } else { $return = new \DateTime($value); }';
}
}
29 changes: 29 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Types/DateTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Doctrine\ODM\MongoDB\Tests\Types;

use Doctrine\ODM\MongoDB\Types\Type;

class DateTypeTest extends \PHPUnit_Framework_TestCase
{
public function testConvertToDatabaseValue()
{
$type = Type::getType('date');

$date = \DateTime::createFromFormat('U.u', '1423743340.626000'); // "seconds.microseconds"

$this->assertNull($type->convertToDatabaseValue(null), 'null is not converted');
$this->assertEquals(1423743340, $type->convertToDatabaseValue($date)->sec);
$this->assertEquals(626000, $type->convertToDatabaseValue($date)->usec);
}

public function testConvertToPHPValue()
{
$type = Type::getType('date');

$date = new \MongoDate(1423743340, 626000);

$this->assertNull($type->convertToPHPValue(null), 'null is not converted');
$this->assertEquals('1423743340.626000', $type->convertToPHPValue($date)->format('U.u'));
}
}