Skip to content

Commit

Permalink
Merge pull request #18 from garemoko/master
Browse files Browse the repository at this point in the history
Work around for DateTime::ISO8601 PHP bug
  • Loading branch information
bscSCORM committed Dec 16, 2014
2 parents 0a2b5e7 + baed84d commit d5a4b93
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public function getEtag() { return $this->etag; }
public function setTimestamp($value) {
if (isset($value)) {
if ($value instanceof \DateTime) {
$value = $value->format(\DateTime::ISO8601);
// Use format('c') instead of format(\DateTime::ISO8601) due to bug in format(\DateTime::ISO8601) that generates an invalid timestamp.
$value = $value->format('c');
}
elseif (is_string($value)) {
$value = $value;
Expand Down
3 changes: 2 additions & 1 deletion src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public function hasId() { return isset($this->id); }
public function setStored($value) {
if (isset($value)) {
if ($value instanceof \DateTime) {
$value = $value->format(\DateTime::ISO8601);
// Use format('c') instead of format(\DateTime::ISO8601) due to bug in format(\DateTime::ISO8601) that generates an invalid timestamp.
$value = $value->format('c');
}
elseif (is_string($value)) {
$value = $value;
Expand Down
3 changes: 2 additions & 1 deletion src/StatementBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ public function getContext() { return $this->context; }
public function setTimestamp($value) {
if (isset($value)) {
if ($value instanceof \DateTime) {
$value = $value->format(\DateTime::ISO8601);
// Use format('c') instead of format(\DateTime::ISO8601) due to bug in format(\DateTime::ISO8601) that generates an invalid timestamp.
$value = $value->format('c');
}
elseif (is_string($value)) {
$value = $value;
Expand Down
50 changes: 50 additions & 0 deletions tests/ISO8601Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
Copyright 2014 Rustici Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use TinCan\Statement;
use TinCan\State;

class ISO8601Test extends PHPUnit_Framework_TestCase {
public function testProperties() {
$str_datetime = '2014-12-15T19:16:05+00:00';
$str_datetime_tz = '2014-12-15T13:16:05-06:00';

$datetime = new \DateTime();
$datetime->setDate(2014, 12, 15);
$datetime->setTime(19, 16, 05);

$statement = new Statement();
$statement->setStored($datetime);
$statement->setTimestamp($datetime);

$document = new State();
$document->setTimestamp($datetime);

$this->assertEquals($statement->getStored(), $str_datetime, 'stored matches');
$this->assertEquals($statement->getTimestamp(), $str_datetime, 'timestamp matches');
$this->assertEquals($document->getTimestamp(), $str_datetime, 'document timestamp matches');

$datetime->setTimezone(new DateTimeZone('America/Chicago'));
$statement->setStored($datetime);
$statement->setTimestamp($datetime);
$document->setTimestamp($datetime);

$this->assertEquals($statement->getStored(), $str_datetime_tz, 'stored matches');
$this->assertEquals($statement->getTimestamp(), $str_datetime_tz, 'timestamp matches');
$this->assertEquals($document->getTimestamp(), $str_datetime_tz, 'document timestamp matches');
}
}

0 comments on commit d5a4b93

Please sign in to comment.