1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

working on conversion of datetime to and from UTC

This commit is contained in:
David Bomba 2019-04-22 21:59:04 +10:00
parent 4e126b03f0
commit d3f2c4a571
2 changed files with 82 additions and 0 deletions

View 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;
}
}

View 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');
}
}