Index: date_deploy.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/deploy/modules/date_deploy/Attic/date_deploy.module,v
retrieving revision 1.1.2.5
diff -u -p -r1.1.2.5 date_deploy.module
--- date_deploy.module	12 Mar 2010 00:04:55 -0000	1.1.2.5
+++ date_deploy.module	18 Oct 2010 22:45:38 -0000
@@ -1,28 +1,93 @@
 <?php
 // $Id: date_deploy.module,v 1.1.2.5 2010/03/12 00:04:55 heyrocker Exp $
 /**
- * Get an array listing the names of all date fields.
+ * Get an array of all date fields.
  *
  * @return
- *   Array of all created date fields
+ *   Array of date fields, keyed by name.
  **/
 function date_deploy_get_date_fields() {
-  // This isn't changing much, so cache it to save some queries
+
   static $date_fields = array();
-  
+
   if (empty($date_fields)) {
-    $fields = content_fields();
+    // TODO: determine a better way to grab all available date types
     $date_types = array('datetime', 'date', 'datestamp');
+    $fields = content_fields();
+
     foreach ($fields as $name => $field) {
       if (in_array($field['type'], $date_types)) {
-        $date_fields[$name] = $name;
+        $date_fields[$name] = $field;
       }
     }
+
   }
-  
+
   return $date_fields;
+
 }
 
+
+/**
+ * Rebuild a date field array. The array structure varies based on widget type.
+ *
+ * @param $date
+ *   An array of date information via a node_load() call.
+ * @param $field
+ *   An array of information for this specific date field.
+ * @return
+ *   An array of date information suitable for drupal_execute() calls.
+ */
+function date_deploy_rebuild_date($date, $field) {
+
+  $rebuild     = array();
+  $timezone_db = new DateTimeZone($date['timezone_db']);
+  $timezone    = new DateTimeZone($date['timezone']);
+  $format      = isset($field['widget']['input_format_custom']) && $field['widget']['input_format_custom'] ? $field['widget']['input_format_custom'] : $field['widget']['input_format'];
+
+  // Some date fields have a to and from date, so we'll need to account for
+  // that.
+  $value['value'] = new DateTime($date['value'], $timezone_db);
+  $value['value']->setTimezone($timezone);
+
+  if (isset($date['value2']) ) {
+    $value['value2'] = new DateTime($date['value2'], $timezone_db);
+    $value['value2']->setTimezone($timezone);
+  }
+
+  foreach($value as $key => $datetime) {
+
+    switch ($field['widget']['type']) {
+      // date_popup requires both "date" and "time" data. Granularity has been
+      // taken into account in the format value available in the field definition.
+      case 'date_popup':
+        $rebuild[$key]['date'] = $datetime->format(date_limit_format($format, array('year', 'month', 'day') ) );
+        $rebuild[$key]['time'] = $datetime->format(date_limit_format($format, array('hour', 'minute', 'second') ) );
+        break;
+  
+      // date_select requires a breakdown of each element of the date. Granularity
+      // will be taken into account during validation, so we don't need to handle
+      // it here. For whatever reason "0" is not valid via Form API for "minute",
+      // only "00".
+      case 'date_select':
+        $parts       = date_parse($datetime->format($format) );
+        $rebuild[$key] = array_intersect_key($parts, array_flip(array('year', 'month', 'day', 'hour', 'minute', 'second') ) );
+        $rebuild[$key]['minute'] = str_pad($value['minute'], 2, '0', STR_PAD_LEFT);
+        break;
+  
+      // Granularity has yet to be taken into account, so we'll have to handle it.
+      default:
+        $rebuild[$key]['date'] = $datetime->format(date_limit_format($format, $field['granularity']) );
+        break;
+    }
+
+  }
+
+  return $rebuild;
+
+}
+
+
 /**
  * Implementation of hook_node_deploy(),
  *
@@ -44,23 +109,16 @@ function date_node_deploy(&$node) {
   if (!$original_node = node_load($node->nid)) {
     return;
   }  
-  $date_fields = date_deploy_get_date_fields();
-  
-  // The dates need to be saved as $node->field[delta]['value'][part]
-  foreach ($date_fields as $field_name) {
+  $fields = date_deploy_get_date_fields();
+
+  foreach ($fields as $field_name => $field) {
     if (property_exists($original_node, $field_name)) {
-      unset($node->{$field_name});
       foreach ($original_node->{$field_name} as $key => $date_value) {
         if (!is_null($date_value['value'])) {
-          $date_parts = date_parse($date_value['value']);
-          $node->{$field_name}[$key]['value']['year'] = $date_parts['year'];
-          $node->{$field_name}[$key]['value']['month'] = $date_parts['month'];
-          $node->{$field_name}[$key]['value']['day'] = $date_parts['day'];
-          $node->{$field_name}[$key]['value']['hour'] = $date_parts['hour'];
-          $node->{$field_name}[$key]['value']['minute'] = $date_parts['minute'];
-          $node->{$field_name}[$key]['value']['second'] = $date_parts['second'];
+          unset($node->{$field_name});
+          $node->{$field_name}[$key] = date_deploy_rebuild_date($date_value, $field);
         }
       }
     }
-  } 
-}
+  }
+}
\ No newline at end of file
