Permalink
Browse files

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

…Windows servers
  • Loading branch information...
sammarshallou committed Feb 6, 2012
1 parent 0dde394 commit d9498b3805876e2e85e1d2e558502d12a3f986c1
Showing with 37 additions and 21 deletions.
  1. +4 −4 lang/en/langconfig.php
  2. +25 −9 lib/moodlelib.php
  3. +8 −8 lib/simpletest/testmoodlelib.php
View
@@ -39,16 +39,16 @@
$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
@@ -1945,9 +1945,10 @@ function format_time($totalsecs, $str=NULL) {
* {@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;
}
@@ -1317,13 +1317,13 @@ public function test_userdate() {
'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 @@ public function test_userdate() {
'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 @@ public function test_userdate() {
'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 @@ public function test_userdate() {
'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'
),
);

0 comments on commit d9498b3

Please sign in to comment.