[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Shared/ -> TimeZone.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_Shared
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  
  29  
  30  /**
  31   * PHPExcel_Shared_TimeZone
  32   *
  33   * @category   PHPExcel
  34   * @package    PHPExcel_Shared
  35   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  36   */
  37  class PHPExcel_Shared_TimeZone
  38  {
  39      /*
  40       * Default Timezone used for date/time conversions
  41       *
  42       * @private
  43       * @var    string
  44       */
  45      protected static $timezone    = 'UTC';
  46  
  47      /**
  48       * Validate a Timezone name
  49       *
  50       * @param     string        $timezone            Time zone (e.g. 'Europe/London')
  51       * @return     boolean                        Success or failure
  52       */
  53      public static function _validateTimeZone($timezone)
  54      {
  55          if (in_array($timezone, DateTimeZone::listIdentifiers())) {
  56              return true;
  57          }
  58          return false;
  59      }
  60  
  61      /**
  62       * Set the Default Timezone used for date/time conversions
  63       *
  64       * @param     string        $timezone            Time zone (e.g. 'Europe/London')
  65       * @return     boolean                        Success or failure
  66       */
  67      public static function setTimeZone($timezone)
  68      {
  69          if (self::_validateTimezone($timezone)) {
  70              self::$timezone = $timezone;
  71              return true;
  72          }
  73          return false;
  74      }
  75  
  76  
  77      /**
  78       * Return the Default Timezone used for date/time conversions
  79       *
  80       * @return     string        Timezone (e.g. 'Europe/London')
  81       */
  82      public static function getTimeZone()
  83      {
  84          return self::$timezone;
  85      }
  86  
  87  
  88      /**
  89       *    Return the Timezone transition for the specified timezone and timestamp
  90       *
  91       *    @param        DateTimeZone         $objTimezone    The timezone for finding the transitions
  92       *    @param        integer                 $timestamp        PHP date/time value for finding the current transition
  93       *    @return         array                The current transition details
  94       */
  95      private static function getTimezoneTransitions($objTimezone, $timestamp)
  96      {
  97          $allTransitions = $objTimezone->getTransitions();
  98          $transitions = array();
  99          foreach ($allTransitions as $key => $transition) {
 100              if ($transition['ts'] > $timestamp) {
 101                  $transitions[] = ($key > 0) ? $allTransitions[$key - 1] : $transition;
 102                  break;
 103              }
 104              if (empty($transitions)) {
 105                  $transitions[] = end($allTransitions);
 106              }
 107          }
 108  
 109          return $transitions;
 110      }
 111  
 112      /**
 113       *    Return the Timezone offset used for date/time conversions to/from UST
 114       *    This requires both the timezone and the calculated date/time to allow for local DST
 115       *
 116       *    @param        string                 $timezone        The timezone for finding the adjustment to UST
 117       *    @param        integer                 $timestamp        PHP date/time value
 118       *    @return         integer                Number of seconds for timezone adjustment
 119       *    @throws        PHPExcel_Exception
 120       */
 121      public static function getTimeZoneAdjustment($timezone, $timestamp)
 122      {
 123          if ($timezone !== null) {
 124              if (!self::_validateTimezone($timezone)) {
 125                  throw new PHPExcel_Exception("Invalid timezone " . $timezone);
 126              }
 127          } else {
 128              $timezone = self::$timezone;
 129          }
 130  
 131          if ($timezone == 'UST') {
 132              return 0;
 133          }
 134  
 135          $objTimezone = new DateTimeZone($timezone);
 136          if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
 137              $transitions = $objTimezone->getTransitions($timestamp, $timestamp);
 138          } else {
 139              $transitions = self::getTimezoneTransitions($objTimezone, $timestamp);
 140          }
 141  
 142          return (count($transitions) > 0) ? $transitions[0]['offset'] : 0;
 143      }
 144  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1