Skip to content

Commit

Permalink
refs matomo-org#1486 added method to detect whether current year is a…
Browse files Browse the repository at this point in the history
… leap year
  • Loading branch information
tsteur committed Jan 8, 2014
1 parent 44eef27 commit 32f40ab
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ public function isEarlier(Date $date)
return $this->getTimestamp() < $date->getTimestamp();
}

/**
* Returns `true` if the current year is a leap year, false otherwise.
*
* @return bool
*/
public function isLeapYear()
{
$currentYear = date('Y', $this->getTimestamp());

return ($currentYear % 400) == 0 || (($currentYear % 4) == 0 && ($currentYear % 100) != 0);
}

/**
* Converts this date to the requested string format. See {@link http://php.net/date}
* for the list of format strings.
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPUnit/Core/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,31 @@ public function testSubPeriod()
$date = $date->subPeriod(5, 'year');
$this->assertEquals($dateExpected->getTimestamp(), $date->getTimestamp());
}

/**
* @group Core
*/
public function testIsLeapYear()
{
$date = Date::factory('2011-03-01');
$this->assertFalse($date->isLeapYear());
$date = Date::factory('2011-01-01');
$this->assertFalse($date->isLeapYear());
$date = Date::factory('2011-01-31');
$this->assertFalse($date->isLeapYear());

$date = Date::factory('2012-01-01');
$this->assertTrue($date->isLeapYear());
$date = Date::factory('2012-12-31');
$this->assertTrue($date->isLeapYear());

$date = Date::factory('2013-01-01');
$this->assertFalse($date->isLeapYear());
$date = Date::factory('2013-12-31');
$this->assertFalse($date->isLeapYear());

$date = Date::factory('2052-01-01');
$this->assertTrue($date->isLeapYear());

}
}

0 comments on commit 32f40ab

Please sign in to comment.