Merge branch 'MDL-75100-master' of https://github.com/marinaglancy/moodle

This commit is contained in:
Jun Pataleta 2022-11-22 09:14:04 +08:00
commit a1ed676d15
10 changed files with 219 additions and 19 deletions

View File

@ -45,7 +45,7 @@ if (isset($_ENV['TMPDIR']) && is_dir($_ENV['TMPDIR'])) {
$tmpfile = $tmpdir . '/moodle-mailout.log';
$fh = fopen($tmpfile, 'a+', false)
or mdie("Error openning $tmpfile on append\n");
fwrite($fh, "==== ".strftime("%a %b %e %H:%M:%S %Y", time())." ====\n");
fwrite($fh, "==== ".date("D M d H:i:s Y", time())." ====\n");
fwrite($fh, "==== Commandline: " . implode(' ',$argv) . "\n");
$stdin = fopen('php://stdin', 'r');

View File

@ -52,7 +52,7 @@ class rollback extends base {
);
$context->applications[] = [
'timeapplied' => strftime($format, $application->time),
'timeapplied' => \core_date::strftime($format, (int)$application->time),
'user' => fullname($user),
'action' => $rollbacklink->out(false),
];

View File

@ -92,8 +92,7 @@ class renderer extends \plugin_renderer_base {
* @return string
*/
public function format_published_date($timestamp) {
return strftime(get_string('strftimerecentfull', 'langconfig'), $timestamp);
return date('j F Y, g:i a', $timestamp);
return \core_date::strftime(get_string('strftimerecentfull', 'langconfig'), $timestamp);
}
/**

View File

@ -118,9 +118,7 @@ class behat_calendar extends behat_base {
* @Given /^I hover over today in the mini-calendar block$/
*/
public function i_hover_over_today_in_mini_calendar_block(): void {
// For window's compatibility, using %d and not %e.
$todaysday = trim(strftime('%d'));
$todaysday = ltrim($todaysday, '0');
$todaysday = date('j');
$this->i_hover_over_day_of_this_month_in_mini_calendar_block($todaysday);
}
@ -130,9 +128,7 @@ class behat_calendar extends behat_base {
* @Given /^I hover over today in the calendar$/
*/
public function i_hover_over_today_in_the_calendar() {
// For window's compatibility, using %d and not %e.
$todaysday = trim(strftime('%d'));
$todaysday = ltrim($todaysday, '0');
$todaysday = date('j');
return $this->i_hover_over_day_of_this_month_in_calendar($todaysday);
}

View File

@ -329,11 +329,11 @@ class structure extends type_base {
date_default_timezone_set(\core_date::get_user_timezone($timezone));
if ($fixday) {
$daystring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %d', $time)));
$daystring = ltrim(str_replace(array(' 0', ' '), '', date(' d', $time)));
$datestring = str_replace('DD', $daystring, $datestring);
}
if ($fixhour) {
$hourstring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %I', $time)));
$hourstring = ltrim(str_replace(array(' 0', ' '), '', date(' h', $time)));
$datestring = str_replace('HH', $hourstring, $datestring);
}

View File

@ -692,4 +692,209 @@ class core_date {
}
}
}
/**
* Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible)
* This provides a cross-platform alternative to strftime() for when it will be removed from PHP.
* Note that output can be slightly different between libc sprintf and this function as it is using ICU.
*
* From:
* https://github.com/alphp/strftime
*
* @param string $format Date format
* @param int|string|DateTime $timestamp Timestamp
* @param string|null $locale
* @return string
* @author BohwaZ <https://bohwaz.net/>
*/
public static function strftime(string $format, $timestamp = null, ?string $locale = null) : string {
// phpcs:disable
if (!($timestamp instanceof DateTimeInterface)) {
$timestamp = is_int($timestamp) ? '@' . $timestamp : (string) $timestamp;
try {
$timestamp = new DateTime($timestamp);
} catch (Exception $e) {
throw new InvalidArgumentException('$timestamp argument is neither a valid UNIX timestamp, a valid date-time string or a DateTime object.', 0, $e);
}
}
$timestamp->setTimezone(new DateTimeZone(date_default_timezone_get()));
if (empty($locale)) {
// get current locale
$locale = setlocale(LC_TIME, '0');
}
// remove trailing part not supported by ext-intl locale
$locale = preg_replace('/[^\w-].*$/', '', $locale);
$intl_formats = [
'%a' => 'EEE', // An abbreviated textual representation of the day Sun through Sat
'%A' => 'EEEE', // A full textual representation of the day Sunday through Saturday
'%b' => 'MMM', // Abbreviated month name, based on the locale Jan through Dec
'%B' => 'MMMM', // Full month name, based on the locale January through December
'%h' => 'MMM', // Abbreviated month name, based on the locale (an alias of %b) Jan through Dec
];
$intl_formatter = function (DateTimeInterface $timestamp, string $format) use ($intl_formats, $locale) {
$tz = $timestamp->getTimezone();
$date_type = IntlDateFormatter::FULL;
$time_type = IntlDateFormatter::FULL;
$pattern = '';
switch ($format) {
// %c = Preferred date and time stamp based on locale
// Example: Tue Feb 5 00:45:10 2009 for February 5, 2009 at 12:45:10 AM
case '%c':
$date_type = IntlDateFormatter::LONG;
$time_type = IntlDateFormatter::SHORT;
break;
// %x = Preferred date representation based on locale, without the time
// Example: 02/05/09 for February 5, 2009
case '%x':
$date_type = IntlDateFormatter::SHORT;
$time_type = IntlDateFormatter::NONE;
break;
// Localized time format
case '%X':
$date_type = IntlDateFormatter::NONE;
$time_type = IntlDateFormatter::MEDIUM;
break;
default:
$pattern = $intl_formats[$format];
}
// In October 1582, the Gregorian calendar replaced the Julian in much of Europe, and
// the 4th October was followed by the 15th October.
// ICU (including IntlDateFormattter) interprets and formats dates based on this cutover.
// Posix (including strftime) and timelib (including DateTimeImmutable) instead use
// a "proleptic Gregorian calendar" - they pretend the Gregorian calendar has existed forever.
// This leads to the same instants in time, as expressed in Unix time, having different representations
// in formatted strings.
// To adjust for this, a custom calendar can be supplied with a cutover date arbitrarily far in the past.
$calendar = IntlGregorianCalendar::createInstance();
$calendar->setGregorianChange(PHP_INT_MIN);
return (new IntlDateFormatter($locale, $date_type, $time_type, $tz, $calendar, $pattern))->format($timestamp);
};
// Same order as https://www.php.net/manual/en/function.strftime.php
$translation_table = [
// Day
'%a' => $intl_formatter,
'%A' => $intl_formatter,
'%d' => 'd',
'%e' => function ($timestamp) {
return sprintf('% 2u', $timestamp->format('j'));
},
'%j' => function ($timestamp) {
// Day number in year, 001 to 366
return sprintf('%03d', $timestamp->format('z')+1);
},
'%u' => 'N',
'%w' => 'w',
// Week
'%U' => function ($timestamp) {
// Number of weeks between date and first Sunday of year
$day = new DateTime(sprintf('%d-01 Sunday', $timestamp->format('Y')));
return sprintf('%02u', 1 + ($timestamp->format('z') - $day->format('z')) / 7);
},
'%V' => 'W',
'%W' => function ($timestamp) {
// Number of weeks between date and first Monday of year
$day = new DateTime(sprintf('%d-01 Monday', $timestamp->format('Y')));
return sprintf('%02u', 1 + ($timestamp->format('z') - $day->format('z')) / 7);
},
// Month
'%b' => $intl_formatter,
'%B' => $intl_formatter,
'%h' => $intl_formatter,
'%m' => 'm',
// Year
'%C' => function ($timestamp) {
// Century (-1): 19 for 20th century
return floor($timestamp->format('Y') / 100);
},
'%g' => function ($timestamp) {
return substr($timestamp->format('o'), -2);
},
'%G' => 'o',
'%y' => 'y',
'%Y' => 'Y',
// Time
'%H' => 'H',
'%k' => function ($timestamp) {
return sprintf('% 2u', $timestamp->format('G'));
},
'%I' => 'h',
'%l' => function ($timestamp) {
return sprintf('% 2u', $timestamp->format('g'));
},
'%M' => 'i',
'%p' => 'A', // AM PM (this is reversed on purpose!)
'%P' => 'a', // am pm
'%r' => 'h:i:s A', // %I:%M:%S %p
'%R' => 'H:i', // %H:%M
'%S' => 's',
'%T' => 'H:i:s', // %H:%M:%S
'%X' => $intl_formatter, // Preferred time representation based on locale, without the date
// Timezone
'%z' => 'O',
'%Z' => 'T',
// Time and Date Stamps
'%c' => $intl_formatter,
'%D' => 'm/d/Y',
'%F' => 'Y-m-d',
'%s' => 'U',
'%x' => $intl_formatter,
];
$out = preg_replace_callback('/(?<!%)%([_#-]?)([a-zA-Z])/', function ($match) use ($translation_table, $timestamp) {
$prefix = $match[1];
$char = $match[2];
$pattern = '%'.$char;
if ($pattern == '%n') {
return "\n";
} elseif ($pattern == '%t') {
return "\t";
}
if (!isset($translation_table[$pattern])) {
throw new InvalidArgumentException(sprintf('Format "%s" is unknown in time format', $pattern));
}
$replace = $translation_table[$pattern];
if (is_string($replace)) {
$result = $timestamp->format($replace);
} else {
$result = $replace($timestamp, $pattern);
}
switch ($prefix) {
case '_':
// replace leading zeros with spaces but keep last char if also zero
return preg_replace('/\G0(?=.)/', ' ', $result);
case '#':
case '-':
// remove leading zeros but keep last char if also zero
return preg_replace('/^0+(?=.)/', '', $result);
}
return $result;
}, $format);
$out = str_replace('%%', '%', $out);
return $out;
// phpcs:enable
}
}

View File

@ -2377,7 +2377,7 @@ function date_format_string($date, $format, $tz = 99) {
date_default_timezone_set(core_date::get_user_timezone($tz));
if (strftime('%p', 0) === strftime('%p', HOURSECS * 18)) {
if (date('A', 0) === date('A', HOURSECS * 18)) {
$datearray = getdate($date);
$format = str_replace([
'%P',
@ -2388,7 +2388,7 @@ function date_format_string($date, $format, $tz = 99) {
], $format);
}
$datestring = strftime($format, $date);
$datestring = core_date::strftime($format, $date);
core_date::set_default_server_timezone();
if ($localewincharset) {

View File

@ -1177,8 +1177,8 @@ class MoodleODSWriter {
if (isset($cell->formula)) {
$buffer .= '<table:table-cell table:formula="of:'.$cell->formula.'"'.$extra.'></table:table-cell>'."\n";
} else if ($cell->type == 'date') {
$buffer .= '<table:table-cell office:value-type="date" office:date-value="' . strftime('%Y-%m-%dT%H:%M:%S', $cell->value) . '"'.$extra.'>'
. $pretext . strftime('%Y-%m-%dT%H:%M:%S', $cell->value) . $posttext
$buffer .= '<table:table-cell office:value-type="date" office:date-value="' . date("Y-m-d\\TH:i:s", $cell->value) . '"'.$extra.'>'
. $pretext . date("Y-m-d\\TH:i:s", $cell->value) . $posttext
. '</table:table-cell>'."\n";
} else if ($cell->type == 'float') {
$buffer .= '<table:table-cell office:value-type="float" office:value="' . htmlspecialchars($cell->value, ENT_QUOTES, 'utf-8') . '"'.$extra.'>'
@ -1267,7 +1267,7 @@ class MoodleODSWriter {
<office:meta>
<meta:generator>Moodle '.$CFG->release.'</meta:generator>
<meta:initial-creator>' . htmlspecialchars(fullname($USER, true), ENT_QUOTES, 'utf-8') . '</meta:initial-creator>
<meta:creation-date>'.strftime('%Y-%m-%dT%H:%M:%S').'</meta:creation-date>
<meta:creation-date>'.date("Y-m-d\\TH:i:s").'</meta:creation-date>
<meta:document-statistic meta:table-count="1" meta:cell-count="0" meta:object-count="0"/>
</office:meta>
</office:document-meta>';

View File

@ -276,7 +276,7 @@ class date_legacy_test extends \advanced_testcase {
$expected->setTimezone(new \DateTimeZone(($user->timezone == 99 ? 'Pacific/Auckland' : $user->timezone)));
$result = userdate($expected->getTimestamp(), '', 99, false, false);
date_default_timezone_set($expected->getTimezone()->getName());
$ex = strftime($format, $expected->getTimestamp());
$ex = \core_date::strftime($format, $expected->getTimestamp());
date_default_timezone_set($CFG->timezone);
$this->assertSame($ex, $result);
}

View File

@ -1771,7 +1771,7 @@ class page_wiki_map extends page_wiki {
$strdataux = '';
foreach ($pages as $page) {
$user = wiki_get_user_info($page->userid);
$strdata = strftime('%d %b %Y', $page->timemodified);
$strdata = date('d M Y', $page->timemodified);
if ($strdata != $strdataux) {
$table->data[] = array($OUTPUT->heading($strdata, 4));
$strdataux = $strdata;