MDL-31514 Lib: Time format to remove unwanted zeros caused errors on Windows servers

This commit is contained in:
sam marshall 2012-02-06 12:26:40 +00:00
parent 0dde394db5
commit d9498b3805
3 changed files with 37 additions and 21 deletions

View File

@ -39,16 +39,16 @@ $string['parentlanguage'] = '';
$string['strftimedate'] = '%d %B %Y';
$string['strftimedatefullshort'] = '%d/%m/%y';
$string['strftimedateshort'] = '%d %B';
$string['strftimedatetime'] = '%d %B %Y, %l:%M %p';
$string['strftimedatetime'] = '%d %B %Y, %I:%M %p';
$string['strftimedatetimeshort'] = '%d/%m/%y, %H:%M';
$string['strftimedaydate'] = '%A, %d %B %Y';
$string['strftimedaydatetime'] = '%A, %d %B %Y, %l:%M %p';
$string['strftimedaydatetime'] = '%A, %d %B %Y, %I:%M %p';
$string['strftimedayshort'] = '%A, %d %B';
$string['strftimedaytime'] = '%a, %H:%M';
$string['strftimemonthyear'] = '%B %Y';
$string['strftimerecent'] = '%d %b, %H:%M';
$string['strftimerecentfull'] = '%a, %d %b %Y, %l:%M %p';
$string['strftimetime'] = '%l:%M %p';
$string['strftimerecentfull'] = '%a, %d %b %Y, %I:%M %p';
$string['strftimetime'] = '%I:%M %p';
$string['thisdirection'] = 'ltr';
$string['thisdirectionvertical'] = 'btt';
$string['thislanguage'] = 'English';

View File

@ -1945,9 +1945,10 @@ function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0,
* {@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($date, $format = '', $timezone = 99, $fixday = true) {
function userdate($date, $format = '', $timezone = 99, $fixday = true, $fixhour = true) {
global $CFG;
@ -1960,6 +1961,19 @@ function userdate($date, $format = '', $timezone = 99, $fixday = true) {
} else if ($fixday) {
$formatnoday = str_replace('%d', 'DD', $format);
$fixday = ($formatnoday != $format);
$format = $formatnoday;
}
// Note: This logic about fixing 12-hour time to remove unnecessary leading
// zero is required because on Windows, PHP strftime function does not
// support the correct 'hour without leading zero' parameter (%l).
if (!empty($CFG->nofixhour)) {
// Config.php can force %I not to be fixed.
$fixhour = false;
} else if ($fixhour) {
$formatnohour = str_replace('%I', 'HH', $format);
$fixhour = ($formatnohour != $format);
$format = $formatnohour;
}
//add daylight saving offset for string timezones only, as we can't get dst for
@ -1971,21 +1985,25 @@ function userdate($date, $format = '', $timezone = 99, $fixday = true) {
$timezone = get_user_timezone_offset($timezone);
if (abs($timezone) > 13) { /// Server time
$datestring = strftime($format, $date);
if ($fixday) {
$datestring = strftime($formatnoday, $date);
$daystring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %d', $date)));
$datestring = str_replace('DD', $daystring, $datestring);
} else {
$datestring = strftime($format, $date);
}
if ($fixhour) {
$hourstring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %I', $date)));
$datestring = str_replace('HH', $hourstring, $datestring);
}
} else {
$date += (int)($timezone * 3600);
$datestring = gmstrftime($format, $date);
if ($fixday) {
$datestring = gmstrftime($formatnoday, $date);
$daystring = ltrim(str_replace(array(' 0', ' '), '', gmstrftime(' %d', $date)));
$datestring = str_replace('DD', $daystring, $datestring);
} else {
$datestring = gmstrftime($format, $date);
}
if ($fixhour) {
$hourstring = ltrim(str_replace(array(' 0', ' '), '', gmstrftime(' %I', $date)));
$datestring = str_replace('HH', $hourstring, $datestring);
}
}
@ -1999,8 +2017,6 @@ function userdate($date, $format = '', $timezone = 99, $fixday = true) {
}
}
// When using the %l (12-hour time with no leading zero), it adds unwanted spaces
$datestring = trim(str_replace(' ', ' ', $datestring));
return $datestring;
}

View File

@ -1317,13 +1317,13 @@ class moodlelib_test extends UnitTestCase {
'time' => '1309514400',
'usertimezone' => 'America/Moncton',
'timezone' => '99', //dst offset and timezone offset.
'expectedoutput' => 'Friday, 1 July 2011, 07:00 AM'
'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM'
),
array(
'time' => '1309514400',
'usertimezone' => 'America/Moncton',
'timezone' => 'America/Moncton', //dst offset and timezone offset.
'expectedoutput' => 'Friday, 1 July 2011, 07:00 AM'
'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM'
),
array(
'time' => '1293876000 ',
@ -1335,13 +1335,13 @@ class moodlelib_test extends UnitTestCase {
'time' => '1293876000 ',
'usertimezone' => 'America/Moncton',
'timezone' => '99', //no dst offset in jan, so just timezone offset.
'expectedoutput' => 'Saturday, 1 January 2011, 06:00 AM'
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM'
),
array(
'time' => '1293876000 ',
'usertimezone' => 'America/Moncton',
'timezone' => 'America/Moncton', //no dst offset in jan
'expectedoutput' => 'Saturday, 1 January 2011, 06:00 AM'
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM'
),
array(
'time' => '1293876000 ',
@ -1353,7 +1353,7 @@ class moodlelib_test extends UnitTestCase {
'time' => '1293876000 ',
'usertimezone' => '-2',
'timezone' => '99', //take user timezone
'expectedoutput' => 'Saturday, 1 January 2011, 08:00 AM'
'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM'
),
array(
'time' => '1293876000 ',
@ -1365,19 +1365,19 @@ class moodlelib_test extends UnitTestCase {
'time' => '1293876000 ',
'usertimezone' => '-10',
'timezone' => '-2', //take this timezone
'expectedoutput' => 'Saturday, 1 January 2011, 08:00 AM'
'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM'
),
array(
'time' => '1293876000 ',
'usertimezone' => '-10',
'timezone' => 'random/time', //this should show server time
'expectedoutput' => 'Saturday, 1 January 2011, 06:00 PM'
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM'
),
array(
'time' => '1293876000 ',
'usertimezone' => '14', //server time zone
'timezone' => '99', //this should show user time
'expectedoutput' => 'Saturday, 1 January 2011, 06:00 PM'
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM'
),
);