[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.date.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
   3  // +----------------------------------------------------------------------+
   4  // | Eventum - Issue Tracking System                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB                  |
   7  // |                                                                      |
   8  // | This program is free software; you can redistribute it and/or modify |
   9  // | it under the terms of the GNU General Public License as published by |
  10  // | the Free Software Foundation; either version 2 of the License, or    |
  11  // | (at your option) any later version.                                  |
  12  // |                                                                      |
  13  // | This program 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        |
  16  // | GNU General Public License for more details.                         |
  17  // |                                                                      |
  18  // | You should have received a copy of the GNU General Public License    |
  19  // | along with this program; if not, write to:                           |
  20  // |                                                                      |
  21  // | Free Software Foundation, Inc.                                       |
  22  // | 59 Temple Place - Suite 330                                          |
  23  // | Boston, MA 02111-1307, USA.                                          |
  24  // +----------------------------------------------------------------------+
  25  // | Authors: João Prado Maia <jpm@mysql.com>                             |
  26  // +----------------------------------------------------------------------+
  27  //
  28  // @(#) $Id: class.date.php 3246 2007-02-09 09:10:12Z glen $
  29  //
  30  
  31  // this line needed to make sure PEAR knows all eventum dates are stored as UTC (GMT).
  32  $_DATE_TIMEZONE_DEFAULT = 'UTC';
  33  
  34  require_once (APP_INC_PATH . "class.auth.php");
  35  require_once (APP_INC_PATH . "class.prefs.php");
  36  require_once(APP_PEAR_PATH . "Date.php");
  37  
  38  if (!defined('APP_DEFAULT_TIMEZONE')) {
  39      define('APP_DEFAULT_TIMEZONE', 'UTC');
  40  }
  41  define("SECOND", 1);
  42  define("MINUTE", SECOND * 60);
  43  define("HOUR", MINUTE * 60);
  44  define("DAY", HOUR * 24);
  45  define("WEEK", DAY * 7);
  46  define("MONTH", WEEK * 4);
  47  define("YEAR", MONTH * 12);
  48  
  49  /**
  50   * Class to handle date convertion issues, which enable the
  51   * application of storing all dates in GMT dates and allowing each
  52   * user to specify a timezone that is supposed to be used across the
  53   * pages.
  54   *
  55   * @version 1.0
  56   * @author João Prado Maia <jpm@mysql.com>
  57   */
  58  
  59  class Date_API
  60  {
  61      /**
  62       * Returns whether the given hour is AM or not.
  63       *
  64       * @access  public
  65       * @param   integer $hour The hour number
  66       * @return  boolean
  67       */
  68      function isAM($hour)
  69      {
  70          if (($hour >= 0) && ($hour <= 11)) {
  71              return true;
  72          } else {
  73              return false;
  74          }
  75      }
  76  
  77  
  78      /**
  79       * Returns whether the given hour is PM or not.
  80       *
  81       * @access  public
  82       * @param   integer $hour The hour number
  83       * @return  boolean
  84       */
  85      function isPM($hour)
  86      {
  87          if (($hour >= 12) && ($hour <= 23)) {
  88              return true;
  89          } else {
  90              return false;
  91          }
  92      }
  93  
  94  
  95      /**
  96       * Returns the current UNIX timestamp in the GMT timezone.
  97       *
  98       * @access  public
  99       * @return  integer The current UNIX timestamp in GMT
 100       */
 101      function getCurrentUnixTimestampGMT()
 102      {
 103          return gmmktime();
 104      }
 105  
 106  
 107      /**
 108       * Method used to get a pretty-like formatted time output for the
 109       * difference in time between two unix timestamps.
 110       *
 111       * @access  public
 112       * @param   integer $now_ts The current UNIX timestamp
 113       * @param   integer $old_ts The old UNIX timestamp
 114       * @return  string The formatted difference in time
 115       */
 116      function getFormattedDateDiff($now_ts, $old_ts)
 117      {
 118          $value = (integer) (($now_ts - $old_ts) / DAY);
 119          $ret = sprintf("%d", round($value, 1)) . "d";
 120          $mod = (integer) (($now_ts - $old_ts) % DAY);
 121          $mod = (integer) ($mod / HOUR);
 122          return $ret . " " . $mod . "h";
 123      }
 124  
 125  
 126      /**
 127       * Method used to get the user's current time (timezone included) as
 128       * a UNIX timestamp.
 129       *
 130       * @access  public
 131       * @param   integer $timestamp The current UNIX timestamp
 132       * @param   string $timezone The needed timezone
 133       * @return  integer The UNIX timestamp representing the user's current time
 134       */
 135      function getUnixTimestamp($timestamp, $timezone = FALSE)
 136      {
 137          if (!$timezone) {
 138              $timezone = Date_API::getPreferredTimezone();
 139          }
 140          $date = new Date($timestamp);
 141          // now convert to another timezone and return the timestamp
 142          $date->convertTZById($timezone);
 143          return $date->getDate(DATE_FORMAT_UNIXTIME);
 144      }
 145  
 146  
 147      /**
 148       * Method used to get the current date in the GMT timezone in an
 149       * RFC822 compliant format.
 150       *
 151       * @access  public
 152       * @return  string The current GMT date
 153       * @param   string $timezone The needed timezone
 154       */
 155      function getRFC822Date($timestamp, $timezone = FALSE)
 156      {
 157          if (!$timezone) {
 158              $timezone = Date_API::getPreferredTimezone();
 159          }
 160          $date = new Date($timestamp);
 161          // now convert to another timezone and return the date
 162          $date->convertTZById($timezone);
 163          return $date->format('%a, %d %b %Y %H:%M:%S') . " GMT";
 164      }
 165  
 166  
 167      /**
 168       * Method used to get the current date in the GMT timezone.
 169       *
 170       * @access  public
 171       * @return  string The current GMT date
 172       */
 173      function getCurrentDateGMT()
 174      {
 175          return gmdate('Y-m-d H:i:s');
 176      }
 177  
 178  
 179      /**
 180       * Method used to get the full list of available timezones to be
 181       * presented to the user.
 182       *
 183       * @access  public
 184       * @return  array The list of timezones
 185       */
 186      function getTimezoneList()
 187      {
 188          return Date_TimeZone::getAvailableIDs();
 189      }
 190  
 191  
 192      /**
 193       * Method used to get the proper short name for a given date.
 194       *
 195       * @access  public
 196       * @param   object $date The Date object
 197       * @return  string The timezone short name
 198       */
 199      function getTimezoneShortName($date)
 200      {
 201          if ($date->inDaylightTime()) {
 202              return $date->tz->getDSTShortName();
 203          } else {
 204              return $date->tz->getShortName();
 205          }
 206      }
 207  
 208  
 209      /**
 210       * Method used to get the proper timezone short name for the current date
 211       * and time on the given user's timezone.
 212       *
 213       * @access  public
 214       * @param   object $date The Date object
 215       * @return  string The timezone short name
 216       */
 217      function getTimezoneShortNameByUser($usr_id)
 218      {
 219          $date = new Date();
 220          $date->convertTZById(Date_API::getPreferredTimezone($usr_id));
 221          return Date_API::getTimezoneShortName($date);
 222      }
 223  
 224  
 225      /**
 226       * Method used to get the formatted date for a specific timestamp
 227       * and a specific timezone, provided by the user' preference.
 228       *
 229       * @access  public
 230       * @param   string $timestamp The date timestamp to be formatted
 231       * @param   string $timezone The timezone name
 232       * @return  string
 233       */
 234      function getFormattedDate($timestamp, $timezone = FALSE)
 235      {
 236          if ($timezone === FALSE) {
 237              $timezone = Date_API::getPreferredTimezone();
 238          }
 239          $date = new Date($timestamp);
 240          // now convert to another timezone and return the date
 241          $date->convertTZById($timezone);
 242          return $date->format('%a, %d %b %Y, %H:%M:%S ') . Date_API::getTimezoneShortName($date);
 243      }
 244  
 245  
 246      /**
 247       * Method used to get the formatted date for a specific timestamp
 248       * and a specific timezone, provided by the user' preference.
 249       *
 250       * @access  public
 251       * @param   string $timestamp The date timestamp to be formatted
 252       * @param   boolean $convert If the timestamp should be converted to the preferred timezone
 253       * @return  string
 254       */
 255      function getSimpleDate($timestamp, $convert = true)
 256      {
 257          if (empty($timestamp)) {
 258              return '';
 259          }
 260          $date = new Date($timestamp);
 261          // now convert to another timezone and return the date
 262          if ($convert) {
 263              $timezone = Date_API::getPreferredTimezone();
 264              $date->convertTZById($timezone);
 265          }
 266          return $date->format('%d %b %Y');
 267      }
 268  
 269  
 270      /**
 271       * Method used to get the timezone preferred by the user.
 272       *
 273       * @access  public
 274       * @param   integer $usr_id The user ID
 275       * @return  string The timezone preferred by the user
 276       */
 277      function getPreferredTimezone($usr_id = FALSE)
 278      {
 279          if ($usr_id === FALSE) {
 280              $usr_id = Auth::getUserID();
 281          }
 282          if (empty($usr_id)) {
 283              return Date_API::getDefaultTimezone();
 284          }
 285          $prefs = Prefs::get($usr_id);
 286          if (empty($prefs["timezone"])) {
 287              return Date_API::getDefaultTimezone();
 288          } else {
 289              return $prefs["timezone"];
 290          }
 291      }
 292  
 293  
 294      /**
 295       * Method used to get the application default timezone.
 296       *
 297       * @access  public
 298       * @return  string The default timezone
 299       */
 300      function getDefaultTimezone()
 301      {
 302          return APP_DEFAULT_TIMEZONE;
 303      }
 304  
 305  
 306      /**
 307       * Method used to convert the user date (that might be in a
 308       * specific timezone) to a GMT date.
 309       *
 310       * @access  public
 311       * @param   string $date The user based date
 312       * @return  string The date in the GMT timezone
 313       */
 314      function getDateGMT($date)
 315      {
 316          $dt = new Date($date);
 317          $dt->setTZbyID(Date_API::getPreferredTimezone());
 318          $dt->toUTC();
 319          return $dt->format('%Y-%m-%d %H:%M:%S');
 320      }
 321  
 322  
 323      /**
 324       * Method used to convert a unix timestamp date to a GMT date.
 325       *
 326       * @access  public
 327       * @param   integer $timestamp The user based date
 328       * @return  string The date in the GMT timezone
 329       */
 330      function getDateGMTByTS($timestamp)
 331      {
 332          return gmdate('Y-m-d H:i:s', $timestamp);
 333      }
 334  
 335  
 336      /**
 337       * Returns a list of weeks (May 2 - May 8, May 9 - May 15).
 338       *
 339       * @access public
 340       * @param   integer $weeks_past The number of weeks in the past to include.
 341       * @param   integer $weeks_future The number of weeks in the future to include.
 342       * @return  array An array of weeks.
 343       */
 344      function getWeekOptions($weeks_past, $weeks_future)
 345      {
 346          $options = array();
 347  
 348          // get current week details
 349          $current_start = date("U") - (DAY * (date("w") - 1));
 350  
 351          // previous weeks
 352          for ($week = $weeks_past; $week > 0; $week--) {
 353              $option = Date_API::formatWeekOption($current_start - ($week * WEEK));
 354              $options[$option[0]] = $option[1];
 355          }
 356  
 357          $option = Date_API::formatWeekOption($current_start);
 358          $options[$option[0]] = $option[1];
 359  
 360          // future weeks
 361          for ($week = 1; $week <= $weeks_future; $week++) {
 362              $option = Date_API::formatWeekOption($current_start + ($week * WEEK));
 363              $options[$option[0]] = $option[1];
 364          }
 365  
 366          return $options;
 367      }
 368  
 369  
 370      /**
 371       * Returns the current week in the same format formatWeekOption users.
 372       *
 373       * @access  public
 374       * @return  string A string containg the current week.
 375       */
 376      function getCurrentWeek()
 377      {
 378          $value_format = "Y-m-d";
 379          $start = date("U") - (DAY * (date("w") - 1));
 380          return date($value_format, $start) . "_" . date($value_format, ($start + (DAY * 6)));
 381      }
 382  
 383  
 384      /**
 385       * Formats a given week start and week end to a format useable by getWeekOptions().
 386       *
 387       * @access  private
 388       * @param   integer $start The start date of the week.
 389       * @return  array An array usable as an option in getWeekOptions.
 390       */
 391      function formatWeekOption($start)
 392      {
 393          $value_format = "Y-m-d";
 394          $display_format = "M jS";
 395          $end = ($start + (DAY * 6));
 396          $value = date($value_format, $start) . "_" . date($value_format, $end);
 397          $display = date($display_format, $start) . " - " . date($display_format, $end);
 398          return array($value,$display);
 399      }
 400  }
 401  
 402  // benchmarking the included file (aka setup time)
 403  if (APP_BENCHMARK) {
 404      $GLOBALS['bench']->setMarker('Included Date_API Class');
 405  }


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7