Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1264
diff -u -p -r1.1264 common.inc
--- includes/common.inc	22 Nov 2010 05:22:43 -0000	1.1264
+++ includes/common.inc	25 Nov 2010 18:36:36 -0000
@@ -1815,26 +1815,28 @@ function format_interval($timestamp, $gr
 }
 
 /**
- * Format a date with the given configured format or a custom format string.
- *
- * Drupal allows administrators to select formatting strings for 'short',
- * 'medium' and 'long' date formats. This function can handle these formats,
- * as well as any custom format.
+ * Formats a date, using a date type or a custom date format string.
  *
  * @param $timestamp
- *   The exact date to format, as a UNIX timestamp.
+ *   The date/time to format, as a UNIX timestamp.
  * @param $type
- *   The format to use. Can be "short", "medium" or "long" for the preconfigured
- *   date formats. If "custom" is specified, then $format is required as well.
+ *   (optional) The format to use, one of:
+ *   - 'short', 'medium', or 'long' (the corresponding built-in date formats);
+ *     'medium' is the default.
+ *   - The name of a date type defined by a module in hook_date_format_types().
+ *   - The machine name of an administrator-defined date format.
+ *   - 'custom', to use $format.
+ *   Defaults to 'medium'.
  * @param $format
- *   A PHP date format string as required by date(). A backslash should be used
- *   before a character to avoid interpreting the character as part of a date
- *   format.
+ *   (optional) If $type is 'custom', a PHP date format string suitable for
+ *   input to date(). Use a backslash to escape ordinary text, so it does not
+ *   get interpreted as date format characters.
  * @param $timezone
- *   Time zone identifier; if omitted, the user's time zone is used.
+ *   (optional) Time zone identifier. Defaults to the user's time zone.
  * @param $langcode
- *   Optional language code to translate to a language other than what is used
- *   to display the page.
+ *   (optional) Language code to translate to. Defaults to the language used to
+ *   display the page.
+ *
  * @return
  *   A translated date string in the requested format.
  */
@@ -1865,15 +1867,24 @@ function format_date($timestamp, $type =
     case 'short':
       $format = variable_get('date_format_short', 'm/d/Y - H:i');
       break;
+
     case 'long':
       $format = variable_get('date_format_long', 'l, F j, Y - H:i');
       break;
+
     case 'custom':
       // No change to format.
       break;
-    case 'medium':
+
     default:
+      $format = variable_get('date_format_' . $type, NULL);
+      if (isset($format)) {
+        break;
+      }
+      // Fall-through to 'medium', if requested format is undefined.
+    case 'medium':
       $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
+      break;
   }
 
   // Create a DateTime object from the timestamp.
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.134
diff -u -p -r1.134 common.test
--- modules/simpletest/tests/common.test	22 Nov 2010 05:22:43 -0000	1.134
+++ modules/simpletest/tests/common.test	25 Nov 2010 18:29:47 -0000
@@ -2091,6 +2091,32 @@ class FormatDateUnitTest extends DrupalW
   }
 
   /**
+   * Test admin-defined formats in format_date().
+   */
+  function testAdminDefinedFormatDate() {
+    // Create an admin user.
+    $this->admin_user = $this->drupalCreateUser(array('administer site configuration'));
+    $this->drupalLogin($this->admin_user);
+
+    // Add new date format.
+    $admin_date_format = 'j M y';
+    $edit = array('date_format' => $admin_date_format);
+    $this->drupalPost('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
+
+    // Add new date type.
+    $edit = array(
+      'date_type' => 'Example Style',
+      'machine_name' => 'example_style',
+      'date_format' => $admin_date_format,
+    );
+    $this->drupalPost('admin/config/regional/date-time/types/add', $edit, t('Add date type'));
+
+    $timestamp = strtotime('2007-03-10T00:00:00+00:00');
+    $this->assertIdentical(format_date($timestamp, 'example_style', '', 'America/Los_Angeles'), '9 Mar 07', t('Test format_date() using an admin-defined date type.'));
+    $this->assertIdentical(format_date($timestamp, 'undefined_style'), format_date($timestamp, 'medium'), t('Test format_date() defaulting to medium when $type not found.'));
+  }
+
+  /**
    * Tests for the format_date() function.
    */
   function testFormatDate() {
