cvs: pear /Calendar Week.php /Calendar/tests week_test.php

From: Date: Fri, 20 Oct 2006 14:03:58 +0000
Subject: cvs: pear /Calendar Week.php /Calendar/tests week_test.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-42677@lists.php.net to get a copy of this message
quipo Fri Oct 20 14:03:58 2006 UTC Modified files: /pear/Calendar Week.php /pear/Calendar/tests week_test.php Log: fixed Calendar_Week::thisYear() according the ISO-8601 standard (bug #8758) http://cvs.php.net/viewvc.cgi/pear/Calendar/Week.php?r1=1.7&r2=1.8&diff_format=u Index: pear/Calendar/Week.php diff -u pear/Calendar/Week.php:1.7 pear/Calendar/Week.php:1.8 --- pear/Calendar/Week.php:1.7 Sat Oct 22 10:26:49 2005 +++ pear/Calendar/Week.php Fri Oct 20 14:03:58 2006 @@ -18,11 +18,11 @@ // | Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: Week.php,v 1.7 2005/10/22 10:26:49 quipo Exp $ +// $Id: Week.php,v 1.8 2006/10/20 14:03:58 quipo Exp $ // /** * @package Calendar - * @version $Id: Week.php,v 1.7 2005/10/22 10:26:49 quipo Exp $ + * @version $Id: Week.php,v 1.8 2006/10/20 14:03:58 quipo Exp $ */ /** @@ -248,6 +248,35 @@ } /** + * Returns the value for this year + * + * When a on the first/last week of the year, the year of the week is + * calculated according to ISO-8601 + * + * @param string return value format ['int' | 'timestamp' | 'object' | 'array'] + * @return int e.g. 2003 or timestamp + * @access public + */ + function thisYear($format = 'int') + { + $tmp_cal = new Calendar(); + $tmp_cal->setTimestamp($this->thisWeek); + $first_dow = $tmp_cal->thisDay('array'); + $days_in_week = $tmp_cal->cE->getDaysInWeek($tmp_cal->year, $tmp_cal->month, $tmp_cal->day); + $tmp_cal->day += $days_in_week; + $last_dow = $tmp_cal->thisDay('array'); + + if ($first_dow['year'] == $last_dow['year']) { + return $first_dow['year']; + } + + if ($last_dow['day'] > floor($days_in_week / 2)) { + return $last_dow['year']; + } + return $first_dow['year']; + } + + /** * Gets the value of the previous week, according to the requested format * * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] http://cvs.php.net/viewvc.cgi/pear/Calendar/tests/week_test.php?r1=1.4&r2=1.5&diff_format=u Index: pear/Calendar/tests/week_test.php diff -u pear/Calendar/tests/week_test.php:1.4 pear/Calendar/tests/week_test.php:1.5 --- pear/Calendar/tests/week_test.php:1.4 Thu Oct 20 18:56:21 2005 +++ pear/Calendar/tests/week_test.php Fri Oct 20 14:03:58 2006 @@ -1,5 +1,5 @@ <?php -// $Id: week_test.php,v 1.4 2005/10/20 18:56:21 quipo Exp $ +// $Id: week_test.php,v 1.5 2006/10/20 14:03:58 quipo Exp $ define('CALENDAR_FIRST_DAY_OF_WEEK', 1); //force firstDay = monday require_once('simple_include.php'); @@ -15,6 +15,41 @@ $this->cal = Calendar_Factory::create('Week', 2003, 10, 9); //print_r($this->cal); } + function testThisYear () { + $this->assertEqual(2003, $this->cal->thisYear()); + + $stamp = mktime(0,0,0,1,1,2003); + $this->cal->setTimestamp($stamp); + $this->assertEqual(2003, $this->cal->thisYear()); + + $stamp = mktime(0,0,0,12,31,2003); + $this->cal->setTimestamp($stamp); + $this->assertEqual(2004, $this->cal->thisYear()); + + $stamp = mktime(0,0,0,1,1,2005); + $this->cal->setTimestamp($stamp); + $this->assertEqual(2004, $this->cal->thisYear()); + + $stamp = mktime(0,0,0,12,31,2004); + $this->cal->setTimestamp($stamp); + $this->assertEqual(2004, $this->cal->thisYear()); + + $stamp = mktime(0,0,0,1,1,2005); + $this->cal->setTimestamp($stamp); + $this->assertEqual(2004, $this->cal->thisYear()); + + $stamp = mktime(0,0,0,12,31,2005); + $this->cal->setTimestamp($stamp); + $this->assertEqual(2005, $this->cal->thisYear()); + + $stamp = mktime(0,0,0,1,1,2006); + $this->cal->setTimestamp($stamp); + $this->assertEqual(2005, $this->cal->thisYear()); + + $stamp = mktime(0,0,0,12,31,2006); + $this->cal->setTimestamp($stamp); + $this->assertEqual(2006, $this->cal->thisYear()); + } function testPrevDay () { $this->assertEqual(8, $this->cal->prevDay()); }

« previous php.pear.cvs (#42677) next »