1: <?php
2: /**
3: * A wrapper around PHP's native DateTime class that works around a PHP 5.2.x
4: * issue that does not allow DateTime objects to be serialized.
5: *
6: * See: http://bugs.php.net/bug.php?id=41334
7: *
8: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
9: *
10: * See the enclosed file COPYING for license information (LGPL). If you
11: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
12: *
13: * @author Michael Slusarz <slusarz@horde.org>
14: * @category Horde
15: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
16: * @package Imap_Client
17: */
18: class Horde_Imap_Client_DateTime implements Serializable
19: {
20: /**
21: * The DateTime object to use for function calls.
22: *
23: * @var DateTime
24: */
25: private $_datetime = null;
26:
27: /**
28: * Indicate an unparseable time.
29: *
30: * @var boolean
31: */
32: private $_error = false;
33:
34: /**
35: * The datetime string.
36: *
37: * @var string
38: */
39: private $_string;
40:
41: /**
42: * Constructor.
43: *
44: * @param string $time String in a format accepted by strtotime().
45: */
46: public function __construct($time = null)
47: {
48: $this->_string = $time;
49: }
50:
51: /**
52: * String representation: UNIX timestamp.
53: */
54: public function __toString()
55: {
56: return $this->format('U');
57: }
58:
59: /**
60: * Serialize.
61: *
62: * @return string Serialized representation of this object.
63: */
64: public function serialize()
65: {
66: return $this->_string;
67: }
68:
69: /**
70: * Unserialize.
71: *
72: * @param string $data Serialized data.
73: *
74: * @throws Exception
75: */
76: public function unserialize($data)
77: {
78: $this->_string = $data;
79: }
80:
81: /**
82: * Was this an unparseable date?
83: *
84: * @return boolean True if unparseable.
85: */
86: public function error()
87: {
88: $this->_init();
89:
90: return $this->_error;
91: }
92:
93: /**
94: * Called on a function call.
95: *
96: * @throws Exception
97: */
98: public function __call($name, $arguments)
99: {
100: $this->_init();
101:
102: return call_user_func_array(array($this->_datetime, $name), $arguments);
103: }
104:
105: /**
106: * Init the DateTime object.
107: */
108: private function _init()
109: {
110: if ($this->_datetime) {
111: return;
112: }
113:
114: $tz = new DateTimeZone('UTC');
115:
116: if (!is_null($this->_string)) {
117: /* DateTime in PHP 5.2 returns false, not a thrown Exception. */
118: try {
119: $this->_datetime = date_create($this->_string, $tz);
120: } catch (Exception $e) {}
121:
122: if (!$this->_datetime &&
123: substr(rtrim($this->_string), -3) == ' UT') {
124: /* Bug #5717 - Check for UT vs. UTC. */
125: try {
126: $this->_datetime = date_create($this->_string . 'C', $tz);
127: } catch (Exception $e) {}
128: }
129:
130: if (!$this->_datetime) {
131: /* Bug #9847 - Catch paranthesized timezone information
132: * at end of date string. */
133: $date = preg_replace("/\s*\([^\)]+\)\s*$/", '', $this->_string, -1, $i);
134: if ($i) {
135: try {
136: $this->_datetime = date_create($date, $tz);
137: } catch (Exception $e) {}
138: }
139: }
140: }
141:
142: if (!$this->_datetime) {
143: $this->_datetime = new DateTime('@0', $tz);
144: $this->_error = true;
145: }
146: }
147:
148: }
149: