Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.470
diff -u -d -F^\s*function -r1.470 system.module
--- modules/system/system.module	20 Apr 2007 08:44:01 -0000	1.470
+++ modules/system/system.module	21 Apr 2007 10:21:58 -0000
@@ -809,29 +809,85 @@ function system_date_time_settings() {
   // Date settings:
   $zones = _system_zonelist();
 
-  // Date settings: possible date formats
-  $dateshort = array('Y-m-d H:i', 'm/d/Y - H:i', 'd/m/Y - H:i', 'Y/m/d - H:i',
-           'd.m.Y - H:i', 'm/d/Y - g:ia', 'd/m/Y - g:ia', 'Y/m/d - g:ia',
-           'M j Y - H:i', 'j M Y - H:i', 'Y M j - H:i',
-           'M j Y - g:ia', 'j M Y - g:ia', 'Y M j - g:ia');
-  $datemedium = array('D, Y-m-d H:i', 'D, m/d/Y - H:i', 'D, d/m/Y - H:i',
-          'D, Y/m/d - H:i', 'F j, Y - H:i', 'j F, Y - H:i', 'Y, F j - H:i',
-          'D, m/d/Y - g:ia', 'D, d/m/Y - g:ia', 'D, Y/m/d - g:ia',
-          'F j, Y - g:ia', 'j F Y - g:ia', 'Y, F j - g:ia', 'j. F Y - G:i');
-  $datelong = array('l, F j, Y - H:i', 'l, j F, Y - H:i', 'l, Y,  F j - H:i',
-        'l, F j, Y - g:ia', 'l, j F Y - g:ia', 'l, Y,  F j - g:ia', 'l, j. F Y - G:i');
+  // Define default date formats.
+  $dates = array(
+    'short' => array(
+      'Y-m-d H:i',    // ISO date: 2007-03-26 16:38
+      'm/d/Y - H:i',  // 03/26/2007 - 16:38
+      'm/d/Y - g:ia', // 03/26/2007 - 4:38pm
+      'M j Y - H:i',  // Sep 26 2007 - 16:38
+      'M j Y - g:ia', // Sep 26 2007 - 4:38pm
+    ),
+    'medium' => array(
+      'D, Y-m-d H:i',    // Wed, 2007-03-26 16:38
+      'D, m/d/Y - H:i',  // Wed, 03/26/2007 - 16:38
+      'D, m/d/Y - g:ia', // Wed, 03/26/2007 - 4:38pm
+      'F j, Y - H:i',    // September 26, 2007 - 16:38
+      'F j, Y - g:ia',   // September 26, 2007 - 4:38pm
+    ),
+    'long' => array(
+      'l, F j, Y - H:i',  // Wednesday, September 26, 2007 - 16:38
+      'l, F j, Y - g:ia', // Wednesday, September 26, 2007 - 4:38pm
+    ),
+  );
 
-  // Date settings: construct choices for user
-  foreach ($dateshort as $f) {
-    $dateshortchoices[$f] = format_date(time(), 'custom', $f);
-  }
-  foreach ($datemedium as $f) {
-    $datemediumchoices[$f] = format_date(time(), 'custom', $f);
+  // Get localized date formats
+  global $language;
+  if (function_exists('locale') && $language->language != 'en') {
+    $localized_dates = array(
+      'short' => trim(t('@short: m/d/Y - H:i', array('@short:' => ''))),
+      'medium' => trim(t('@medium: D, m/d/Y - H:i', array('@medium:' => ''))),
+      'long' => trim(t('@long: l, F j, Y - H:i', array('@long:' => ''))),
+    );
+
+    // If the date format does not yet exist, it's added to the array.
+    foreach ($localized_dates as $type => $format) {
+      if (!empty($format) && !in_array($format, $dates[$type])) {
+        $dates[$type][] = $format;
+      }
+    }
   }
-  foreach ($datelong as $f) {
-    $datelongchoices[$f] = format_date(time(), 'custom', $f);
+
+  // Stores the default selections for each date format and the custom dates.
+  $default_dates = array();
+  $custom_dates = array();
+  $now = time();
+
+  foreach (array_keys($dates) as $type) {
+    // Expand the dates.
+    $dates[$type] = array_flip($dates[$type]);
+    foreach ($dates[$type] as $format => $title) {
+      $dates[$type][$format] = format_date($now, 'custom', $format);
+    }
+
+    // Custom dates
+    $custom = variable_get('date_format_'. $type, NULL);
+
+    // Add the item for a custom date format.
+    $dates[$type]['custom'] = '<'. t('custom') .'>';
+
+    // Select the right default values.
+    if (empty($custom)) {
+      // If the date format is unset or an empty string,
+      // set the default selection to the first array element.
+      reset($dates[$type]);
+      $default_dates[$type] = key($dates[$type]);
+      $custom_dates[$type] = '';
+    }
+    elseif (isset($dates[$type][$custom])) {
+      // The date from the setting is a default date, so don't display it as custom.
+      $default_dates[$type] = $custom;
+      $custom_dates[$type] = '';
+    }
+    else {
+      // We have a real custom date format.
+      $default_dates[$type] = 'custom';
+      $custom_dates[$type] = $custom;
+    }
   }
 
+  $custom_help_text = t('See the <a href="@url">PHP manual page</a> for a detailed description of allowed characters.', array('@url' => 'http://php.net/date'));
+
   $form['date_default_timezone'] = array(
     '#type' => 'select',
     '#title' => t('Default time zone'),
@@ -851,27 +907,54 @@ function system_date_time_settings() {
   $form['date_format_short'] = array(
     '#type' => 'select',
     '#title' => t('Short date format'),
-    '#default_value' => variable_get('date_format_short', $dateshort[1]),
-    '#options' => $dateshortchoices,
+    '#default_value' => $default_dates['short'],
+    '#options' => $dates['short'],
     '#description' => t('The short format of date display.')
   );
 
+  $form['date_format_short_custom'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Custom short date format'),
+    '#default_value' => $custom_dates['short'],
+    '#description' => t('A custom short date format.') .' '. $custom_help_text,
+    '#size' => 40,
+    '#maxlength' => 255,
+  );
+
   $form['date_format_medium'] = array(
     '#type' => 'select',
     '#title' => t('Medium date format'),
-    '#default_value' => variable_get('date_format_medium', $datemedium[1]),
-    '#options' => $datemediumchoices,
+    '#default_value' => $default_dates['medium'],
+    '#options' =>$dates['medium'],
     '#description' => t('The medium sized date display.')
   );
 
+  $form['date_format_medium_custom'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Custom medium date format'),
+    '#default_value' => $custom_dates['medium'],
+    '#description' => t('A custom medium sized date format.') .' '. $custom_help_text,
+    '#size' => 40,
+    '#maxlength' => 255,
+  );
+
   $form['date_format_long'] = array(
     '#type' => 'select',
     '#title' => t('Long date format'),
-    '#default_value' => variable_get('date_format_long', $datelong[0]),
-    '#options' => $datelongchoices,
+    '#default_value' => $default_dates['long'],
+    '#options' => $dates['long'],
     '#description' => t('Longer date format used for detailed display.')
   );
 
+  $form['date_format_long_custom'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Custom long date format'),
+    '#default_value' => $custom_dates['long'],
+    '#description' => t('Custom longer date format used for detailed display.') .' '. $custom_help_text,
+    '#size' => 40,
+    '#maxlength' => 255,
+  );
+
   $form['date_first_day'] = array(
     '#type' => 'select',
     '#title' => t('First day of week'),
@@ -880,7 +963,20 @@ function system_date_time_settings() {
     '#description' => t('The first day of the week for calendar views.')
   );
 
-  return system_settings_form($form);
+  $form['#submit']['system_date_time_settings_submit'] = array();
+
+  $form = system_settings_form($form);
+  return $form;
+}
+
+function system_date_time_settings_submit($form_id, $form_values) {
+  // Save the custom date format strings in the right variables
+  foreach (array('short', 'medium', 'long') as $type) {
+    if ($form_values['date_format_'. $type] == 'custom') {
+      $form_values['date_format_'. $type] = $form_values['date_format_'. $type .'_custom'];
+    }
+    unset($form_values['date_format_'. $type .'_custom']);
+  }
 }
 
 function system_site_maintenance_settings() {
