You are here

public function FeedsDateTime::setTimezone in Feeds 7

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

Overrides default DateTime function. Only changes output values if actually had time granularity. This should be used as a "converter" for output, to switch tzs.

In order to set a timezone for a datetime that doesn't have such granularity, merge() it with one that does.

1 call to FeedsDateTime::setTimezone()
FeedsDateTime::__construct in plugins/FeedsParser.inc
Overridden constructor.

File

plugins/FeedsParser.inc, line 530

Class

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

Code

public function setTimezone(DateTimeZone $tz, $force = FALSE) {

  // PHP 5.2.6 has a fatal error when setting a date's timezone to itself.
  // http://bugs.php.net/bug.php?id=45038
  if (version_compare(PHP_VERSION, '5.2.7', '<') && $tz == $this
    ->getTimezone()) {
    $tz = new DateTimeZone($tz
      ->getName());
  }
  if (!$this
    ->hasTime() || !$this
    ->hasGranularity('zone') || $force) {

    // this has no time or timezone granularity, so timezone doesn't mean much
    // We set the timezone using the method, which will change the day/hour, but then we switch back
    $arr = $this
      ->toArray();
    parent::setTimezone($tz);
    $this
      ->setDate($arr['year'], $arr['month'], $arr['day']);
    $this
      ->setTime($arr['hour'], $arr['minute'], $arr['second']);
    return;
  }
  parent::setTimezone($tz);
}