Skip to content

Commit bdd9fcc

Browse files
committed
Fix bug with precision not being correctly copied
1 parent 67a0909 commit bdd9fcc

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ All notable changes to `period` will be documented in this file
66

77
- initial release
88

9+
## 0.5.1 - 2019-01-14
10+
11+
- Fix bug with precision not being correctly copied
12+
913
## 0.5.0 - 2019-01-09
1014

1115
- Add boundary and precision support

src/Period.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,15 @@ public function gap(Period $period): ?Period
285285
if ($this->getIncludedStart() >= $period->getIncludedEnd()) {
286286
return static::make(
287287
$period->getIncludedEnd()->add($this->interval),
288-
$this->getIncludedStart()->sub($this->interval)
288+
$this->getIncludedStart()->sub($this->interval),
289+
$this->getPrecisionMask()
289290
);
290291
}
291292

292293
return static::make(
293294
$this->getIncludedEnd()->add($this->interval),
294-
$period->getIncludedStart()->sub($this->interval)
295+
$period->getIncludedStart()->sub($this->interval),
296+
$this->getPrecisionMask()
295297
);
296298
}
297299

@@ -316,7 +318,7 @@ public function overlapSingle(Period $period): ?Period
316318
return null;
317319
}
318320

319-
return static::make($start, $end);
321+
return static::make($start, $end, $this->getPrecisionMask());
320322
}
321323

322324
/**
@@ -363,6 +365,8 @@ public function overlapAll(Period ...$periods): Period
363365

364366
public function diffSingle(Period $period): PeriodCollection
365367
{
368+
$this->ensurePrecisionMatches($period);
369+
366370
$periodCollection = new PeriodCollection();
367371

368372
if (! $this->overlapsWith($period)) {
@@ -385,14 +389,16 @@ public function diffSingle(Period $period): PeriodCollection
385389
if ($overlap->getIncludedStart() > $start) {
386390
$periodCollection[] = static::make(
387391
$start,
388-
$overlap->getIncludedStart()->sub($this->interval)
392+
$overlap->getIncludedStart()->sub($this->interval),
393+
$this->getPrecisionMask()
389394
);
390395
}
391396

392397
if ($overlap->getIncludedEnd() < $end) {
393398
$periodCollection[] = static::make(
394399
$overlap->getIncludedEnd()->add($this->interval),
395-
$end
400+
$end,
401+
$this->getPrecisionMask()
396402
);
397403
}
398404

tests/PeriodTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use DateTimeImmutable;
77
use Spatie\Period\Period;
88
use PHPUnit\Framework\TestCase;
9+
use Spatie\Period\Precision;
910

1011
class PeriodTest extends TestCase
1112
{

tests/PrecisionTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,37 @@ public function precision_is_kept_when_testing_contains()
186186
$this->assertFalse($a->contains(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2018-02-01 00:00:00')));
187187
$this->assertFalse($a->contains(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2017-12-21 23:59:59')));
188188
}
189+
190+
/** @test */
191+
public function precision_is_kept_with_diff()
192+
{
193+
$a = Period::make('2018-01-05 00:00:00', '2018-01-10 00:00:00', Precision::MINUTE);
194+
$b = Period::make('2018-01-15 00:00:00', '2018-03-01 00:00:00', Precision::MINUTE);
195+
196+
[$diff] = $a->diff($b);
197+
198+
$this->assertEquals(Precision::MINUTE, $diff->getPrecisionMask());
199+
}
200+
201+
/** @test */
202+
public function precision_is_kept_with_overlap()
203+
{
204+
$a = Period::make('2018-01-05 00:00:00', '2018-01-10 00:00:00', Precision::MINUTE);
205+
$b = Period::make('2018-01-01 00:00:00', '2018-01-31 00:00:00', Precision::MINUTE);
206+
207+
[$diff] = $a->overlap($b);
208+
209+
$this->assertEquals(Precision::MINUTE, $diff->getPrecisionMask());
210+
}
211+
212+
/** @test */
213+
public function precision_is_kept_with_gap()
214+
{
215+
$a = Period::make('2018-01-05 00:00:00', '2018-01-10 00:00:00', Precision::MINUTE);
216+
$b = Period::make('2018-01-15 00:00:00', '2018-01-31 00:00:00', Precision::MINUTE);
217+
218+
$gap = $a->gap($b);
219+
220+
$this->assertEquals(Precision::MINUTE, $gap->getPrecisionMask());
221+
}
189222
}

0 commit comments

Comments
 (0)