Skip to content

Commit d5a4b93

Browse files
committed
Merge pull request #18 from garemoko/master
Work around for DateTime::ISO8601 PHP bug
2 parents 0a2b5e7 + baed84d commit d5a4b93

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

src/Document.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public function getEtag() { return $this->etag; }
4848
public function setTimestamp($value) {
4949
if (isset($value)) {
5050
if ($value instanceof \DateTime) {
51-
$value = $value->format(\DateTime::ISO8601);
51+
// Use format('c') instead of format(\DateTime::ISO8601) due to bug in format(\DateTime::ISO8601) that generates an invalid timestamp.
52+
$value = $value->format('c');
5253
}
5354
elseif (is_string($value)) {
5455
$value = $value;

src/Statement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public function hasId() { return isset($this->id); }
100100
public function setStored($value) {
101101
if (isset($value)) {
102102
if ($value instanceof \DateTime) {
103-
$value = $value->format(\DateTime::ISO8601);
103+
// Use format('c') instead of format(\DateTime::ISO8601) due to bug in format(\DateTime::ISO8601) that generates an invalid timestamp.
104+
$value = $value->format('c');
104105
}
105106
elseif (is_string($value)) {
106107
$value = $value;

src/StatementBase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ public function getContext() { return $this->context; }
158158
public function setTimestamp($value) {
159159
if (isset($value)) {
160160
if ($value instanceof \DateTime) {
161-
$value = $value->format(\DateTime::ISO8601);
161+
// Use format('c') instead of format(\DateTime::ISO8601) due to bug in format(\DateTime::ISO8601) that generates an invalid timestamp.
162+
$value = $value->format('c');
162163
}
163164
elseif (is_string($value)) {
164165
$value = $value;

tests/ISO8601Test.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/*
3+
Copyright 2014 Rustici Software
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
use TinCan\Statement;
19+
use TinCan\State;
20+
21+
class ISO8601Test extends PHPUnit_Framework_TestCase {
22+
public function testProperties() {
23+
$str_datetime = '2014-12-15T19:16:05+00:00';
24+
$str_datetime_tz = '2014-12-15T13:16:05-06:00';
25+
26+
$datetime = new \DateTime();
27+
$datetime->setDate(2014, 12, 15);
28+
$datetime->setTime(19, 16, 05);
29+
30+
$statement = new Statement();
31+
$statement->setStored($datetime);
32+
$statement->setTimestamp($datetime);
33+
34+
$document = new State();
35+
$document->setTimestamp($datetime);
36+
37+
$this->assertEquals($statement->getStored(), $str_datetime, 'stored matches');
38+
$this->assertEquals($statement->getTimestamp(), $str_datetime, 'timestamp matches');
39+
$this->assertEquals($document->getTimestamp(), $str_datetime, 'document timestamp matches');
40+
41+
$datetime->setTimezone(new DateTimeZone('America/Chicago'));
42+
$statement->setStored($datetime);
43+
$statement->setTimestamp($datetime);
44+
$document->setTimestamp($datetime);
45+
46+
$this->assertEquals($statement->getStored(), $str_datetime_tz, 'stored matches');
47+
$this->assertEquals($statement->getTimestamp(), $str_datetime_tz, 'timestamp matches');
48+
$this->assertEquals($document->getTimestamp(), $str_datetime_tz, 'document timestamp matches');
49+
}
50+
}

0 commit comments

Comments
 (0)