Skip to content

Commit

Permalink
remove ElapsedPeriod::ofPeriod()
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Nov 23, 2024
1 parent a0b4603 commit 13ff798
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
- `Innmind\TimeContinuum\PointInTime\Day::weekNumber()`
- `Innmind\TimeContinuum\PointInTime\Day::toString()`
- `Innmind\TimeContinuum\ElapsedPeriod::maybe()`
- `Innmind\TimeContinuum\ElapsedPeriod::ofPeriod()`

## 3.4.1 - 2023-09-17

Expand Down
22 changes: 0 additions & 22 deletions src/ElapsedPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,6 @@ public static function of(int $microseconds): self
return new self($microseconds);
}

/**
* @psalm-pure
*
* @throws \LogicException When using a period containing months or years
*/
public static function ofPeriod(Period $period): self
{
if ($period->months() !== 0 || $period->years() !== 0) {
// a month or a year is not constant
throw new \LogicException('Months and years can not be converted to microseconds');
}

$milliseconds = Period\Value::day->milliseconds($period->days()) +
Period\Value::hour->milliseconds($period->hours()) +
Period\Value::minute->milliseconds($period->minutes()) +
Period\Value::second->milliseconds($period->seconds()) +
$period->milliseconds();
$milliseconds *= 1_000;

return new self($milliseconds + $period->microseconds());
}

/**
* @return int<0, max>
*/
Expand Down
17 changes: 16 additions & 1 deletion src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,23 @@ public function add(self $period): self
);
}

/**
* @throws \LogicException When using a period containing months or years
*/
public function asElapsedPeriod(): ElapsedPeriod
{
return ElapsedPeriod::ofPeriod($this);
if ($this->months() !== 0 || $this->years() !== 0) {
// a month or a year is not constant
throw new \LogicException('Months and years can not be converted to microseconds');
}

$milliseconds = Period\Value::day->milliseconds($this->days()) +
Period\Value::hour->milliseconds($this->hours()) +
Period\Value::minute->milliseconds($this->minutes()) +
Period\Value::second->milliseconds($this->seconds()) +
$this->milliseconds();
$milliseconds *= 1_000;

return ElapsedPeriod::of($milliseconds + $this->microseconds());
}
}
4 changes: 2 additions & 2 deletions tests/ElapsedPeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public function testThrowWhenTryingToBuildFromYearPeriod()
{
$this->expectException(\LogicException::class);

ElapsedPeriod::ofPeriod(Period::year(1));
Period::year(1)->asElapsedPeriod();
}

public function testThrowWhenTryingToBuildFromMonthPeriod()
{
$this->expectException(\LogicException::class);

ElapsedPeriod::ofPeriod(Period::month(1));
Period::month(1)->asElapsedPeriod();
}
}

0 comments on commit 13ff798

Please sign in to comment.