Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.925
diff -u -p -r1.925 common.inc
--- includes/common.inc	18 Jun 2009 21:19:01 -0000	1.925
+++ includes/common.inc	30 Jun 2009 20:09:26 -0000
@@ -1909,36 +1909,33 @@ function format_date($timestamp, $type =
       $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') {
+  // Replace characters in format string which need to be translated, 'X', with
+  // an arbitrary escape sequence '+=X=+'. The 'F' format character, long month
+  // name, requires special handling (see below). These patterns exclude any
+  // format characters which have been escaped with '\' (octal 134).
+  $format = preg_replace(array('@\134{2}@', '@(?<!\134)r@', '@(?<!\134)F@', '@(?<!\134)([AaeDlMT])@', '@\+==\+@'), array('+==+', 'D, d M Y H:i:s O', '+=!F=+', '+=$1=+', '\\\\\\\\'), $format);
+
+  // Make a single call to date_format() using the format string.
+  $date = date_format($date_time, $format);
+
+  // Now look for any escape sequences and translate them.
+  if (preg_match_all('@\+=(!?)([a-zA-Z/_-]+)=\+@', $date, $matches)) {
+    foreach ($matches[2] as $key => $match) {
+      $options = array('langcode' => $langcode);
       // 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);
-    }
-    elseif ($c == '\\') {
-      $date .= $format[++$i];
-    }
-    else {
-      $date .= $c;
+      if ($matches[1][$key] == '!') {
+        $options['context'] = 'Long month name';
+      }
+      $replace[] = t($match, array(), $options);
     }
+    $date = str_replace($matches[0], $replace, $date);
   }
 
   return $date;
