Skip to content

Commit d952e47

Browse files
committed
Adding Interval::roundTo method
1 parent 8e05f97 commit d952e47

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All Notable changes to `bakame/tokei` will be documented in this file.
1414
- `IntervalSet::previous`
1515
- `IntervalSet::nearest`
1616
- `Interval::fromFormat`
17+
- `Interval::roundTo`
1718
- `Duration::fromDateInterval`
1819
- `Duration::fromFormat`
1920
- `Duration::format`

src/Interval.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ public function expand(Duration $duration): self
274274
return self::between($this->start->shift($duration->negated()), $this->end->shift($duration));
275275
}
276276

277+
public function roundTo(Unit $unit, SnapMode $mode = SnapMode::Nearest): self
278+
{
279+
$rounded = self::between($this->start->roundTo($unit, $mode), $this->end->roundTo($unit, $mode));
280+
281+
return $rounded->equals($this) ? $this : $rounded;
282+
}
283+
277284
/**
278285
* @throws InvalidDuration
279286
*/

src/IntervalTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,4 +987,20 @@ public function test_fixing_split_at_overflow(): void
987987
$splits->allFormatted(IntervalFormat::Iso80000)
988988
);
989989
}
990+
991+
public function test_roundto_interval(): void
992+
{
993+
$interval = Interval::between(
994+
Time::at(22, 13, 35),
995+
Time::at(3, 42, 27)
996+
);
997+
998+
self::assertSame($interval->roundTo(Unit::Second), $interval);
999+
1000+
$updated = $interval->roundTo(Unit::Minute);
1001+
self::assertNotEquals($updated, $interval);
1002+
1003+
self::assertTrue($updated->start->equals(Time::at(22, 14)));
1004+
self::assertTrue($updated->end->equals(Time::at(3, 42)));
1005+
}
9901006
}

0 commit comments

Comments
 (0)