8000 Add microseconds in period caster · symfony/symfony@9ad9cbe · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ad9cbe

Browse files
committed
Add microseconds in period caster
1 parent 660fecc commit 9ad9cbe

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/Symfony/Component/VarDumper/Caster/DateCaster.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub,
3232
;
3333

3434
$a = array();
35-
$a[$prefix.'date'] = new ConstStub($d->format('Y-m-d H:i:'.self::formatSeconds($d->format('s'), $d->format('u')).($location ? ' e (P)' : ' P')), $title);
35+
$a[$prefix.'date'] = new ConstStub(self::formatDateTime($d, $location ? ' e (P)' : ' P'), $title);
3636

3737
$stub->class .= $d->format(' @U');
3838

@@ -100,23 +100,28 @@ public static function castPeriod(\DatePeriod $p, array $a, Stub $stub, $isNeste
100100
);
101101
break;
102102
}
103-
$dates[] = sprintf('%s) %s', $i + 1, $d->format('Y-m-d H:i:s'));
103+
$dates[] = sprintf('%s) %s', $i + 1, self::formatDateTime($d));
104104
}
105105
}
106106

107107
$period = sprintf(
108108
'every %s, from %s (%s) %s',
109109
self::formatInterval($p->getDateInterval()),
110-
$p->getStartDate()->format('Y-m-d H:i:s'),
110+
self::formatDateTime($p->getStartDate()),
111111
$p->include_start_date ? 'included' : 'excluded',
112-
($end = $p->getEndDate()) ? 'to '.$end->format('Y-m-d H:i:s') : 'recurring '.$p->recurrences.' time/s'
112+
($end = $p->getEndDate()) ? 'to '.self::formatDateTime($end) : 'recurring '.$p->recurrences.' time/s'
113113
);
114114

115115
$p = array(Caster::PREFIX_VIRTUAL.'period' => new ConstStub($period, implode("\n", $dates)));
116116

117117
return $filter & Caster::EXCLUDE_VERBOSE ? $p : $p + $a;
118118
}
119119

120+
private static function formatDateTime(\DateTimeInterface $d, $extra = '')
121+
{
122+
return $d->format('Y-m-d H:i:'.self::formatSeconds($d->format('s'), $d->format('u')).$extra);
123+
}
124+
120125
private static function formatSeconds($s, $us)
121126
{
122127
return sprintf('%02d.%s', $s, 0 === ($len = strlen($t = rtrim($us, '0'))) ? '0' : ($len <= 3 ? str_pad($t, 3, '0') : $us));

src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,28 +371,30 @@ public function providePeriods()
371371
{
372372
$i = new \DateInterval('PT0S');
373373
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';
374+
$h0 = "00:00:00$ms";
375+
$h1 = "01:00:00$ms";
374376

375377
$periods = array(
376-
array('2017-01-01', 'P1D', '2017-01-03', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-03 00:00:00', '1) 2017-01-01%a2) 2017-01-02'),
377-
array('2017-01-01', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 2 time/s', '1) 2017-01-01%a2) 2017-01-02'),
378+
array('20 E30A 17-01-01', 'P1D', '2017-01-03', 0, "every + 1d, from 2017-01-01 $h0 (included) to 2017-01-03 $h0", "1) 2017-01-01 $h0%a2) 2017-01-02 $h0"),
379+
array('2017-01-01', 'P1D', 1, 0, "every + 1d, from 2017-01-01 $h0 (included) recurring 2 time/s", "1) 2017-01-01 $h0%a2) 2017-01-02 $h0"),
378380

379-
array('2017-01-01', 'P1D', '2017-01-04', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-04 00:00:00', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
380-
array('2017-01-01', 'P1D', 2, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 3 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
381+
array('2017-01-01', 'P1D', '2017-01-04', 0, "every + 1d, from 2017-01-01 $h0 (included) to 2017-01-04 $h0", "1) 2017-01-01 $h0%a2) 2017-01-02 $h0%a3) 2017-01-03 $h0"),
382+
array('2017-01-01', 'P1D', 2, 0, "every + 1d, from 2017-01-01 $h0 (included) recurring 3 time/s", "1) 2017-01-01 $h0%a2) 2017-01-02 $h0%a3) 2017-01-03 $h0"),
381383

382-
array('2017-01-01', 'P1D', '2017-01-05', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-05 00:00:00', '1) 2017-01-01%a2) 2017-01-02%a1 more'),
383-
array('2017-01-01', 'P1D', 3, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 4 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03%a1 more'),
384+
array('2017-01-01', 'P1D', '2017-01-05', 0, "every + 1d, from 2017-01-01 $h0 (included) to 2017-01-05 $h0", "1) 2017-01-01 $h0%a2) 2017-01-02 $h0%a1 more"),
385+
array('2017-01-01', 'P1D', 3, 0, "every + 1d, from 2017-01-01 $h0 (included) recurring 4 time/s", "1) 2017-01-01 $h0%a2) 2017-01-02 $h0%a3) 2017-01-03 $h0%a1 more"),
384386

385-
array('2017-01-01', 'P1D', '2017-01-21', 0, 'every + 1d, from 2017-01-01 00:00:00 (included) to 2017-01-21 00:00:00', '1) 2017-01-01%a17 more'),
386-
array('2017-01-01', 'P1D', 19, 0, 'every + 1d, from 2017-01-01 00:00:00 (included) recurring 20 time/s', '1) 2017-01-01%a17 more'),
387+
array('2017-01-01', 'P1D', '2017-01-21', 0, "every + 1d, from 2017-01-01 $h0 (included) to 2017-01-21 $h0", "1) 2017-01-01 $h0%a17 more"),
388+
array('2017-01-01', 'P1D', 19, 0, "every + 1d, from 2017-01-01 $h0 (included) recurring 20 time/s", "1) 2017-01-01 $h0%a17 more"),
387389

388-
array('2017-01-01 01:00:00', 'P1D', '2017-01-03 01:00:00', 0, 'every + 1d, from 2017-01-01 01:00:00 (included) to 2017-01-03 01:00:00', '1) 2017-01-01 01:00:00%a2) 2017-01-02 01:00:00'),
389-
array('2017-01-01 01:00:00', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 01:00:00 (included) recurring 2 time/s', '1) 2017-01-01 01:00:00%a2) 2017-01-02 01:00:00'),
390+
array('2017-01-01 01:00:00', 'P1D', '2017-01-03 01:00:00', 0, "every + 1d, from 2017-01-01 $h1 (included) to 2017-01-03 $h1", "1) 2017-01-01 $h1%a2) 2017-01-02 $h1"),
391+
array('2017-01-01 01:00:00', 'P1D', 1, 0, "every + 1d, from 2017-01-01 $h1 (included) recurring 2 time/s", "1) 2017-01-01 $h1%a2) 2017-01-02 $h1"),
390392

391-
array('2017-01-01', 'P1DT1H', '2017-01-03', 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00 (included) to 2017-01-03 00:00:00", '1) 2017-01-01 00:00:00%a2) 2017-01-02 01:00:00'),
392-
array('2017-01-01', 'P1DT1H', 1, 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00 (included) recurring 2 time/s", '1) 2017-01-01 00:00:00%a2) 2017-01-02 01:00:00'),
393+
array('2017-01-01', 'P1DT1H', '2017-01-03', 0, "every + 1d $h1, from 2017-01-01 $h0 (included) to 2017-01-03 $h0", "1) 2017-01-01 $h0%a2) 2017-01-02 $h1"),
394+
array('2017-01-01', 'P1DT1H', 1, 0, "every + 1d $h1, from 2017-01-01 $h0 (included) recurring 2 time/s", "1) 2017-01-01 $h0%a2) 2017-01-02 $h1"),
393395

394-
array('2017-01-01', 'P1D', '2017-01-04', \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00 (excluded) to 2017-01-04 00:00:00', '1) 2017-01-02%a2) 2017-01-03'),
395-
array('2017-01-01', 'P1D', 2, \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00 (excluded) recurring 2 time/s', '1) 2017-01-02%a2) 2017-01-03'),
396+
array('2017-01-01', 'P1D', '2017-01-04', \DatePeriod::EXCLUDE_START_DATE, "every + 1d, from 2017-01-01 $h0 (excluded) to 2017-01-04 $h0", "1) 2017-01-02 $h0%a2) 2017-01-03 $h0"),
397+
array('2017-01-01', 'P1D', 2, \DatePeriod::EXCLUDE_START_DATE, "every + 1d, from 2017-01-01 $h0 (excluded) recurring 2 time/s", "1) 2017-01-02 $h0%a2) 2017-01-03 $h0"),
396398
);
397399

398400
if (\PHP_VERSION_ID < 70107) {

0 commit comments

Comments
 (0)
0