diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2014-01-09 00:42:16 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2014-01-09 00:42:16 +0400 |
commit | 32f40ab9525256f1f7654d54f3307a4e9e1bc7c4 (patch) | |
tree | fc6fafeb3a494b042bbf2f0b1c9f818744aa580d | |
parent | 44eef274c25fa2563fed4a99bb3c01b9ce9a61ad (diff) |
refs #1486 added method to detect whether current year is a leap year
-rw-r--r-- | core/Date.php | 12 | ||||
-rw-r--r-- | tests/PHPUnit/Core/DateTest.php | 27 |
2 files changed, 39 insertions, 0 deletions
diff --git a/core/Date.php b/core/Date.php index 3b21ed186a..063420940c 100644 --- a/core/Date.php +++ b/core/Date.php @@ -290,6 +290,18 @@ class Date } /** + * 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. * diff --git a/tests/PHPUnit/Core/DateTest.php b/tests/PHPUnit/Core/DateTest.php index ad4252c1a4..28133e5ea1 100644 --- a/tests/PHPUnit/Core/DateTest.php +++ b/tests/PHPUnit/Core/DateTest.php @@ -238,4 +238,31 @@ class DateTest extends PHPUnit_Framework_TestCase $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()); + + } } |