-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created LocalDateTime util in order to replace toLocalDateTime
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |