Skip to content

Instantly share code, notes, and snippets.

@nicolas-grekas
Created May 5, 2017 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolas-grekas/d645a474490b9b00a4f9a55b420ff8d2 to your computer and use it in GitHub Desktop.
Save nicolas-grekas/d645a474490b9b00a4f9a55b420ff8d2 to your computer and use it in GitHub Desktop.
date-caster.diff
diff --git a/src/Symfony/Component/VarDumper/Caster/DateCaster.php b/src/Symfony/Component/VarDumper/Caster/DateCaster.php
index 61d24b1..b078ecd 100644
--- a/src/Symfony/Component/VarDumper/Caster/DateCaster.php
+++ b/src/Symfony/Component/VarDumper/Caster/DateCaster.php
@@ -23,17 +23,15 @@ class DateCaster
public static function castDate(\DateTimeInterface $d, array $a, Stub $stub, $isNested, $filter)
{
$prefix = Caster::PREFIX_VIRTUAL;
+ $location = $d->getTimezone()->getLocation();
+ $fromNow = (new \DateTime())->diff($d);
+
+ $title = $d->format('l, j F Y')
+ ."\n".$fromNow->format('%R').(ltrim($fromNow->format('%yy %mm %dd %H:%I:%Ss'), ' 0ymd:s') ?: '0s').' from now'
+ .($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '');
$a = array();
- $a[$prefix.'date'] = new ConstStub(
- $d->format('Y-m-d H:i:s.u '.($d->getTimeZone()->getLocation() ? 'e (P)' : 'P')),
- sprintf(
- "literal: %s\nΔnow: %s\nDST: %s",
- $d->format('l, j F Y'),
- (new \DateTime())->diff($d)->format('%R %yy %mm %dd %H:%I:%S'),
- $d->format('I') ? 'yes' : 'no'
- )
- );
+ $a[$prefix.'date'] = new ConstStub($d->format('Y-m-d H:i:s.u '.($location ? 'e (P)' : 'P')), $title);
$stub->class .= $d->format(' @U');
diff --git a/src/Symfony/Component/VarDumper/Caster/StubCaster.php b/src/Symfony/Component/VarDumper/Caster/StubCaster.php
index f8b13af..2ec096c 100644
--- a/src/Symfony/Component/VarDumper/Caster/StubCaster.php
+++ b/src/Symfony/Component/VarDumper/Caster/StubCaster.php
@@ -35,8 +35,10 @@ class StubCaster
$stub->class = Stub::STRING_BINARY;
}
- return array();
+ $a = array();
}
+
+ return $a;
}
public static function castCutArray(CutArrayStub $c, array $a, Stub $stub, $isNested)
diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
index e2dea94..4d60b9a 100644
--- a/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
+++ b/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
@@ -12,6 +12,8 @@
namespace Symfony\Component\VarDumper\Tests\Caster;
use PHPUnit\Framework\TestCase;
+use Symfony\Component\VarDumper\Caster\DateCaster;
+use Symfony\Component\VarDumper\Cloner\Stub;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
/**
@@ -24,9 +26,9 @@ class DateCasterTest extends TestCase
/**
* @dataProvider provideDates
*/
- public function testCastDate($time, $timezone, $expexted)
+ public function testDumpDate($time, $timezone, $expected)
{
- if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && preg_match('/[\+|\-]\d{2}:\d{2}/', $timezone)) {
+ if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && preg_match('/[-+]\d{2}:\d{2}/', $timezone)) {
$this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
}
@@ -34,13 +36,47 @@ class DateCasterTest extends TestCase
$xDump = <<<EODUMP
DateTime @1493503200 {
- date: $expexted
+ date: $expected
}
EODUMP;
$this->assertDumpMatchesFormat($xDump, $date);
}
+ public function testCastDate()
+ {
+ $stub = new Stub();
+ $date = new \DateTime('2017-08-30 00:00:00.000000', new \DateTimeZone('Europe/Zurich'));
+ $cast = DateCaster::castDate($date, array('foo' => 'bar'), $stub, false, 0);
+
+ $xDump = <<<'EODUMP'
+array:1 [
+ "\x00~\x00date" => 2017-08-30 00:00:00.000000 Europe/Zurich (+02:00)
+]
+EODUMP;
+
+ $this->assertDumpMatchesFormat($xDump, $cast);
+
+ $xDump = <<<'EODUMP'
+Symfony\Component\VarDumper\Caster\ConstStub {
+ +type: "ref"
+ +class: "2017-08-30 00:00:00.000000 Europe/Zurich (+02:00)"
+ +value: """
+ Wednesday, 30 August 2017\n
+ +%a from now\n
+ DST On
+ """
+ +cut: 0
+ +handle: 0
+ +refCount: 0
+ +position: 0
+ +attr: []
+}
+EODUMP;
+
+ $this->assertDumpMatchesFormat($xDump, $cast["\0~\0date"]);
+ }
+
public function provideDates()
{
return array(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment