diff --git a/src/Serializer/Handler/CarbonHandler.php b/src/Serializer/Handler/CarbonHandler.php index 6233b14..bb0ad82 100644 --- a/src/Serializer/Handler/CarbonHandler.php +++ b/src/Serializer/Handler/CarbonHandler.php @@ -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; } diff --git a/src/Task.php b/src/Task.php index edcd274..72de2f5 100644 --- a/src/Task.php +++ b/src/Task.php @@ -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; } } \ No newline at end of file diff --git a/src/TaskManager.php b/src/TaskManager.php index b30c397..a8a92a7 100644 --- a/src/TaskManager.php +++ b/src/TaskManager.php @@ -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 */ diff --git a/tests/TaskManagerTest.php b/tests/TaskManagerTest.php index 65400a7..a95bcb8 100644 --- a/tests/TaskManagerTest.php +++ b/tests/TaskManagerTest.php @@ -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); } } \ No newline at end of file