Skip to content

Commit

Permalink
Fix bug with precision not being correctly copied
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Jan 14, 2019
1 parent 67a0909 commit bdd9fcc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to `period` will be documented in this file

- initial release

## 0.5.1 - 2019-01-14

- Fix bug with precision not being correctly copied

## 0.5.0 - 2019-01-09

- Add boundary and precision support
Expand Down
16 changes: 11 additions & 5 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,15 @@ public function gap(Period $period): ?Period
if ($this->getIncludedStart() >= $period->getIncludedEnd()) {
return static::make(
$period->getIncludedEnd()->add($this->interval),
$this->getIncludedStart()->sub($this->interval)
$this->getIncludedStart()->sub($this->interval),
$this->getPrecisionMask()
);
}

return static::make(
$this->getIncludedEnd()->add($this->interval),
$period->getIncludedStart()->sub($this->interval)
$period->getIncludedStart()->sub($this->interval),
$this->getPrecisionMask()
);
}

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

return static::make($start, $end);
return static::make($start, $end, $this->getPrecisionMask());
}

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

public function diffSingle(Period $period): PeriodCollection
{
$this->ensurePrecisionMatches($period);

$periodCollection = new PeriodCollection();

if (! $this->overlapsWith($period)) {
Expand All @@ -385,14 +389,16 @@ public function diffSingle(Period $period): PeriodCollection
if ($overlap->getIncludedStart() > $start) {
$periodCollection[] = static::make(
$start,
$overlap->getIncludedStart()->sub($this->interval)
$overlap->getIncludedStart()->sub($this->interval),
$this->getPrecisionMask()
);
}

if ($overlap->getIncludedEnd() < $end) {
$periodCollection[] = static::make(
$overlap->getIncludedEnd()->add($this->interval),
$end
$end,
$this->getPrecisionMask()
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/PeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DateTimeImmutable;
use Spatie\Period\Period;
use PHPUnit\Framework\TestCase;
use Spatie\Period\Precision;

class PeriodTest extends TestCase
{
Expand Down
33 changes: 33 additions & 0 deletions tests/PrecisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,37 @@ public function precision_is_kept_when_testing_contains()
$this->assertFalse($a->contains(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2018-02-01 00:00:00')));
$this->assertFalse($a->contains(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2017-12-21 23:59:59')));
}

/** @test */
public function precision_is_kept_with_diff()
{
$a = Period::make('2018-01-05 00:00:00', '2018-01-10 00:00:00', Precision::MINUTE);
$b = Period::make('2018-01-15 00:00:00', '2018-03-01 00:00:00', Precision::MINUTE);

[$diff] = $a->diff($b);

$this->assertEquals(Precision::MINUTE, $diff->getPrecisionMask());
}

/** @test */
public function precision_is_kept_with_overlap()
{
$a = Period::make('2018-01-05 00:00:00', '2018-01-10 00:00:00', Precision::MINUTE);
$b = Period::make('2018-01-01 00:00:00', '2018-01-31 00:00:00', Precision::MINUTE);

[$diff] = $a->overlap($b);

$this->assertEquals(Precision::MINUTE, $diff->getPrecisionMask());
}

/** @test */
public function precision_is_kept_with_gap()
{
$a = Period::make('2018-01-05 00:00:00', '2018-01-10 00:00:00', Precision::MINUTE);
$b = Period::make('2018-01-15 00:00:00', '2018-01-31 00:00:00', Precision::MINUTE);

$gap = $a->gap($b);

$this->assertEquals(Precision::MINUTE, $gap->getPrecisionMask());
}
}

0 comments on commit bdd9fcc

Please sign in to comment.