Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  /**
   3   * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @category  Horde
   9   * @copyright 2012-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * A wrapper around PHP's native DateTime class that handles improperly
  16   * formatted dates and adds a few features missing from the base object
  17   * (string representation; doesn't fail on bad date input).
  18   *
  19   * @author    Michael Slusarz <slusarz@horde.org>
  20   * @category  Horde
  21   * @copyright 2012-2017 Horde LLC
  22   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  23   * @package   Imap_Client
  24   */
  25  class Horde_Imap_Client_DateTime extends DateTime
  26  {
  27      /**
  28       */
  29      public function __construct($time = null, $tz = null)
  30      {
  31          /* See https://bugs.php.net/bug.php?id=67118 */
  32          $bug_67118 = (version_compare(PHP_VERSION, '5.6', '>=')) ||
  33                       in_array(PHP_VERSION, array('5.4.29', '5.5.13'));
  34          $tz = new DateTimeZone('UTC');
  35  
  36          /* Bug #14381 Catch malformed offset - which doesn't cause
  37             DateTime to throw exception. */
  38          if (substr(rtrim($time), -5) === ' 0000') {
  39              $time = substr(trim($time), 0, strlen(trim($time)) - 5) . ' +0000';
  40              try {
  41                  if ($bug_67118) {
  42                      new DateTime($time, $tz);
  43                  }
  44                  parent::__construct($time, $tz);
  45                  return;
  46              } catch (Exception $e) {}
  47          }
  48  
  49          try {
  50              if ($bug_67118) {
  51                  new DateTime($time, $tz);
  52              }
  53              parent::__construct($time, $tz);
  54              return;
  55          } catch (Exception $e) {}
  56  
  57          /* Check for malformed day-of-week parts, usually incorrectly
  58           *  localized. E.g. Fr, 15 Apr 2016 15:15:09 +0000 */
  59          if (!preg_match("/^(Mon|Tue|Wed|Thu|Fri|Sat|Sun),/", $time)) {
  60              $time = preg_replace("/^(\S*,)/", '', $time, 1, $i);
  61              if ($i) {
  62                  try {
  63                      if ($bug_67118) {
  64                          new DateTime($time, $tz);
  65                      }
  66                      parent::__construct($time, $tz);
  67                      return;
  68                  } catch (Exception $e) {}
  69              }
  70          }
  71  
  72          /* Bug #5717 - Check for UT vs. UTC. */
  73          if (substr(rtrim($time), -3) === ' UT') {
  74              try {
  75                  if ($bug_67118) {
  76                      new DateTime($time . 'C', $tz);
  77                  }
  78                  parent::__construct($time . 'C', $tz);
  79                  return;
  80              } catch (Exception $e) {}
  81          }
  82  
  83          /* Bug #9847 - Catch paranthesized timezone information at end of date
  84           * string. */
  85          $date = preg_replace("/\s*\([^\)]+\)\s*$/", '', $time, -1, $i);
  86          if ($i) {
  87              try {
  88                  if ($bug_67118) {
  89                      new DateTime($date, $tz);
  90                  }
  91                  parent::__construct($date, $tz);
  92                  return;
  93              } catch (Exception $e) {}
  94          }
  95  
  96          parent::__construct('@-1', $tz);
  97      }
  98  
  99      /**
 100       * String representation: UNIX timestamp.
 101       */
 102      public function __toString()
 103      {
 104          return $this->error()
 105              ? '0'
 106              : strval($this->getTimestamp());
 107      }
 108  
 109      /**
 110       * Was this an unparseable date?
 111       *
 112       * @return boolean  True if unparseable.
 113       */
 114      public function error()
 115      {
 116          return ($this->getTimestamp() === -1);
 117      }
 118  
 119  }