8000 Improve microseconds support in date caster · symfony/symfony@479b5d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 479b5d6

Browse files
committed
Improve microseconds support in date caster
1 parent 660fecc commit 479b5d6

File tree

2 files changed

+71
-56
lines changed

2 files changed

+71
-56
lines changed

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

Lines changed: 10 additions & 5 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

@@ -62,7 +62,7 @@ private static function formatInterval(\DateInterval $i)
6262
}
6363

6464
if (\PHP_VERSION_ID >= 70100 && isset($i->f)) {
65-
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, $i->f) : '';
65+
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, substr($i->f, 2)) : '';
6666
} else {
6767
$format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
6868
}
@@ -100,23 +100,28 @@ 8000 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: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,9 @@ public function provideDateTimes()
100100
/**
101101
* @dataProvider provideIntervals
102102
*/
103-
public function testDumpInterval($intervalSpec, $invert, $expected)
103+
public function testDumpInterval($intervalSpec, $ms, $invert, $expected)
104104
{
105-
$interval = new \DateInterval($intervalSpec);
106-
$interval->invert = $invert;
105+
$interval = $this->createInterval($intervalSpec, $ms, $invert);
107106

108107
$xDump = <<<EODUMP
109108
DateInterval {
@@ -117,10 +116,9 @@ public function testDumpInterval($intervalSpec, $invert, $expected)
117116
/**
118117
* @dataProvider provideIntervals
119118
*/
120-
public function testDumpIntervalExcludingVerbosity($intervalSpec, $invert, $expected)
119+
public function testDumpIntervalExcludingVerbosity($intervalSpec, $ms, $invert, $expected)
121120
{
122-
$interval = new \DateInterval($intervalSpec);
123-
$interval->invert = $invert;
121+
$interval = $this->createInterval($intervalSpec, $ms, $invert);
124122

125123
$xDump = <<<EODUMP
126124
DateInterval {
@@ -134,10 +132,9 @@ public function testDumpIntervalExcludingVerbosity($intervalSpec, $invert, $expe
134132
/**
135133
* @dataProvider provideIntervals
136134
*/
137-
public function testCastInterval($intervalSpec, $invert, $xInterval, $xSeconds)
135+
public function testCastInterval($intervalSpec, $ms, $invert, $xInterval, $xSeconds)
138136
{
139-
$interval = new \DateInterval($intervalSpec);
140-
$interval->invert = $invert;
137+
$interval = $this->createInterval($intervalSpec, $ms, $invert);
141138
$stub = new Stub();
142139

143140
$cast = DateCaster::castInterval($interval, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
@@ -167,40 +164,42 @@ public function testCastInterval($intervalSpec, $invert, $xInterval, $xSeconds)
167164
}
168165
EODUMP;
169166

170-
$this->assertDumpEquals($xDump, $cast["\0~\0interval"]);
167+
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0interval"]);
171168
}
172169

173170
public function provideIntervals()
174171
{
175172
$i = new \DateInterval('PT0S');
176-
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';
173+
$ms = ($withMs = \PHP_VERSION_ID >= 70100 && isset($i->f)) ? '.0' : '';
177174

178175
return array(
179-
array('PT0S', 0, '0s', '0s'),
180-
array('PT1S', 0, '+ 00:00:01'.$ms, '1s'),
181-
array('PT2M', 0, '+ 00:02:00'.$ms, '120s'),
182-
array('PT3H', 0, '+ 03:00:00'.$ms, '10 800s'),
183-
array('P4D', 0, '+ 4d', '345 600s'),
184-
array('P5M', 0, '+ 5m', null),
185-
array('P6Y', 0, '+ 6y', null),
186-
array('P1Y2M3DT4H5M6S', 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
187-
array('PT1M60S', 0, '+ 00:02:00'.$ms, null),
188-
array('PT1H60M', 0, '+ 02:00:00'.$ms, null),
189-
array('P1DT24H', 0, '+ 2d', null),
190-
array('P1M32D', 0, '+ 1m 32d', null),
191-
192-
array('PT0S', 1, '0s', '0s'),
193-
array('PT1S', 1, '- 00:00:01'.$ms, '-1s'),
194-
array('PT2M', 1, '- 00:02:00'.$ms, '-120s'),
195-
array('PT3H', 1, '- 03:00:00'.$ms, '-10 800s'),
196-
array('P4D', 1, '- 4d', '-345 600s'),
197-
array('P5M', 1, '- 5m', null),
198-
array('P6Y', 1, '- 6y', null),
199-
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms, null),
200-
array('PT1M60S', 1, '- 00:02:00'.$ms, null),
201-
array('PT1H60M', 1, '- 02:00:00'.$ms, null),
202-
array('P1DT24H', 1, '- 2d', null),
203-
array('P1M32D', 1, '- 1m 32d', null),
176+
array('PT0S', 0, 0, '0s', '0s'),
177+
array('PT0S', 0.1, 0, $withMs ? '+ 00:00:00.100' : '0s', '%is'),
178+
array('PT1S', 0, 0, '+ 00:00:01'.$ms, '1s'),
179+
array('PT2M', 0, 0, '+ 00:02:00'.$ms, '120s'),
180+
array('PT3H', 0, 0, '+ 03:00:00'.$ms, '10 800s'),
181+
array('P4D', 0, 0, '+ 4d', '345 600s'),
182+
array('P5M', 0, 0, '+ 5m', null),
183+
array('P6Y', 0, 0, '+ 6y', null),
184+
array('P1Y2M3DT4H5M6S', 0, 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
185+
array('PT1M60S', 0, 0, '+ 00:02:00'.$ms, null),
186+
array('PT1H60M', 0, 0, '+ 02:00:00'.$ms, null),
187+
array('P1DT24H', 0, 0, '+ 2d', null),
188+
array('P1M32D', 0, 0, '+ 1m 32d', null),
189+
190+
array('PT0S', 0, 1, '0s', '0s'),
191+
array('PT0S', 0.1, 1, $withMs ? '- 00:00:00.100' : '0s', '%is'),
192+
array('PT1S', 0, 1, '- 00:00:01'.$ms, '-1s'),
193+
array('PT2M', 0, 1, '- 00:02:00'.$ms, '-120s'),
194+
array('PT3H', 0, 1, '- 03:00:00'.$ms, '-10 800s'),
195+
array('P4D', 0, 1, '- 4d', '-345 600s'),
196+
array('P5M', 0, 1, '- 5m', null),
197+
array('P6Y', 0, 1, '- 6y', null),
198+
array('P1Y2M3DT4H5M6S', 0, 1, '- 1y 2m 3d 04:05:06'.$ms, null),
199+
array('PT1M60S', 0, 1, '- 00:02:00'.$ms, null),
200+
array('PT1H60M', 0, 1, '- 02:00:00'.$ms, null),
201+
array('P1DT24H', 0, 1, '- 2d', null),
202+
array('P1M32D', 0, 1, '- 1m 32d', null),
204203
);
205204
}
206205

@@ -349,7 +348,7 @@ public function testCastPeriod($start, $interval, $end, $options, $xPeriod, $xDa
349348
]
350349
EODUMP;
351350

352-
$this->assertDumpMatchesFormat($xDump, $cast);
351+
$this->assertDumpEquals($xDump, $cast);
353352

354353
$xDump = <<<EODUMP
355354
Symfony\Component\VarDumper\Caster\ConstStub {
@@ -373,26 +372,26 @@ public function providePeriods()
373372
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';
374373

375374
$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'),
375+
array('2017-01-01', 'P1D', '2017-01-03', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-03 00:00:00.0', '1) 2017-01-01%a2) 2017-01-02'),
376+
array('2017-01-01', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 2 time/s', '1) 2017-01-01%a2) 2017-01-02'),
378377

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'),
378+
array('2017-01-01', 'P1D', '2017-01-04', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-04 00:00:00.0', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
379+
array('2017-01-01', 'P1D', 2, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 3 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
381380

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'),
381+
array('2017-01-01', 'P1D', '2017-01-05', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-05 00:00:00.0', '1) 2017-01-01%a2) 2017-01-02%a1 more'),
382+
array('2017-01-01', 'P1D', 3, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 4 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03%a1 more'),
384383

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'),
384+
array('2017-01-01', 'P1D', '2017-01-21', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-21 00:00:00.0', '1) 2017-01-01%a17 more'),
385+
array('2017-01-01', 'P1D', 19, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 20 time/s', '1) 2017-01-01%a17 more'),
387386

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'),
387+
array('2017-01-01 01:00:00', 'P1D', '2017-01-03 01:00:00', 0, 'every + 1d, from 2017-01-01 01:00:00.0 (included) to 2017-01-03 01:00:00.0', '1) 2017-01-01 01:00:00.0%a2) 2017-01-02 01:00:00.0'),
388+
array('2017-01-01 01:00:00', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 01:00:00.0 (included) recurring 2 time/s', '1) 2017-01-01 01:00:00.0%a2) 2017-01-02 01:00:00.0'),
390389

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'),
390+
array('2017-01-01', 'P1DT1H', '2017-01-03', 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00.0 (included) to 2017-01-03 00:00:00.0", '1) 2017-01-01 00:00:00.0%a2) 2017-01-02 01:00:00.0'),
391+
array('2017-01-01', 'P1DT1H', 1, 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00.0 (included) recurring 2 time/s", '1) 2017-01-01 00:00:00.0%a2) 2017-01-02 01:00:00.0'),
393392

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'),
393+
array('2017-01-01', 'P1D', '2017-01-04', \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00.0 (excluded) to 2017-01-04 00:00:00.0', '1) 2017-01-02%a2) 2017-01-03'),
394+
array('2017-01-01', 'P1D', 2, \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00.0 (excluded) recurring 2 time/s', '1) 2017-01-02%a2) 2017-01-03'),
396395
);
397396

398397
if (\PHP_VERSION_ID < 70107) {
@@ -401,4 +400,15 @@ public function providePeriods()
401400

402401
return $periods;
403402
}
403+
404+
private function createInterval($intervalSpec, $ms, $invert)
405+
{
406+
$interval = new \DateInterval($intervalSpec);
407+
if (\PHP_VERSION_ID >= 70100 && isset($interval->f)) {
408+
$interval->f = $ms;
409+
}
410+
$interval->invert = $invert;
411+
412+
return $interval;
413+
}
404414
}

0 commit comments

Comments
 (0)
0