[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/data/Date/ -> TimeZone.class.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4: */
   3  //
   4  // +----------------------------------------------------------------------+
   5  // | PHP Version 4                                                        |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 1997-2003 The PHP Group                                |
   8  // +----------------------------------------------------------------------+
   9  // | This source file is subject to version 2.02 of the PHP license,      |
  10  // | that is bundled with this package in the file LICENSE, and is        |
  11  // | available at through the world-wide-web at                           |
  12  // | http://www.php.net/license/2_02.txt.                                 |
  13  // | If you did not receive a copy of the PHP license and are unable to   |
  14  // | obtain it through the world-wide-web, please send a note to          |
  15  // | license@php.net so we can mail you a copy immediately.               |
  16  // +----------------------------------------------------------------------+
  17  // | Authors: Baba Buehler <baba@babaz.com>                               |
  18  // |                                                                      |
  19  // +----------------------------------------------------------------------+
  20  //
  21  // $Id: TimeZone.php,v 1.6 2004/05/16 12:48:06 pajoye Exp $
  22  //
  23  // Date_TimeZone Class
  24  //
  25  
  26  /**
  27   * \ingroup Data
  28   *
  29   * TimeZone representation class, along with time zone information data.
  30   *
  31   * TimeZone representation class, along with time zone information data.
  32   * The default timezone is set from the first valid timezone id found
  33   * in one of the following places, in this order: <br>
  34   * 1) global $_DATE_TIMEZONE_DEFAULT<br>
  35   * 2) system environment variable PHP_TZ<br>
  36   * 3) system environment variable TZ<br>
  37   * 4) the result of date('T')<br>
  38   * If no valid timezone id is found, the default timezone is set to 'UTC'.
  39   * You may also manually set the default timezone by passing a valid id to
  40   * Date_TimeZone::setDefault().<br>
  41   *
  42   * This class includes time zone data (from zoneinfo) in the form of a global array, $_DATE_TIMEZONE_DATA.
  43   *
  44   *
  45   * @author Baba Buehler <baba@babaz.com>
  46   * @access public
  47   * @version 1.0
  48   */
  49  class Date_TimeZone
  50  {
  51      /**
  52       * Time Zone ID of this time zone
  53       * @var string
  54       */
  55      var $id;
  56      /**
  57       * Long Name of this time zone (ie Central Standard Time)
  58       * @var string
  59       */
  60      var $longname;
  61      /**
  62       * Short Name of this time zone (ie CST)
  63       * @var string
  64       */
  65      var $shortname;
  66      /**
  67       * true if this time zone observes daylight savings time
  68       * @var boolean
  69       */
  70      var $hasdst;
  71      /**
  72       * DST Long Name of this time zone
  73       * @var string
  74       */
  75      var $dstlongname;
  76      /**
  77       * DST Short Name of this timezone
  78       * @var string
  79       */
  80      var $dstshortname;
  81      /**
  82       * offset, in milliseconds, of this timezone
  83       * @var int
  84       */
  85      var $offset;
  86  
  87      /**
  88       * System Default Time Zone
  89       * @var object Date_TimeZone
  90       */
  91      var $default;
  92  
  93  
  94      /**
  95       * Constructor
  96       *
  97       * Creates a new Date::TimeZone object, representing the time zone
  98       * specified in $id.  If the supplied ID is invalid, the created
  99       * time zone is UTC.
 100       *
 101       * @access public
 102       * @param string $id the time zone id
 103       * @return object Date_TimeZone the new Date_TimeZone object
 104       */
 105      function Date_TimeZone($id)
 106      {
 107          global $_DATE_TIMEZONE_DATA;
 108          if(Date_TimeZone::isValidID($id)) {
 109              $this->id = $id;
 110              $this->longname = $_DATE_TIMEZONE_DATA[$id]['longname'];
 111              $this->shortname = $_DATE_TIMEZONE_DATA[$id]['shortname'];
 112              $this->offset = $_DATE_TIMEZONE_DATA[$id]['offset'];
 113              if($_DATE_TIMEZONE_DATA[$id]['hasdst']) {
 114                  $this->hasdst = true;
 115                  $this->dstlongname = $_DATE_TIMEZONE_DATA[$id]['dstlongname'];
 116                  $this->dstshortname = $_DATE_TIMEZONE_DATA[$id]['dstshortname'];
 117              } else {
 118                  $this->hasdst = false;
 119                  $this->dstlongname = $this->longname;
 120                  $this->dstshortname = $this->shortname;
 121              }
 122          } else {
 123              $this->id = 'UTC';
 124              $this->longname = $_DATE_TIMEZONE_DATA[$this->id]['longname'];
 125              $this->shortname = $_DATE_TIMEZONE_DATA[$this->id]['shortname'];
 126              $this->hasdst = $_DATE_TIMEZONE_DATA[$this->id]['hasdst'];
 127              $this->offset = $_DATE_TIMEZONE_DATA[$this->id]['offset'];
 128          }
 129      }
 130  
 131      /**
 132       * Return a TimeZone object representing the system default time zone
 133       *
 134       * Return a TimeZone object representing the system default time zone,
 135       * which is initialized during the loading of TimeZone.php.
 136       *
 137       * @access public
 138       * @return object Date_TimeZone the default time zone
 139       */
 140      function getDefault()
 141      {
 142          global $_DATE_TIMEZONE_DEFAULT;
 143          return new Date_TimeZone($_DATE_TIMEZONE_DEFAULT);
 144      }
 145  
 146      /**
 147       * Sets the system default time zone to the time zone in $id
 148       *
 149       * Sets the system default time zone to the time zone in $id
 150       *
 151       * @access public
 152       * @param string $id the time zone id to use
 153       */
 154      function setDefault($id)
 155      {
 156          global $_DATE_TIMEZONE_DEFAULT;
 157          if(Date_TimeZone::isValidID($id)) {
 158              $_DATE_TIMEZONE_DEFAULT = $id;
 159          }
 160      }
 161  
 162      /**
 163       * Tests if given id is represented in the $_DATE_TIMEZONE_DATA time zone data
 164       *
 165       * Tests if given id is represented in the $_DATE_TIMEZONE_DATA time zone data
 166       *
 167       * @access public
 168       * @param string $id the id to test
 169       * @return boolean true if the supplied ID is valid
 170       */
 171      function isValidID($id)
 172      {
 173          global $_DATE_TIMEZONE_DATA;
 174          if(isset($_DATE_TIMEZONE_DATA[$id])) {
 175              return true;
 176          } else {
 177              return false;
 178          }
 179      }
 180  
 181      /**
 182       * Is this time zone equal to another
 183       *
 184       * Tests to see if this time zone is equal (ids match)
 185       * to a given Date_TimeZone object.
 186       *
 187       * @access public
 188       * @param object Date_TimeZone $tz the timezone to test
 189       * @return boolean true if this time zone is equal to the supplied time zone
 190       */
 191      function isEqual($tz)
 192      {
 193          if(strcasecmp($this->id, $tz->id) == 0) {
 194              return true;
 195          } else {
 196              return false;
 197          }
 198      }
 199  
 200      /**
 201       * Is this time zone equivalent to another
 202       *
 203       * Tests to see if this time zone is equivalent to
 204       * a given time zone object.  Equivalence in this context
 205       * is defined by the two time zones having an equal raw
 206       * offset and an equal setting of "hasdst".  This is not true
 207       * equivalence, as the two time zones may have different rules
 208       * for the observance of DST, but this implementation does not
 209       * know DST rules.
 210       *
 211       * @access public
 212       * @param object Date_TimeZone $tz the timezone object to test
 213       * @return boolean true if this time zone is equivalent to the supplied time zone
 214       */
 215      function isEquivalent($tz)
 216      {
 217          if($this->offset == $tz->offset && $this->hasdst == $tz->hasdst) {
 218              return true;
 219          } else {
 220              return false;
 221          }
 222      }
 223  
 224      /**
 225       * Returns true if this zone observes daylight savings time
 226       *
 227       * Returns true if this zone observes daylight savings time
 228       *
 229       * @access public
 230       * @return boolean true if this time zone has DST
 231       */
 232      function hasDaylightTime()
 233      {
 234          return $this->hasdst;
 235      }
 236  
 237      /**
 238       * Is the given date/time in DST for this time zone
 239       *
 240       * Attempts to determine if a given Date object represents a date/time
 241       * that is in DST for this time zone.  WARNINGS: this basically attempts to
 242       * "trick" the system into telling us if we're in DST for a given time zone.
 243       * This uses putenv() which may not work in safe mode, and relies on unix time
 244       * which is only valid for dates from 1970 to ~2038.  This relies on the
 245       * underlying OS calls, so it may not work on Windows or on a system where
 246       * zoneinfo is not installed or configured properly.
 247       *
 248       * @access public
 249       * @param object Date $date the date/time to test
 250       * @return boolean true if this date is in DST for this time zone
 251       */
 252      function inDaylightTime($date)
 253      {
 254          $env_tz = "";
 255          if(getenv("TZ")) {
 256              $env_tz = getenv("TZ");
 257          }
 258          putenv("TZ=".$this->id);
 259          $ltime = localtime($date->getTime(), true);
 260          putenv("TZ=".$env_tz);
 261          return $ltime['tm_isdst'];
 262      }
 263  
 264      /**
 265       * Get the DST offset for this time zone
 266       *
 267       * Returns the DST offset of this time zone, in milliseconds,
 268       * if the zone observes DST, zero otherwise.  Currently the
 269       * DST offset is hard-coded to one hour.
 270       *
 271       * @access public
 272       * @return int the DST offset, in milliseconds or zero if the zone does not observe DST
 273       */
 274      function getDSTSavings()
 275      {
 276          if($this->hasdst) {
 277              return 3600000;
 278          } else {
 279              return 0;
 280          }
 281      }
 282  
 283      /**
 284       * Get the DST-corrected offset to UTC for the given date
 285       *
 286       * Attempts to get the offset to UTC for a given date/time, taking into
 287       * account daylight savings time, if the time zone observes it and if
 288       * it is in effect.  Please see the WARNINGS on Date::TimeZone::inDaylightTime().
 289       *
 290       *
 291       * @access public
 292       * @param object Date $date the Date to test
 293       * @return int the corrected offset to UTC in milliseconds
 294       */
 295      function getOffset($date)
 296      {
 297          if($this->inDaylightTime($date)) {
 298              return $this->offset + $this->getDSTSavings();
 299          } else {
 300              return $this->offset;
 301          }
 302      }
 303  
 304      /**
 305       * Returns the list of valid time zone id strings
 306       *
 307       * Returns the list of valid time zone id strings
 308       *
 309       * @access public
 310       * @return mixed an array of strings with the valid time zone IDs
 311       */
 312      function getAvailableIDs()
 313      {
 314          global $_DATE_TIMEZONE_DATA;
 315          return array_keys($_DATE_TIMEZONE_DATA);
 316      }
 317  
 318      /**
 319       * Returns the id for this time zone
 320       *
 321       * Returns the time zone id  for this time zone, i.e. "America/Chicago"
 322       *
 323       * @access public
 324       * @return string the id
 325       */
 326      function getID()
 327      {
 328          return $this->id;
 329      }
 330  
 331      /**
 332       * Returns the long name for this time zone
 333       *
 334       * Returns the long name for this time zone,
 335       * i.e. "Central Standard Time"
 336       *
 337       * @access public
 338       * @return string the long name
 339       */
 340      function getLongName()
 341      {
 342          return $this->longname;
 343      }
 344  
 345      /**
 346       * Returns the short name for this time zone
 347       *
 348       * Returns the short name for this time zone, i.e. "CST"
 349       *
 350       * @access public
 351       * @return string the short name
 352       */
 353      function getShortName()
 354      {
 355          return $this->shortname;
 356      }
 357  
 358      /**
 359       * Returns the DST long name for this time zone
 360       *
 361       * Returns the DST long name for this time zone, i.e. "Central Daylight Time"
 362       *
 363       * @access public
 364       * @return string the daylight savings time long name
 365       */
 366      function getDSTLongName()
 367      {
 368          return $this->dstlongname;
 369      }
 370  
 371      /**
 372       * Returns the DST short name for this time zone
 373       *
 374       * Returns the DST short name for this time zone, i.e. "CDT"
 375       *
 376       * @access public
 377       * @return string the daylight savings time short name
 378       */
 379      function getDSTShortName()
 380      {
 381          return $this->dstshortname;
 382      }
 383  
 384      /**
 385       * Returns the raw (non-DST-corrected) offset from UTC/GMT for this time zone
 386       *
 387       * Returns the raw (non-DST-corrected) offset from UTC/GMT for this time zone
 388       *
 389       * @access public
 390       * @return int the offset, in milliseconds
 391       */
 392      function getRawOffset()
 393      {
 394          return $this->offset;
 395      }
 396  
 397  } // Date_TimeZone
 398  
 399  
 400  //
 401  // Time Zone Data
 402  //  offset is in miliseconds
 403  //
 404  $GLOBALS['_DATE_TIMEZONE_DATA'] = array(
 405          'UTC' => array(
 406          'offset' => 0,
 407          'longname' => "Coordinated Universal Time",
 408          'shortname' => 'UTC',
 409          'hasdst' => false )
 410  );
 411  
 412  
 413  Date_TimeZone::setDefault('UTC');
 414  //
 415  // END
 416  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics