From 56e5844c4b24cd611d1132c7668740363ecbdcb2 Mon Sep 17 00:00:00 2001 From: Sayapin Alexander Date: Mon, 5 Mar 2012 23:01:12 +0400 Subject: [PATCH 1/2] Fixed calculating number of weeks in year accordingly ISO 8601 --- core/Base/Date.class.php | 8 +++++++- test/core/DateTest.class.php | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/core/Base/Date.class.php b/core/Base/Date.class.php index af60ef8678..3cd3c2f5f6 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..b9712da5bd 100644 --- a/test/core/DateTest.class.php +++ b/test/core/DateTest.class.php @@ -43,6 +43,13 @@ public function testDayDifference() $right = Timestamp::create('2008-03-30 03:00:00'); $this->dayDifferenceTest($left, $right, 1); + + $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.'); // unsolved giv's case // $left = Timestamp::create('2008-10-25 03:00:00'); From fbc219d1ae2e54576d246a3dd1921ba719cd795d Mon Sep 17 00:00:00 2001 From: Sayapin Alexander Date: Sun, 11 Mar 2012 21:49:31 +0400 Subject: [PATCH 2/2] Added randomly selected years for weekcount calculation. Fixed coding style. --- core/Base/Date.class.php | 2 +- test/core/DateTest.class.php | 41 +++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/core/Base/Date.class.php b/core/Base/Date.class.php index 3cd3c2f5f6..57ce906b7b 100644 --- a/core/Base/Date.class.php +++ b/core/Base/Date.class.php @@ -119,7 +119,7 @@ public static function compare(Date $left, Date $right) public static function getWeekCountInYear($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 { diff --git a/test/core/DateTest.class.php b/test/core/DateTest.class.php index b9712da5bd..b3efeef692 100644 --- a/test/core/DateTest.class.php +++ b/test/core/DateTest.class.php @@ -41,16 +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); - $weekCount = Date::getWeekCountInYear(2012); - $this->assertEquals($weekCount, 52, "Week count is incorrect"); + $this->dayDifferenceTest($left, $right, 1); - $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.'); - // unsolved giv's case // $left = Timestamp::create('2008-10-25 03:00:00'); // $right = Timestamp::create('2008-10-26 02:59:00'); @@ -74,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'));