Skip to content

Commit

Permalink
MDL-70926 core: getuserdate() shoud show debugging if null was passed
Browse files Browse the repository at this point in the history
passing null to getdate() has different results in PHP7 and PHP8
  • Loading branch information
marinaglancy authored and stronk7 committed May 6, 2021
1 parent 5469202 commit b46badb
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 4 deletions.
Expand Up @@ -192,7 +192,7 @@ public function create_instance(\stdClass $dbrow) {
(new \DateTimeImmutable())->setTimestamp($dbrow->timestart + $dbrow->timeduration),
(new \DateTimeImmutable())->setTimestamp($dbrow->timesort ? $dbrow->timesort : $dbrow->timestart),
(new \DateTimeImmutable())->setTimestamp($dbrow->timemodified),
(new \DateTimeImmutable())->setTimestamp(usergetmidnight($dbrow->timesort))
(new \DateTimeImmutable())->setTimestamp($dbrow->timesort ? usergetmidnight($dbrow->timesort) : 0)
),
!empty($dbrow->visible),
$subscription,
Expand Down
2 changes: 1 addition & 1 deletion calendar/tests/helpers.php
Expand Up @@ -230,7 +230,7 @@ public function create_instance(\stdClass $record) {
(new \DateTimeImmutable())->setTimestamp($record->timestart + $record->timeduration),
(new \DateTimeImmutable())->setTimestamp($record->timesort ? $record->timesort : $record->timestart),
(new \DateTimeImmutable())->setTimestamp($record->timemodified),
(new \DateTimeImmutable())->setTimestamp(usergetmidnight($record->timesort))
(new \DateTimeImmutable())->setTimestamp($record->timesort ? usergetmidnight($record->timesort) : 0)
),
!empty($record->visible),
$subscription,
Expand Down
2 changes: 1 addition & 1 deletion lib/excellib.class.php
Expand Up @@ -246,7 +246,7 @@ public function write_url($row, $col, $url, $format = null) {
* Write one date somewhere in the worksheet.
* @param integer $row Zero indexed row
* @param integer $col Zero indexed column
* @param string $date The date to write in UNIX timestamp format
* @param int $date The date to write in UNIX timestamp format
* @param mixed $format The XF format for the cell
*/
public function write_date($row, $col, $date, $format = null) {
Expand Down
2 changes: 1 addition & 1 deletion lib/form/defaultcustom.php
Expand Up @@ -103,7 +103,7 @@ public function __construct($elementname = null, $elementlabel = null, $options
*/
protected function timestamp_to_date_array($value) {
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$currentdate = $calendartype->timestamp_to_date_array($value, $this->_options['timezone']);
$currentdate = $calendartype->timestamp_to_date_array((int)$value, $this->_options['timezone']);
return array(
'minute' => $currentdate['minutes'],
'hour' => $currentdate['hours'],
Expand Down
8 changes: 8 additions & 0 deletions lib/moodlelib.php
Expand Up @@ -2354,6 +2354,14 @@ function date_format_string($date, $format, $tz = 99) {
* @return array An array that represents the date in user time
*/
function usergetdate($time, $timezone=99) {
if ($time === null) {
// PHP8 and PHP7 return different results when getdate(null) is called.
// Display warning and cast to 0 to make sure the usergetdate() behaves consistently on all versions of PHP.
// In the future versions of Moodle we may consider adding a strict typehint.
debugging('usergetdate() expects parameter $time to be int, null given', DEBUG_DEVELOPER);
$time = 0;
}

date_default_timezone_set(core_date::get_user_timezone($timezone));
$result = getdate($time);
core_date::set_default_server_timezone();
Expand Down
6 changes: 6 additions & 0 deletions lib/tests/moodlelib_test.php
Expand Up @@ -1317,6 +1317,12 @@ public function test_usergetdate() {
$this->assertSame(356, $yday);
$this->assertSame('Wednesday', $weekday);
$this->assertSame('December', $month);

// Edge cases - 0 and null - they all mean 1st Jan 1970. Null shows debugging message.
$this->assertSame(1970, usergetdate(0)['year']);
$this->assertDebuggingNotCalled();
$this->assertSame(1970, usergetdate(null)['year']);
$this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
}

public function test_mark_user_preferences_changed() {
Expand Down

0 comments on commit b46badb

Please sign in to comment.