From 4a9c429b0c70c34ff3240a52bf86ceb0bc8975ec Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 29 Mar 2012 15:38:28 +0800 Subject: [PATCH] MDL-32234 Library: usergetdate return different values when timezone is set usergetdate should return consistent values for different timezone. There are two return path 1. If timezone is servertime, it returns getdate 2. If timezone other then servertime, it use gmstrftime, which returns string and values with leading zero. In second case, leading zero's are removed and values are type casted. One major fix is with yday, getdate returns 0 through 365, whereas %j returns 001 through 366. This has been fixed as well. --- lib/moodlelib.php | 9 +++++++++ lib/tests/moodlelib_test.php | 6 ++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 90c1677f1ce..a6a82170790 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -2086,6 +2086,15 @@ function usergetdate($time, $timezone=99) { $getdate['seconds'] ) = explode('_', $datestring); + // set correct datatype to match with getdate() + $getdate['seconds'] = (int)$getdate['seconds']; + $getdate['yday'] = (int)$getdate['yday'] - 1; // gettime returns 0 through 365 + $getdate['year'] = (int)$getdate['year']; + $getdate['mon'] = (int)$getdate['mon']; + $getdate['wday'] = (int)$getdate['wday']; + $getdate['mday'] = (int)$getdate['mday']; + $getdate['hours'] = (int)$getdate['hours']; + $getdate['minutes'] = (int)$getdate['minutes']; return $getdate; } diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index e00c015405f..8552f6a2e51 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -1020,10 +1020,9 @@ class moodlelib_testcase extends advanced_testcase { $this->assertEquals($wday,3); $this->assertEquals($mon,12); $this->assertEquals($year,2009); - $this->assertEquals($yday,357); + $this->assertEquals($yday,356); $this->assertEquals($weekday, 'Wednesday'); $this->assertEquals($month, 'December'); - $arr = usergetdate($ts);//gets the timezone from the $USER object $arr = array_values($arr); @@ -1035,10 +1034,9 @@ class moodlelib_testcase extends advanced_testcase { $this->assertEquals($wday,3); $this->assertEquals($mon,12); $this->assertEquals($year,2009); - $this->assertEquals($yday,357); + $this->assertEquals($yday,356); $this->assertEquals($weekday, 'Wednesday'); $this->assertEquals($month, 'December'); - //set the timezone back to what it was $USER->timezone = $userstimezone; -- 2.43.0