You are here

public function FeedsDateTime::merge in Feeds 6

Same name and namespace in other branches
  1. 7.2 plugins/FeedsParser.inc \FeedsDateTime::merge()
  2. 7 plugins/FeedsParser.inc \FeedsDateTime::merge()

This function will keep this object's values by default.

File

plugins/FeedsParser.inc, line 533

Class

FeedsDateTime
Extend PHP DateTime class with granularity handling, merge functionality and slightly more flexible initialization parameters.

Code

public function merge(FeedsDateTime $other) {
  $other_tz = $other
    ->getTimezone();
  $this_tz = $this
    ->getTimezone();

  // Figure out which timezone to use for combination.
  $use_tz = $this
    ->hasGranularity('zone') || !$other
    ->hasGranularity('zone') ? $this_tz : $other_tz;
  $this2 = clone $this;
  $this2
    ->setTimezone($use_tz);
  $other
    ->setTimezone($use_tz);
  $val = $this2
    ->toArray();
  $otherval = $other
    ->toArray();
  foreach (self::$allgranularity as $g) {
    if ($other
      ->hasGranularity($g) && !$this2
      ->hasGranularity($g)) {

      // The other class has a property we don't; steal it.
      $this2
        ->addGranularity($g);
      $val[$g] = $otherval[$g];
    }
  }
  $other
    ->setTimezone($other_tz);
  $this2
    ->setDate($val['year'], $val['month'], $val['day']);
  $this2
    ->setTime($val['hour'], $val['minute'], $val['second']);
  return $this2;
}