Skip to content
Browse files

Merge branch 'MDL-35412-23' of git://github.com/FMCorz/moodle into MO…

…ODLE_23_STABLE
  • Loading branch information...
2 parents ac2b4fb + 4563525 commit 777354509d3ab3ba52ab9ed97dd7f6a58c48bf06 Sam Hemelryk committed Oct 16, 2012
Showing with 118 additions and 33 deletions.
  1. +19 −19 lib/formslib.php
  2. +42 −14 lib/moodlelib.php
  3. +57 −0 lib/tests/moodlelib_test.php
View
38 lib/formslib.php
@@ -81,25 +81,25 @@ function form_init_date_js() {
$function = 'M.form.dateselector.init_date_selectors';
$config = array(array(
'firstdayofweek' => get_string('firstdayofweek', 'langconfig'),
- 'mon' => strftime('%a', strtotime("Monday")),
- 'tue' => strftime('%a', strtotime("Tuesday")),
- 'wed' => strftime('%a', strtotime("Wednesday")),
- 'thu' => strftime('%a', strtotime("Thursday")),
- 'fri' => strftime('%a', strtotime("Friday")),
- 'sat' => strftime('%a', strtotime("Saturday")),
- 'sun' => strftime('%a', strtotime("Sunday")),
- 'january' => strftime('%B', strtotime("January 1")),
- 'february' => strftime('%B', strtotime("February 1")),
- 'march' => strftime('%B', strtotime("March 1")),
- 'april' => strftime('%B', strtotime("April 1")),
- 'may' => strftime('%B', strtotime("May 1")),
- 'june' => strftime('%B', strtotime("June 1")),
- 'july' => strftime('%B', strtotime("July 1")),
- 'august' => strftime('%B', strtotime("August 1")),
- 'september' => strftime('%B', strtotime("September 1")),
- 'october' => strftime('%B', strtotime("October 1")),
- 'november' => strftime('%B', strtotime("November 1")),
- 'december' => strftime('%B', strtotime("December 1"))
+ 'mon' => date_format_string(strtotime("Monday"), '%a', 99),
+ 'tue' => date_format_string(strtotime("Tuesday"), '%a', 99),
+ 'wed' => date_format_string(strtotime("Wednesday"), '%a', 99),
+ 'thu' => date_format_string(strtotime("Thursday"), '%a', 99),
+ 'fri' => date_format_string(strtotime("Friday"), '%a', 99),
+ 'sat' => date_format_string(strtotime("Saturday"), '%a', 99),
+ 'sun' => date_format_string(strtotime("Sunday"), '%a', 99),
+ 'january' => date_format_string(strtotime("January 1"), '%B', 99),
+ 'february' => date_format_string(strtotime("February 1"), '%B', 99),
+ 'march' => date_format_string(strtotime("March 1"), '%B', 99),
+ 'april' => date_format_string(strtotime("April 1"), '%B', 99),
+ 'may' => date_format_string(strtotime("May 1"), '%B', 99),
+ 'june' => date_format_string(strtotime("June 1"), '%B', 99),
+ 'july' => date_format_string(strtotime("July 1"), '%B', 99),
+ 'august' => date_format_string(strtotime("August 1"), '%B', 99),
+ 'september' => date_format_string(strtotime("September 1"), '%B', 99),
+ 'october' => date_format_string(strtotime("October 1"), '%B', 99),
+ 'november' => date_format_string(strtotime("November 1"), '%B', 99),
+ 'december' => date_format_string(strtotime("December 1"), '%B', 99)
));
$PAGE->requires->yui_module($module, $function, $config);
$done = true;
View
56 lib/moodlelib.php
@@ -2071,13 +2071,7 @@ function userdate($date, $format = '', $timezone = 99, $fixday = true, $fixhour
// (because it's impossible to specify UTF-8 to fetch locale info in Win32)
if (abs($timezone) > 13) { /// Server time
- if ($CFG->ostype == 'WINDOWS' and ($localewincharset = get_string('localewincharset', 'langconfig'))) {
- $format = textlib::convert($format, 'utf-8', $localewincharset);
- $datestring = strftime($format, $date);
- $datestring = textlib::convert($datestring, $localewincharset, 'utf-8');
- } else {
- $datestring = strftime($format, $date);
- }
+ $datestring = date_format_string($date, $format, $timezone);
if ($fixday) {
$daystring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %d', $date)));
$datestring = str_replace('DD', $daystring, $datestring);
@@ -2089,13 +2083,7 @@ function userdate($date, $format = '', $timezone = 99, $fixday = true, $fixhour
} else {
$date += (int)($timezone * 3600);
- if ($CFG->ostype == 'WINDOWS' and ($localewincharset = get_string('localewincharset', 'langconfig'))) {
- $format = textlib::convert($format, 'utf-8', $localewincharset);
- $datestring = gmstrftime($format, $date);
- $datestring = textlib::convert($datestring, $localewincharset, 'utf-8');
- } else {
- $datestring = gmstrftime($format, $date);
- }
+ $datestring = date_format_string($date, $format, $timezone);
if ($fixday) {
$daystring = ltrim(str_replace(array(' 0', ' '), '', gmstrftime(' %d', $date)));
$datestring = str_replace('DD', $daystring, $datestring);
@@ -2110,6 +2098,46 @@ function userdate($date, $format = '', $timezone = 99, $fixday = true, $fixhour
}
/**
+ * Returns a formatted date ensuring it is UTF-8.
+ *
+ * If we are running under Windows convert to Windows encoding and then back to UTF-8
+ * (because it's impossible to specify UTF-8 to fetch locale info in Win32).
+ *
+ * This function does not do any calculation regarding the user preferences and should
+ * therefore receive the final date timestamp, format and timezone. Timezone being only used
+ * to differenciate the use of server time or not (strftime() against gmstrftime()).
+ *
+ * @param int $date the timestamp.
+ * @param string $format strftime format.
+ * @param int|float $timezone the numerical timezone, typically returned by {@link get_user_timezone_offset()}.
+ * @return string the formatted date/time.
+ * @since 2.3.3
+ */
+function date_format_string($date, $format, $tz = 99) {
+ global $CFG;
+ if (abs($tz) > 13) {
+ if ($CFG->ostype == 'WINDOWS') {
+ $localewincharset = get_string('localewincharset', 'langconfig');
+ $format = textlib::convert($format, 'utf-8', $localewincharset);
+ $datestring = strftime($format, $date);
+ $datestring = textlib::convert($datestring, $localewincharset, 'utf-8');
+ } else {
+ $datestring = strftime($format, $date);
+ }
+ } else {
+ if ($CFG->ostype == 'WINDOWS') {
+ $localewincharset = get_string('localewincharset', 'langconfig');
+ $format = textlib::convert($format, 'utf-8', $localewincharset);
+ $datestring = gmstrftime($format, $date);
+ $datestring = textlib::convert($datestring, $localewincharset, 'utf-8');
+ } else {
+ $datestring = gmstrftime($format, $date);
+ }
+ }
+ return $datestring;
+}
+
+/**
* Given a $time timestamp in GMT (seconds since epoch),
* returns an array that represents the date in user time
*
View
57 lib/tests/moodlelib_test.php
@@ -1993,4 +1993,61 @@ public function test_convert_to_array() {
);
$this->assertEquals(convert_to_array($obj), $ar);
}
+
+ /**
+ * Test the function date_format_string().
+ */
+ function test_date_format_string() {
+ // Forcing locale and timezone.
+ $oldlocale = setlocale(LC_TIME, '0');
+ setlocale(LC_TIME, 'en_AU.UTF-8');
+ $systemdefaulttimezone = date_default_timezone_get();
+ date_default_timezone_set('Australia/Perth');
+
+ $tests = array(
+ array(
+ 'tz' => 99,
+ 'str' => '%A, %d %B %Y, %I:%M %p',
+ 'expected' => 'Saturday, 01 January 2011, 06:00 PM'
+ ),
+ array(
+ 'tz' => 0,
+ 'str' => '%A, %d %B %Y, %I:%M %p',
+ 'expected' => 'Saturday, 01 January 2011, 10:00 AM'
+ ),
+ array(
+ 'tz' => -12,
+ 'str' => '%A, %d %B %Y, %I:%M %p',
+ 'expected' => 'Saturday, 01 January 2011, 10:00 AM'
+ ),
+ array(
+ 'tz' => 99,
+ 'str' => 'Žluťoučký koníček %A',
+ 'expected' => 'Žluťoučký koníček Saturday'
+ ),
+ array(
+ 'tz' => 99,
+ 'str' => '言語設定言語 %A',
+ 'expected' => '言語設定言語 Saturday'
+ ),
+ array(
+ 'tz' => 99,
+ 'str' => '简体中文简体 %A',
+ 'expected' => '简体中文简体 Saturday'
+ ),
+ );
+
+ // Note: date_format_string() uses the timezone only to differenciate
+ // the server time from the UTC time. It does not modify the timestamp.
+ // Hence similar results for timezones <= 13.
+ // On different systems case of AM PM changes so compare case insensitive.
+ foreach ($tests as $test) {
+ $str = date_format_string(1293876000, $test['str'], $test['tz']);
+ $this->assertEquals(textlib::strtolower($test['expected']), textlib::strtolower($str));
+ }
+
+ // Restore system default values.
+ date_default_timezone_set($systemdefaulttimezone);
+ setlocale(LC_TIME, $oldlocale);
+ }
}

0 comments on commit 7773545

Please sign in to comment.
Something went wrong with that request. Please try again.