Skip to content

Commit

Permalink
Merge pull request #172 from tatitati/master
Browse files Browse the repository at this point in the history
[Improvement] - ChronosInterval - Display interval in readable format
  • Loading branch information
markstory authored Jun 21, 2018
2 parents 5b9d0ba + c187b00 commit a0293d2
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 20 deletions.
68 changes: 62 additions & 6 deletions src/ChronosInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,72 @@ public function add(DateInterval $interval)
*/
public function __toString()
{
// equivalence
$oneMinuteInSeconds = 60;
$oneHourInSeconds = $oneMinuteInSeconds * 60;
$oneDayInSeconds = $oneHourInSeconds * 24;
$oneMonthInDays = 365 / 12;
$oneMonthInSeconds = $oneDayInSeconds * $oneMonthInDays;
$oneYearInSeconds = 12 * $oneMonthInSeconds;

// convert
$ySecs = $this->y * $oneYearInSeconds;
$mSecs = $this->m * $oneMonthInSeconds;
$dSecs = $this->d * $oneDayInSeconds;
$hSecs = $this->h * $oneHourInSeconds;
$iSecs = $this->i * $oneMinuteInSeconds;
$sSecs = $this->s;

$totalSecs = $ySecs + $mSecs + $dSecs + $hSecs + $iSecs + $sSecs;

$y = null;
$m = null;
$d = null;
$h = null;
$i = null;

// years
if ($totalSecs >= $oneYearInSeconds) {
$y = floor($totalSecs / $oneYearInSeconds);
$totalSecs = $totalSecs - $y * $oneYearInSeconds;
}

// months
if ($totalSecs >= $oneMonthInSeconds) {
$m = floor($totalSecs / $oneMonthInSeconds);
$totalSecs = $totalSecs - $m * $oneMonthInSeconds;
}

// days
if ($totalSecs >= $oneDayInSeconds) {
$d = floor($totalSecs / $oneDayInSeconds);
$totalSecs = $totalSecs - $d * $oneDayInSeconds;
}

// hours
if ($totalSecs >= $oneHourInSeconds) {
$h = floor($totalSecs / $oneHourInSeconds);
$totalSecs = $totalSecs - $h * $oneHourInSeconds;
}

// minutes
if ($totalSecs >= $oneMinuteInSeconds) {
$i = floor($totalSecs / $oneMinuteInSeconds);
$totalSecs = $totalSecs - $i * $oneMinuteInSeconds;
}

$s = $totalSecs;

$date = array_filter([
static::PERIOD_YEARS => $this->y,
static::PERIOD_MONTHS => $this->m,
static::PERIOD_DAYS => $this->d,
static::PERIOD_YEARS => $y,
static::PERIOD_MONTHS => $m,
static::PERIOD_DAYS => $d,
]);

$time = array_filter([
static::PERIOD_HOURS => $this->h,
static::PERIOD_MINUTES => $this->i,
static::PERIOD_SECONDS => $this->s,
static::PERIOD_HOURS => $h,
static::PERIOD_MINUTES => $i,
static::PERIOD_SECONDS => $s,
]);

$specString = static::PERIOD_PREFIX;
Expand Down
85 changes: 71 additions & 14 deletions tests/Interval/IntervalToStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,102 @@ public function testZeroInterval()
$this->assertEquals('PT0S', (string)$ci);
}

public function testNegativeArguments()
{
$ci = new ChronosInterval(1, -2, 0, 15);
$this->assertEquals('P1Y15D', (string)$ci, 'Negative arguments are not considered');
}

/**
* Date section
*/
public function testYearInterval()
{
$ci = new ChronosInterval();
$ci1 = new ChronosInterval(1);
$ci2 = new ChronosInterval(0, 12, 0, 0);
$ci3 = new ChronosInterval(0, 0, 0, 365);
$ci4 = new ChronosInterval(0, 0, 52, 1);

$this->assertEquals('P1Y', (string)$ci);
$this->assertEquals('P1Y', (string)$ci1);
$this->assertEquals('P1Y', (string)$ci2);
$this->assertEquals('P1Y', (string)$ci3);
$this->assertEquals('P1Y', (string)$ci4);
}

public function testMonthInterval()
{
$ci = new ChronosInterval(0, 1);
$this->assertEquals('P1M', (string)$ci);
$ci1 = new ChronosInterval(0, 1);
$ci2 = new ChronosInterval(0, 0, 0, 30, 10);
$ci3 = new ChronosInterval(0, 0, 4, 2, 10);

$this->assertEquals('P1M', (string)$ci1);
$this->assertEquals('P1M', (string)$ci2);
$this->assertEquals('P1M', (string)$ci3);
}

public function testWeekInterval()
{
$ci = new ChronosInterval(0, 0, 1);
$this->assertEquals('P7D', (string)$ci);
$ci1 = new ChronosInterval(0, 0, 1);
$ci2 = new ChronosInterval(0, 0, 0, 7);
$ci3 = new ChronosInterval(0, 0, 0, 0, 7 * 24);
$ci4 = new ChronosInterval(0, 0, 0, 0, 0, 7 * 24 * 60);

$this->assertEquals('P7D', (string)$ci1);
$this->assertEquals('P7D', (string)$ci2);
$this->assertEquals('P7D', (string)$ci3);
$this->assertEquals('P7D', (string)$ci4);
}

public function testDayInterval()
{
$ci = new ChronosInterval(0, 0, 0, 1);
$this->assertEquals('P1D', (string)$ci);
$ci1 = new ChronosInterval(0, 0, 0, 1);
$ci2 = new ChronosInterval(0, 0, 0, 0, 24);
$ci3 = new ChronosInterval(0, 0, 0, 0, 0, 24 * 60);
$ci4 = new ChronosInterval(0, 0, 0, 0, 0, 0, 24 * 60 * 60);

$this->assertEquals('P1D', (string)$ci1);
$this->assertEquals('P1D', (string)$ci2);
$this->assertEquals('P1D', (string)$ci3);
$this->assertEquals('P1D', (string)$ci4);
}

public function testMixedDateInterval()
{
$ci = new ChronosInterval(1, 2, 0, 3);
$this->assertEquals('P1Y2M3D', (string)$ci);
$ci1 = new ChronosInterval(1, 2, 0, 3);
$ci2 = new ChronosInterval(0, 14, 0, 3);
$this->assertEquals('P1Y2M3D', (string)$ci1);
$this->assertEquals('P1Y2M3D', (string)$ci2);
}

/**
* Time section
*/
public function testHourInterval()
{
$ci = new ChronosInterval(0, 0, 0, 0, 1);
$this->assertEquals('PT1H', (string)$ci);
$ci1 = new ChronosInterval(0, 0, 0, 0, 1);
$ci2 = new ChronosInterval(0, 0, 0, 0, 0, 60);
$ci3 = new ChronosInterval(0, 0, 0, 0, 0, 0, 3600);

$this->assertEquals('PT1H', (string)$ci1);
$this->assertEquals('PT1H', (string)$ci2);
$this->assertEquals('PT1H', (string)$ci3);
}

public function testMinuteInterval()
{
$ci = new ChronosInterval(0, 0, 0, 0, 0, 1);
$this->assertEquals('PT1M', (string)$ci);
$ci1 = new ChronosInterval(0, 0, 0, 0, 0, 1);
$ci2 = new ChronosInterval(0, 0, 0, 0, 0, 0, 60);

$this->assertEquals('PT1M', (string)$ci1);
$this->assertEquals('PT1M', (string)$ci2);
}

public function testSecondInterval()
{
$ci = new ChronosInterval(0, 0, 0, 0, 0, 0, 1);

$this->assertEquals('PT1S', (string)$ci);
}

Expand All @@ -79,10 +130,16 @@ public function testMixedTimeInterval()
$this->assertEquals('PT1H2M3S', (string)$ci);
}

/**
* Date and Time sections
*/
public function testMixedDateAndTimeInterval()
{
$ci = new ChronosInterval(1, 2, 0, 3, 4, 5, 6);
$this->assertEquals('P1Y2M3DT4H5M6S', (string)$ci);
$ci1 = new ChronosInterval(0, 0, 0, 0, 48, 120);
$ci2 = new ChronosInterval(0, 24, 0, 0, 48, 120);

$this->assertEquals('P2DT2H', (string)$ci1);
$this->assertEquals('P2Y2DT2H', (string)$ci2);
}

public function testCreatingInstanceEquals()
Expand Down

0 comments on commit a0293d2

Please sign in to comment.