Merge pull request #18 from DavidBadura/fix-timezone

fix timezone problem
This commit is contained in:
David Badura 2015-04-22 14:55:43 +02:00
commit 0eb29ebf73
4 changed files with 43 additions and 15 deletions

View File

@ -70,6 +70,9 @@ class CarbonHandler implements SubscribingHandlerInterface
*/
public function serializeCarbon(VisitorInterface $visitor, Carbon $date, array $type, Context $context)
{
$date = clone $date;
$date->setTimezone($this->defaultTimezone);
return $visitor->visitString($date->format($this->getFormat($type)), $type, $context);
}
@ -94,6 +97,8 @@ class CarbonHandler implements SubscribingHandlerInterface
throw new RuntimeException(sprintf('Invalid datetime "%s", expected format %s.', $data, $format));
}
$datetime->setTimezone(new \DateTimeZone(date_default_timezone_get()));
return $datetime;
}

View File

@ -125,7 +125,7 @@ class Task
public function __construct()
{
$this->urgency = 0;
$this->entry = new Carbon('now', new \DateTimeZone('UTC'));
$this->entry = new Carbon('now');
$this->status = self::STATUS_PENDING;
}
@ -387,18 +387,15 @@ class Task
private function parseDateTime($date)
{
if ($date instanceof \DateTime) {
return new Carbon($date->format('Y-m-d H:i:s'), new \DateTimeZone('UTC'));
return new Carbon($date->format('Y-m-d H:i:s'));
}
if ($date instanceof Carbon) {
$date = clone $date;
$date->setTimezone(new \DateTimeZone('UTC'));
return $date;
}
if (is_string($date)) {
return new Carbon($date, new \DateTimeZone('UTC'));
return new Carbon($date);
}
if ($date === null) {
@ -414,7 +411,7 @@ class Task
public function __clone()
{
$this->uuid = null;
$this->entry = new Carbon('now', new \DateTimeZone('UTC'));
$this->entry = new Carbon('now');
$this->status = self::STATUS_PENDING;
}
}

View File

@ -260,9 +260,9 @@ class TaskManager
'project' => $task->getProject(),
'priority' => $task->getPriority(),
'tags' => $task->getTags(),
'due' => $task->getDue() ? $task->getDue()->format('Ymd\THis\Z') : null,
'wait' => $task->getWait() ? $task->getWait()->format('Ymd\THis\Z') : null,
'until' => $task->getUntil() ? $task->getUntil()->format('Ymd\THis\Z') : null
'due' => $this->transformDate($task->getDue()),
'wait' => $this->transformDate($task->getWait()),
'until' => $this->transformDate($task->getUntil())
];
if ($task->getRecurring()) {
@ -326,6 +326,22 @@ class TaskManager
$refProp->setValue($task, $value);
}
/**
* @param \DateTime $dateTime
* @return null|string
*/
private function transformDate(\DateTime $dateTime = null)
{
if (!$dateTime) {
return null;
}
$dateTime = clone $dateTime;
$dateTime->setTimezone(new \DateTimeZone('UTC'));
return $dateTime->format('Ymd\THis\Z');
}
/**
* @return Serializer
*/

View File

@ -23,10 +23,20 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
*/
protected $taskManager;
/**
* @var string
*/
protected $taskDir;
public function setUp()
{
$this->tearDown();
$this->taskwarrior = new Taskwarrior(__DIR__ . '/.taskrc', __DIR__ . '/.task');
$this->taskDir = __DIR__;
$fs = new Filesystem();
$fs->remove($this->taskDir . '/.taskrc');
$fs->remove($this->taskDir . '/.task');
$this->taskwarrior = new Taskwarrior($this->taskDir . '/.taskrc', $this->taskDir . '/.task');
$this->taskManager = new TaskManager($this->taskwarrior);
$this->taskwarrior->version(); // to initialise
}
@ -34,8 +44,8 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
public function tearDown()
{
$fs = new Filesystem();
$fs->remove(__DIR__ . '/.taskrc');
$fs->remove(__DIR__ . '/.task');
$fs->remove($this->taskDir . '/.taskrc');
$fs->remove($this->taskDir . '/.task');
}
public function testEmpty()
@ -707,6 +717,6 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
*/
private function createDateTime($string = 'now')
{
return new \DateTime($string, new \DateTimeZone('UTC'));
return new \Carbon\Carbon($string);
}
}