diff --git a/core/Base/Date.class.php b/core/Base/Date.class.php index af60ef8678..57ce906b7b 100644 --- a/core/Base/Date.class.php +++ b/core/Base/Date.class.php @@ -118,7 +118,13 @@ public static function compare(Date $left, Date $right) public static function getWeekCountInYear($year) { - return date('W', mktime(0, 0, 0, 12, 31, $year)); + $weekCount = date('W', mktime(0, 0, 0, 12, 31, $year)); + + if ($weekCount == '01') { + return date('W', mktime(0, 0, 0, 12, 24, $year)); + } else { + return $weekCount; + } } public function __construct($date) diff --git a/test/core/DateTest.class.php b/test/core/DateTest.class.php index a484d68fab..b3efeef692 100644 --- a/test/core/DateTest.class.php +++ b/test/core/DateTest.class.php @@ -41,9 +41,9 @@ public function testDayDifference() $left = Timestamp::create('2008-03-29 03:00:00'); $right = Timestamp::create('2008-03-30 03:00:00'); - + $this->dayDifferenceTest($left, $right, 1); - + // unsolved giv's case // $left = Timestamp::create('2008-10-25 03:00:00'); // $right = Timestamp::create('2008-10-26 02:59:00'); @@ -67,6 +67,38 @@ public function testDateComparsion() return $this; } + /** + * @test + **/ + public function testWeekCount() + { + $weekCount = Date::getWeekCountInYear(2012); + $this->assertEquals($weekCount, 52, "Week count is incorrect"); + + $dateFromWeekNumberStamp = Date::makeFromWeek(5, 2012)->toStamp(); + $expectedDate = Date::create('2012-01-30 00:00:00')->toStamp(); + $this->assertEquals($dateFromWeekNumberStamp, $expectedDate, 'Creating date from week number is incorrect.'); + + $weekCount2009 = Date::getWeekCountInYear(2009); + $expectedCount2009 = 53; + $this->assertEquals($weekCount2009, $expectedCount2009, 'Week count for 2009 year is incorrect.'); + + $weekBegin2009Stamp = Date::makeFromWeek(53, 2009)->toStamp(); + $expectedDate2009 = Date::create('2009-12-28 00:00:00')->toStamp(); + $this->assertEquals($weekBegin2009Stamp, $expectedDate2009, 'Week 53 for 2009 starts with incorrect date.'); + + $this-> + assertEquals( + Date::makeFromWeek(1,2010)->toStamp(), + Date::create('2010-01-04')->toStamp(), + 'Week 1 for 2010 starts with incorrect date.' + ); + + $this->assertEquals(Date::getWeekCountInYear(1996), 52, 'Incorrect week count in 1996 year'); + + $this->assertEquals(Date::getWeekCountInYear(1976), 53, 'Incorrect week count in 1976 year'); + } + public function testMakeFromWeek() { $this->makeFromWeekTest(Date::create('2009-12-28'));