Skip to content

Commit

Permalink
Created LocalDateTime util in order to replace toLocalDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
rplansky committed Mar 23, 2017
1 parent 18647f2 commit a327ec2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Mongolid/Util/LocalDateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Mongolid\Util;

use DateTime;
use DateTimeZone;
use MongoDB\BSON\UTCDateTime;

/**
* This class is responsible to convert UTCDateTime from MongoDB driver in
* local date time.
*
* It will be unnecessary once MongoDB driver fixes a know issue in UTCDateTime.
* The proposal was created, for more information:
*
* @see https://jira.mongodb.org/browse/PHPC-760
*/
class LocalDateTime
{
/**
* Retrieves DateTime instance using default timezone
*
* @param UTCDateTime $date
*
* @return DateTime
*/
public static function get(UTCDateTime $date): DateTime
{
return new DateTime(
$date->toDateTime()->format('Y-m-d H:i:s'),
new DateTimeZone(date_default_timezone_get())
);
}
}
23 changes: 23 additions & 0 deletions tests/Mongolid/Util/LocalDateTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Mongolid\Util;

use DateTime;
use MongoDB\BSON\UTCDateTime;
use PHPUnit_Framework_TestCase as TestCase;

class LocalDateTimeTest extends TestCase
{
public function testGetShouldRetrievesDateUsingTimezone()
{
$format = 'd/m/Y H:i:s';
$date = new DateTime('01/05/2017 15:40:00');

date_default_timezone_set('Brazil/Acre');

$this->assertEquals(
$date->format($format),
LocalDateTime::get(new UTCDateTime($date))->format($format)
);
}
}

0 comments on commit a327ec2

Please sign in to comment.