Permalink
Browse files

Merge branch 'MDL-61378-master' of git://github.com/damyon/moodle

  • Loading branch information...
dmonllao committed Oct 9, 2018
2 parents 1471918 + 2015327 commit e2f8547acf62aa1c395d1082b1d68546a7577e18
@@ -2258,6 +2258,35 @@ function userdate($date, $format = '', $timezone = 99, $fixday = true, $fixhour
return $calendartype->timestamp_to_date_string($date, $format, $timezone, $fixday, $fixhour);
}
/**
* Returns a html "time" tag with both the exact user date with timezone information
* as a datetime attribute in the W3C format, and the user readable date and time as text.
*
* @package core
* @category time
* @param int $date the timestamp in UTC, as obtained from the database.
* @param string $format strftime format. You should probably get this using
* get_string('strftime...', 'langconfig');
* @param int|float|string $timezone by default, uses the user's time zone. if numeric and
* not 99 then daylight saving will not be added.
* {@link http://docs.moodle.org/dev/Time_API#Timezone}
* @param bool $fixday If true (default) then the leading zero from %d is removed.
* If false then the leading zero is maintained.
* @param bool $fixhour If true (default) then the leading zero from %I is removed.
* @return string the formatted date/time.
*/
function userdate_htmltime($date, $format = '', $timezone = 99, $fixday = true, $fixhour = true) {
$userdatestr = userdate($date, $format, $timezone, $fixday, $fixhour);
if (CLI_SCRIPT && !PHPUNIT_TEST) {
return $userdatestr;
}
$machinedate = new DateTime();
$machinedate->setTimestamp(intval($date));
$machinedate->setTimezone(core_date::get_user_timezone_object());
return html_writer::tag('time', $userdatestr, ['datetime' => $machinedate->format(DateTime::W3C)]);
}
/**
* Returns a formatted date ensuring it is UTF-8.
*
@@ -1714,73 +1714,85 @@ public function test_userdate() {
'time' => '1309514400',
'usertimezone' => 'America/Moncton',
'timezone' => '0.0', // No dst offset.
'expectedoutput' => 'Friday, 1 July 2011, 10:00 AM'
'expectedoutput' => 'Friday, 1 July 2011, 10:00 AM',
'expectedoutputhtml' => '<time datetime="2011-07-01T07:00:00-03:00">Friday, 1 July 2011, 10:00 AM</time>'
),
array(
'time' => '1309514400',
'usertimezone' => 'America/Moncton',
'timezone' => '99', // Dst offset and timezone offset.
'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM'
'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM',
'expectedoutputhtml' => '<time datetime="2011-07-01T07:00:00-03:00">Friday, 1 July 2011, 7:00 AM</time>'
),
array(
'time' => '1309514400',
'usertimezone' => 'America/Moncton',
'timezone' => 'America/Moncton', // Dst offset and timezone offset.
'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM'
'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM',
'expectedoutputhtml' => '<time datetime="2011-07-01t07:00:00-03:00">Friday, 1 July 2011, 7:00 AM</time>'
),
array(
'time' => '1293876000 ',
'usertimezone' => 'America/Moncton',
'timezone' => '0.0', // No dst offset.
'expectedoutput' => 'Saturday, 1 January 2011, 10:00 AM'
'expectedoutput' => 'Saturday, 1 January 2011, 10:00 AM',
'expectedoutputhtml' => '<time datetime="2011-01-01T06:00:00-04:00">Saturday, 1 January 2011, 10:00 AM</time>'
),
array(
'time' => '1293876000 ',
'usertimezone' => 'America/Moncton',
'timezone' => '99', // No dst offset in jan, so just timezone offset.
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM'
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM',
'expectedoutputhtml' => '<time datetime="2011-01-01T06:00:00-04:00">Saturday, 1 January 2011, 6:00 AM</time>'
),
array(
'time' => '1293876000 ',
'usertimezone' => 'America/Moncton',
'timezone' => 'America/Moncton', // No dst offset in jan.
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM'
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM',
'expectedoutputhtml' => '<time datetime="2011-01-01T06:00:00-04:00">Saturday, 1 January 2011, 6:00 AM</time>'
),
array(
'time' => '1293876000 ',
'usertimezone' => '2',
'timezone' => '99', // Take user timezone.
'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM'
'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM',
'expectedoutputhtml' => '<time datetime="2011-01-01T12:00:00+02:00">Saturday, 1 January 2011, 12:00 PM</time>'
),
array(
'time' => '1293876000 ',
'usertimezone' => '-2',
'timezone' => '99', // Take user timezone.
'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM'
'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM',
'expectedoutputhtml' => '<time datetime="2011-01-01T08:00:00-02:00">Saturday, 1 January 2011, 8:00 AM</time>'
),
array(
'time' => '1293876000 ',
'usertimezone' => '-10',
'timezone' => '2', // Take this timezone.
'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM'
'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM',
'expectedoutputhtml' => '<time datetime="2011-01-01T00:00:00-10:00">Saturday, 1 January 2011, 12:00 PM</time>'
),
array(
'time' => '1293876000 ',
'usertimezone' => '-10',
'timezone' => '-2', // Take this timezone.
'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM'
'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM',
'expectedoutputhtml' => '<time datetime="2011-01-01T00:00:00-10:00">Saturday, 1 January 2011, 8:00 AM</time>'
),
array(
'time' => '1293876000 ',
'usertimezone' => '-10',
'timezone' => 'random/time', // This should show server time.
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM'
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM',
'expectedoutputhtml' => '<time datetime="2011-01-01T00:00:00-10:00">Saturday, 1 January 2011, 6:00 PM</time>'
),
array(
'time' => '1293876000 ',
'usertimezone' => '20', // Fallback to server time zone.
'timezone' => '99', // This should show user time.
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM'
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM',
'expectedoutputhtml' => '<time datetime="2011-01-01T18:00:00+08:00">Saturday, 1 January 2011, 6:00 PM</time>'
),
);
@@ -1791,13 +1803,18 @@ public function test_userdate() {
foreach ($testvalues as $vals) {
$USER->timezone = $vals['usertimezone'];
$actualoutput = userdate($vals['time'], '%A, %d %B %Y, %I:%M %p', $vals['timezone']);
$actualoutputhtml = userdate_htmltime($vals['time'], '%A, %d %B %Y, %I:%M %p', $vals['timezone']);
// On different systems case of AM PM changes so compare case insensitive.
$vals['expectedoutput'] = core_text::strtolower($vals['expectedoutput']);
$vals['expectedoutputhtml'] = core_text::strtolower($vals['expectedoutputhtml']);
$actualoutput = core_text::strtolower($actualoutput);
$actualoutputhtml = core_text::strtolower($actualoutputhtml);
$this->assertSame($vals['expectedoutput'], $actualoutput,
"Expected: {$vals['expectedoutput']} => Actual: {$actualoutput} \ndata: " . var_export($vals, true));
$this->assertSame($vals['expectedoutputhtml'], $actualoutputhtml,
"Expected: {$vals['expectedoutputhtml']} => Actual: {$actualoutputhtml} \ndata: " . var_export($vals, true));
}
}
Oops, something went wrong.

0 comments on commit e2f8547

Please sign in to comment.