2017-04-14 01:47:17 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\VarDumper\Caster;
|
|
|
|
|
|
|
|
use Symfony\Component\VarDumper\Cloner\Stub;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Casts DateTimeInterface related classes to array representation.
|
|
|
|
*
|
|
|
|
* @author Dany Maillard <danymaillard93b@gmail.com>
|
|
|
|
*/
|
|
|
|
class DateCaster
|
|
|
|
{
|
|
|
|
public static function castDateTime(\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, F j, Y')
|
2017-04-30 16:09:09 +01:00
|
|
|
."\n".$fromNow->format('%R').self::formatInterval($fromNow).' from now'
|
2017-04-14 01:47:17 +01:00
|
|
|
.($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
|
|
|
|
;
|
|
|
|
|
|
|
|
$a = array();
|
|
|
|
$a[$prefix.'date'] = new ConstStub($d->format('Y-m-d H:i:s.u '.($location ? 'e (P)' : 'P')), $title);
|
|
|
|
|
|
|
|
$stub->class .= $d->format(' @U');
|
|
|
|
|
|
|
|
return $a;
|
|
|
|
}
|
2017-04-30 16:09:09 +01:00
|
|
|
|
|
|
|
public static function castInterval(\DateInterval $interval, array $a, Stub $stub, $isNested, $filter)
|
|
|
|
{
|
|
|
|
$now = new \DateTimeImmutable();
|
|
|
|
$numberOfSeconds = $now->add($interval)->getTimestamp() - $now->getTimestamp();
|
|
|
|
$title = number_format($numberOfSeconds, 0, '.', ' ').'s';
|
|
|
|
|
|
|
|
$i = array(Caster::PREFIX_VIRTUAL.'interval' => new ConstStub(self::formatInterval($interval), $title));
|
|
|
|
|
|
|
|
return $filter & Caster::EXCLUDE_VERBOSE ? $i : $i + $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function formatInterval(\DateInterval $i)
|
|
|
|
{
|
|
|
|
$format = '%R '
|
|
|
|
.($i->y ? '%yy ' : '')
|
|
|
|
.($i->m ? '%mm ' : '')
|
|
|
|
.($i->d ? '%dd ' : '')
|
2017-07-21 13:15:00 +01:00
|
|
|
.($i->h || $i->i || $i->s || $i->f ? '%H:%I:%S.%F' : '')
|
2017-04-30 16:09:09 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
$format = '%R ' === $format ? '0s' : $format;
|
|
|
|
|
|
|
|
return $i->format(rtrim($format));
|
|
|
|
}
|
2017-04-14 01:47:17 +01:00
|
|
|
}
|