From 3ffb9f4a6e54256f1e2f97a8a360f7fdd71dcf76 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Wed, 8 Jul 2009 20:40:21 +0000 Subject: - Patch #243129 by jrchamp, DamZ, mfb, catch, justinrandell, macgirvin, et al: faster implementation of format_date(). Woot. --- includes/common.inc | 73 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 23 deletions(-) (limited to 'includes') diff --git a/includes/common.inc b/includes/common.inc index c9d68fd82..348390e33 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -1894,6 +1894,12 @@ function format_date($timestamp, $type = 'medium', $format = '', $timezone = NUL $timezones[$timezone] = timezone_open($timezone); } + // Use the default langcode if none is set. + global $language; + if (empty($langcode)) { + $langcode = isset($language->language) ? $language->language : 'en'; + } + switch ($type) { case 'small': $format = variable_get('date_format_short', 'm/d/Y - H:i'); @@ -1909,39 +1915,60 @@ function format_date($timestamp, $type = 'medium', $format = '', $timezone = NUL $format = variable_get('date_format_medium', 'D, m/d/Y - H:i'); } - $max = strlen($format); - $date = ''; // Create a DateTime object from the timestamp. $date_time = date_create('@' . $timestamp); // Set the time zone for the DateTime object. date_timezone_set($date_time, $timezones[$timezone]); - for ($i = 0; $i < $max; $i++) { - $c = $format[$i]; - if (strpos('AaeDlMT', $c) !== FALSE) { - $date .= t(date_format($date_time, $c), array(), array('langcode' => $langcode)); - } - elseif ($c == 'F') { - // Special treatment for long month names: May is both an abbreviation - // and a full month name in English, but other languages have - // different abbreviations. - $date .= t(date_format($date_time, $c), array(), array('context' => 'Long month name', 'langcode' => $langcode)); - } - elseif (strpos('BcdGgHhIijLmNnOoPSstUuWwYyZz', $c) !== FALSE) { - $date .= date_format($date_time, $c); - } - elseif ($c == 'r') { - $date .= format_date($timestamp, 'custom', 'D, d M Y H:i:s O', $timezone, $langcode); + // Encode markers that should be translated. 'A' becomes '\xEF\AA\xFF'. + // xEF and xFF are invalid UTF-8 sequences, and we assume they are not in the + // input string. + // Paired backslashes are isolated to prevent errors in read-ahead evaluation. + // The read-ahead expression ensures that A matches, but not \A. + $format = preg_replace(array('/\\\\\\\\/', '/(? $langcode, + ); + + if ($code == 'F') { + $options['context'] = 'Long month name'; } - elseif ($c == '\\') { - $date .= $format[++$i]; + + if ($code == '') { + $cache[$langcode][$code][$string] = $string; } else { - $date .= $c; + $cache[$langcode][$code][$string] = t($string, array(), $options); } } - - return $date; + return $cache[$langcode][$code][$string]; } /** -- cgit v1.2.3