Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1228
diff -u -p -r1.1228 common.inc
--- includes/common.inc	26 Sep 2010 23:31:35 -0000	1.1228
+++ includes/common.inc	29 Sep 2010 17:31:51 -0000
@@ -1830,13 +1830,14 @@ function format_interval($timestamp, $gr
  *
  * 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.
+ * as well as any admin-defined or custom format.
  *
  * @param $timestamp
  *   The exact date 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.
+ *   date formats, or the machine name of a format defined via the admin
+ *   interface. If "custom" is specified, then $format is required as well.
  * @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
@@ -1883,8 +1884,14 @@ function format_date($timestamp, $type =
       // No change to format.
       break;
     case 'medium':
-    default:
       $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
+      break;
+    default:
+      $format = variable_get('date_format_' . $type);
+      if (!isset($format)) {
+        // Not able to find the requested format, default to medium.
+        $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
+      }
   }
 
   // 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.127
diff -u -p -r1.127 common.test
--- modules/simpletest/tests/common.test	19 Sep 2010 18:10:41 -0000	1.127
+++ modules/simpletest/tests/common.test	29 Sep 2010 17:31:55 -0000
@@ -1967,6 +1967,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() {
