Skip to content

Commit

Permalink
Output proper ISO-8601 strings (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 30, 2016
1 parent a70bbe7 commit c92782f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
4 changes: 2 additions & 2 deletions lib/DateTime/Shared.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public function localize($locale = DateTimeLocalizer::DEFAULT_LOCALE)
* If the format is {@link RFC822} or {@link RFC1123} and the time zone is equivalent to GMT,
* the offset `+0000` is replaced by `GMT` according to the specs.
*
* If the format is {@link ISO8601} and the time zone is equivalent to UTC, the offset `+0000`
* If the format is {@link ISO8601} and the time zone is equivalent to UTC, the offset `+00:00`
* is replaced by `Z` according to the specs.
*
* @param string $as
Expand All @@ -315,7 +315,7 @@ private function format_as($as)
return str_replace('+0000', 'GMT', $value);

case 'ISO8601':
return str_replace('+0000', 'Z', $value);
return str_replace('+00:00', 'Z', $value);

default:
return $value;
Expand Down
42 changes: 21 additions & 21 deletions lib/ImmutableDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
*
* $time = new ImmutableDateTime('now', 'Europe/Paris');
*
* echo $time; // 2013-02-03T21:03:45+0100
* echo $time; // 2013-02-03T21:03:45+01:00
* echo $time->utc; // 2013-02-03T20:03:45Z
* echo $time->local; // 2013-02-03T15:03:45-0500
* echo $time->utc->local; // 2013-02-03T15:03:45-0500
* echo $time->local; // 2013-02-03T15:03:45-05:00
* echo $time->utc->local; // 2013-02-03T15:03:45-05:00
* echo $time->utc->is_utc; // true
* echo $time->utc->is_local; // false
* echo $time->local->is_utc; // false
* echo $time->local->is_local; // true
* echo $time->is_dst; // false
*
* echo $time->as_rss; // Sun, 03 Feb 2013 21:03:45 +0100
* echo $time->as_rss; // Sun, 03 Feb 2013 21:03:45 +01:00
* echo $time->as_db; // 2013-02-03 21:03:45
*
* echo $time->as_time; // 21:03:45
Expand All @@ -50,17 +50,17 @@
* echo $time->is_monday; // false
* echo $time->is_saturday; // true
* echo $time->is_today; // true
* echo $time->tomorrow; // 2013-02-04T00:00:00+0100
* echo $time->tomorrow; // 2013-02-04T00:00:00+01:00
* echo $time->tomorrow->is_future // true
* echo $time->yesterday; // 2013-02-02T00:00:00+0100
* echo $time->yesterday; // 2013-02-02T00:00:00+01:00
* echo $time->yesterday->is_past // true
* echo $time->monday; // 2013-01-28T00:00:00+0100
* echo $time->sunday; // 2013-02-03T00:00:00+0100
* echo $time->monday; // 2013-01-28T00:00:00+01:00
* echo $time->sunday; // 2013-02-03T00:00:00+01:00
*
* echo $time->timestamp; // 1359921825
* echo $time; // 2013-02-03T21:03:45+0100
* echo $time; // 2013-02-03T21:03:45+01:00
* $time->timestamp += 3600 * 4;
* echo $time; // 2013-02-04T01:03:45+0100
* echo $time; // 2013-02-04T01:03:45+01:00
*
* echo $time->timezone; // Europe/Paris
* // or
Expand Down Expand Up @@ -113,17 +113,17 @@ class ImmutableDateTime extends \DateTimeImmutable implements \JsonSerializable,
/*
* The following constants need to be defined because they are only defined by \DateTime
*/
const ATOM = \DateTime::ATOM;
const COOKIE = \DateTime::COOKIE;
const RSS = \DateTime::RSS;
const ISO8601 = \DateTime::ISO8601;
const RFC822 = \DateTime::RFC822;
const RFC850 = \DateTime::RFC850;
const RFC1036 = \DateTime::RFC1036;
const RFC1123 = \DateTime::RFC1123;
const RFC2822 = \DateTime::RFC2822;
const RFC3339 = \DateTime::RFC3339;
const W3C = \DateTime::W3C;
const ATOM = MutableDateTime::ATOM;
const COOKIE = MutableDateTime::COOKIE;
const RSS = MutableDateTime::RSS;
const ISO8601 = MutableDateTime::ISO8601;
const RFC822 = MutableDateTime::RFC822;
const RFC850 = MutableDateTime::RFC850;
const RFC1036 = MutableDateTime::RFC1036;
const RFC1123 = MutableDateTime::RFC1123;
const RFC2822 = MutableDateTime::RFC2822;
const RFC3339 = MutableDateTime::RFC3339;
const W3C = MutableDateTime::W3C;

/**
* @inheritdoc
Expand Down
2 changes: 2 additions & 0 deletions lib/MutableDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class MutableDateTime extends \DateTime implements \JsonSerializable, DateTime
use DateTime\Shared;
use DateTime\Readers;

const ISO8601 = 'Y-m-d\TH:i:sP';

/**
* @inheritdoc
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/AbstractDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ public function test_format_as_iso8601()
public function test_format_as_iso8601_utc()
{
$now = $this->now()->utc;
$this->assertEquals(str_replace('+0000', 'Z', $now->format(ImmutableDateTime::ISO8601)), $now->format_as_iso8601());
$this->assertEquals(str_replace('+0000', 'Z', $now->format(\DateTime::ISO8601)), $now->format_as_iso8601());
}

public function test_get_as_iso8601()
Expand All @@ -690,7 +690,7 @@ public function test_get_as_iso8601()
public function test_as_iso8601_utc()
{
$now = $this->now()->utc;
$this->assertEquals(str_replace('+0000', 'Z', $now->format(ImmutableDateTime::ISO8601)), $now->as_iso8601);
$this->assertEquals(str_replace('+0000', 'Z', $now->format(\DateTime::ISO8601)), $now->as_iso8601);
}

public function test_format_as_rfc822()
Expand Down Expand Up @@ -901,7 +901,7 @@ public function test_compare()
public function test_json_serialize()
{
$date = $this->create("2014-10-23 13:50:10", "Europe/Paris");
$this->assertEquals('{"date":"2014-10-23T13:50:10+0200"}', json_encode([ 'date' => $date ]));
$this->assertEquals('{"date":"2014-10-23T13:50:10+02:00"}', json_encode([ 'date' => $date ]));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/MutableDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ public function test_set_timestamp()
public function test_set_zone()
{
$d = $this->create('1977-06-06T12:00:00', 'Asia/Tokyo');
$this->assertSame('1977-06-06T12:00:00+0900', (string) $d);
$this->assertSame('1977-06-06T12:00:00+09:00', (string) $d);
$d->timezone = 'Europe/Paris';
$this->assertSame('1977-06-06T05:00:00+0200', (string) $d);
$this->assertSame('1977-06-06T05:00:00+02:00', (string) $d);
}

/**
Expand Down

0 comments on commit c92782f

Please sign in to comment.