Skip to content

Commit fbfc617

Browse files
committed
Improve TimeUnit calculation
1 parent ae1d315 commit fbfc617

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

Diff for: src/TimeUnits.php

+24-26
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,30 @@ final class TimeUnits
5555
*/
5656
public function __construct(TimeUnitInterface $unit)
5757
{
58-
$days = $unit->inDays();
59-
$years = $days->getAmount() / Days::DAYS_PER_YEAR;
60-
$this->years = floor($years);
61-
$result = round($years - $this->years, 4);
62-
63-
$days = $result * Days::DAYS_PER_YEAR;
64-
$month = $days / Days::DAYS_PER_MONTH;
65-
$this->months = floor($month);
66-
$result = round($month - $this->months, 4);
67-
68-
$days = $result * Days::DAYS_PER_MONTH;
69-
$weeks = $days / Days::DAYS_PER_WEEK;
70-
$this->weeks = floor($weeks);
71-
$result = round($weeks - $this->weeks, 4);
72-
73-
$days = $result * Days::DAYS_PER_WEEK;
74-
$result = round($days - floor($days), 4);
75-
$hours = $result * Hours::HOURS_PER_DAY;
76-
$this->hours = floor($hours);
77-
$result = round($hours - $this->hours, 4);
78-
79-
$this->days = floor($days);
80-
$minutes = $result * Minutes::MINUTES_PER_HOUR;
81-
$this->minutes = floor($minutes);
82-
$result = round($minutes - $this->minutes, 4);
83-
$this->seconds = $result * Seconds::SECONDS_PER_MINUTE;
58+
$seconds = $unit->inSeconds()->getAmount();
59+
$this->years = floor($seconds / Seconds::SECONDS_PER_YEAR);
60+
61+
$seconds -= $this->years * Seconds::SECONDS_PER_YEAR;
62+
$this->months = floor($seconds / Seconds::SECONDS_PER_MONTH);
63+
64+
$seconds -= $this->months * Seconds::SECONDS_PER_MONTH;
65+
$this->weeks = floor($seconds / Seconds::SECONDS_PER_WEEK);
66+
67+
$seconds -= $this->weeks * Seconds::SECONDS_PER_WEEK;
68+
$this->days = floor($seconds / Seconds::SECONDS_PER_DAY);
69+
70+
$seconds -= $this->days * Seconds::SECONDS_PER_DAY;
71+
$this->hours = floor($seconds / Seconds::SECONDS_PER_HOUR);
72+
73+
$seconds -= $this->hours * Seconds::SECONDS_PER_HOUR;
74+
$this->minutes = floor($seconds / Seconds::SECONDS_PER_MINUTE);
75+
76+
$this->seconds = round($seconds - ($this->minutes * Seconds::SECONDS_PER_MINUTE), 2);
77+
78+
if (abs($this->seconds - Seconds::SECONDS_PER_MINUTE) < 0.01) {
79+
$this->minutes++;
80+
$this->seconds = 0;
81+
}
8482
}
8583

8684
/**

Diff for: tests/TimeUnitsTest.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function testDateUnit()
1717
$this->assertTrue($unit->getWeeks()->equalsAmount(3));
1818
$this->assertTrue($unit->getDays()->equalsAmount(5));
1919
$this->assertTrue($unit->getHours()->equalsAmount(11));
20-
$this->assertTrue($unit->getMinutes()->equalsAmount(17));
21-
$this->assertTrue($unit->getSeconds()->equalsAmount(31.2));
20+
$this->assertTrue($unit->getMinutes()->equalsAmount(33));
21+
$this->assertTrue($unit->getSeconds()->equalsAmount(20));
2222
}
2323

2424
public function testDays()
@@ -30,8 +30,8 @@ public function testDays()
3030
$this->assertTrue($unit->getWeeks()->equalsAmount(0));
3131
$this->assertTrue($unit->getDays()->equalsAmount(2));
3232
$this->assertTrue($unit->getHours()->equalsAmount(7));
33-
$this->assertTrue($unit->getMinutes()->equalsAmount(9));
34-
$this->assertTrue($unit->getSeconds()->equalsAmount(15.84));
33+
$this->assertTrue($unit->getMinutes()->equalsAmount(12));
34+
$this->assertTrue($unit->getSeconds()->equalsAmount(0));
3535
}
3636

3737
public function testHours()
@@ -43,8 +43,8 @@ public function testHours()
4343
$this->assertTrue($unit->getWeeks()->equalsAmount(0));
4444
$this->assertTrue($unit->getDays()->equalsAmount(0));
4545
$this->assertTrue($unit->getHours()->equalsAmount(4));
46-
$this->assertTrue($unit->getMinutes()->equalsAmount(23));
47-
$this->assertTrue($unit->getSeconds()->equalsAmount(5.28));
46+
$this->assertTrue($unit->getMinutes()->equalsAmount(30));
47+
$this->assertTrue($unit->getSeconds()->equalsAmount(0));
4848
}
4949

5050
public function testMinutes()
@@ -56,8 +56,8 @@ public function testMinutes()
5656
$this->assertTrue($unit->getWeeks()->equalsAmount(0));
5757
$this->assertTrue($unit->getDays()->equalsAmount(0));
5858
$this->assertTrue($unit->getHours()->equalsAmount(8));
59-
$this->assertTrue($unit->getMinutes()->equalsAmount(46));
60-
$this->assertTrue($unit->getSeconds()->equalsAmount(10.56));
59+
$this->assertTrue($unit->getMinutes()->equalsAmount(45));
60+
$this->assertTrue($unit->getSeconds()->equalsAmount(0));
6161
}
6262

6363
public function testDateUnitDiff()
@@ -69,7 +69,7 @@ public function testDateUnitDiff()
6969
$this->assertTrue($unit->getWeeks()->equalsAmount(1));
7070
$this->assertTrue($unit->getDays()->equalsAmount(5));
7171
$this->assertTrue($unit->getHours()->equalsAmount(8));
72-
$this->assertTrue($unit->getMinutes()->equalsAmount(14));
73-
$this->assertTrue($unit->getSeconds()->equalsAmount(3.84));
72+
$this->assertTrue($unit->getMinutes()->equalsAmount(40));
73+
$this->assertTrue($unit->getSeconds()->equalsAmount(0));
7474
}
7575
}

0 commit comments

Comments
 (0)