FeedsDateTimeElement.php in Feeds 8.2        
                          
                  
                        
  
  
  
  
  
File
  lib/Drupal/feeds/FeedsDateTimeElement.php
  
    View source  
  <?php
namespace Drupal\feeds;
use DateTimeZone;
class FeedsDateTimeElement extends FeedsElement {
  
  public $start;
  public $end;
  
  public function __construct($start = NULL, $end = NULL, $tz = NULL) {
    $this->start = !isset($start) || $start instanceof FeedsDateTime ? $start : new FeedsDateTime($start, $tz);
    $this->end = !isset($end) || $end instanceof FeedsDateTime ? $end : new FeedsDateTime($end, $tz);
  }
  
  public function getValue() {
    if ($this->start) {
      return $this->start
        ->format('U');
    }
    return '0';
  }
  
  public function merge(FeedsDateTimeElement $other) {
    $this2 = clone $this;
    if ($this->start && $other->start) {
      $this2->start = $this->start
        ->merge($other->start);
    }
    elseif ($other->start) {
      $this2->start = clone $other->start;
    }
    elseif ($this->start) {
      $this2->start = clone $this->start;
    }
    if ($this->end && $other->end) {
      $this2->end = $this->end
        ->merge($other->end);
    }
    elseif ($other->end) {
      $this2->end = clone $other->end;
    }
    elseif ($this->end) {
      $this2->end = clone $this->end;
    }
    return $this2;
  }
  
  protected static function readDateField($entity, $field_name, $delta = 0) {
    $ret = new FeedsDateTimeElement();
    if (isset($entity->{$field_name}['und'][$delta]['date']) && $entity->{$field_name}['und'][$delta]['date'] instanceof FeedsDateTime) {
      $ret->start = $entity->{$field_name}['und'][$delta]['date'];
    }
    if (isset($entity->{$field_name}['und'][$delta]['date2']) && $entity->{$field_name}['und'][$delta]['date2'] instanceof FeedsDateTime) {
      $ret->end = $entity->{$field_name}['und'][$delta]['date2'];
    }
    return $ret;
  }
  
  public function buildDateField($entity, $field_name, $delta = 0) {
    $info = field_info_field($field_name);
    $oldfield = FeedsDateTimeElement::readDateField($entity, $field_name, $delta);
    
    $oldfield = $this
      ->merge($oldfield);
    $use_start = $oldfield->start;
    $use_end = $oldfield->end;
    
    $temp = new FeedsDateTime(NULL, new DateTimeZone(DATETIME_STORAGE_TIMEZONE));
    if ($use_start) {
      $use_start = $use_start
        ->merge($temp);
      $use_start
        ->setTimezone(new DateTimeZone(DATETIME_STORAGE_TIMEZONE));
    }
    if ($use_end) {
      $use_end = $use_end
        ->merge($temp);
      $use_end
        ->setTimezone(new DateTimeZone(DATETIME_STORAGE_TIMEZONE));
    }
    $db_tz = new DateTimeZone(DATETIME_STORAGE_TIMEZONE);
    if (!isset($entity->{$field_name})) {
      $entity->{$field_name} = array(
        'und' => array(),
      );
    }
    if ($use_start) {
      $entity->{$field_name}['und'][$delta]['timezone'] = $use_start
        ->getTimezone()
        ->getName();
      $entity->{$field_name}['und'][$delta]['offset'] = $use_start
        ->getOffset();
      $use_start
        ->setTimezone($db_tz);
      $entity->{$field_name}['und'][$delta]['date'] = $use_start;
      
      $entity->{$field_name}['und'][$delta]['value'] = $use_start
        ->format(DATETIME_DATETIME_STORAGE_FORMAT);
    }
    if ($use_end) {
      
      $entity->{$field_name}['und'][$delta]['offset2'] = $use_end
        ->getOffset();
      $use_end
        ->setTimezone($db_tz);
      $entity->{$field_name}['und'][$delta]['date2'] = $use_end;
      $entity->{$field_name}['und'][$delta]['value2'] = $use_end
        ->format(DATETIME_DATETIME_STORAGE_FORMAT);
    }
  }
}