mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-05 18:52:44 +01:00
working on conversion of datetime to and from UTC
This commit is contained in:
parent
4e126b03f0
commit
d3f2c4a571
40
app/Utils/Traits/MakesTimezoneAdjustment.php
Normal file
40
app/Utils/Traits/MakesTimezoneAdjustment.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils\Traits;
|
||||
|
||||
/**
|
||||
* Class MakesTimezoneAdjustment
|
||||
* @package App\Utils\Traits
|
||||
*/
|
||||
trait MakesTimezoneAdjustment
|
||||
{
|
||||
|
||||
public function createClientDate($utc_date , $timezone)
|
||||
{
|
||||
|
||||
$userTimezone = new \DateTimeZone($timezone);
|
||||
$gmtTimezone = new \DateTimeZone('GMT');
|
||||
//$myDateTime = new \DateTime($utc_date, $gmtTimezone);
|
||||
$offset = $userTimezone->getOffset($utc_date);
|
||||
$myInterval = \DateInterval::createFromDateString((string)$offset . 'seconds');
|
||||
$utc_date->add($myInterval);
|
||||
|
||||
return $utc_date;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function createUtcDate($client_date, $timezone)
|
||||
{
|
||||
|
||||
$userTimezone = new \DateTimeZone($timezone);
|
||||
$gmtTimezone = new \DateTimeZone('GMT');
|
||||
//$clientDateTime = new \DateTime($client_date, $userTimezone);
|
||||
$offset = $userTimezone->getOffset($client_date);
|
||||
$myInterval = \DateInterval::createFromDateString((string)$offset . 'seconds');
|
||||
$client_date->add($myInterval);
|
||||
|
||||
return $client_date;
|
||||
}
|
||||
|
||||
}
|
42
tests/Unit/TimezoneTraitTest.php
Normal file
42
tests/Unit/TimezoneTraitTest.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Utils\Traits\MakesTimezoneAdjustment;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @covers App\Utils\Traits\MakesTimezoneAdjustment
|
||||
*/
|
||||
class TimezoneTraitTest extends TestCase
|
||||
{
|
||||
|
||||
use MakesTimezoneAdjustment;
|
||||
|
||||
|
||||
public function testConvertClientDateToUTCDateTimeTickOverNextDay()
|
||||
{
|
||||
|
||||
$date_src = '2007-04-19 22:59';
|
||||
$client_timezone = 'Europe/Amsterdam'; // +1 UTC
|
||||
$date_time = new \DateTime($date_src, new \DateTimeZone($client_timezone));
|
||||
|
||||
$utc_date = $this->createClientDate($date_time, $client_timezone);
|
||||
|
||||
$this->assertEquals($utc_date->format('Y-m-d'), '2007-04-19');
|
||||
}
|
||||
|
||||
public function testConvertClientDateToUTCDateTimeSameDay()
|
||||
{
|
||||
|
||||
$date_src = '2007-04-19 21:59';
|
||||
$client_timezone = 'Europe/Amsterdam'; // +1 UTC
|
||||
$date_time = new \DateTime($date_src, new \DateTimeZone($client_timezone));
|
||||
|
||||
$utc_date = $this->createUtcDate($date_time, $client_timezone);
|
||||
|
||||
$this->assertEquals($utc_date->format('Y-m-d'), '2007-04-19');
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user