1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class IMP_Message_Date
24: {
25: const DATE_FORCE = 1;
26: const DATE_FULL = 2;
27: const DATE_LOCAL = 3;
28:
29: 30: 31: 32: 33:
34: private static $_cache = array();
35:
36: 37: 38: 39: 40:
41: private $_date;
42:
43: 44: 45: 46: 47:
48: public function __construct($date = null)
49: {
50: if (!is_object($date) && !is_null($date)) {
51: $date = new Horde_Imap_Client_DateTime($date);
52: }
53:
54: $this->_date = $date;
55: }
56:
57: 58:
59: public function __toString()
60: {
61: return $this->format();
62: }
63:
64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76:
77: public function format($format = 0)
78: {
79: global $registry;
80:
81: $udate = null;
82: if ($this->_date && !$this->_date->error()) {
83: try {
84: $udate = $this->_date->format('U');
85:
86: if (empty(self::$_cache['tz'])) {
87: $registry->setTimeZone();
88: self::$_cache['tz'] = true;
89: }
90: } catch (Exception $e) {}
91: }
92:
93: switch ($format) {
94: case self::DATE_LOCAL:
95: if (is_null($udate)) {
96: return '';
97: }
98:
99: $this->_buildCache();
100: $tz = strftime('%Z');
101:
102: if (($udate < self::$_cache['today_start']) ||
103: ($udate > self::$_cache['today_end'])) {
104: if ($udate > self::$_cache['yesterday_start']) {
105:
106: return sprintf(
107: _("Yesterday, %s %s"),
108: $this->_format('time_format', $udate),
109: $tz
110: );
111: }
112:
113:
114: return sprintf(
115: '%s (%s %s)',
116: $this->_format('date_format', $udate),
117: $this->_format('time_format', $udate),
118: $tz
119: );
120: }
121:
122:
123: return sprintf(
124: _("Today, %s %s"),
125: $this->_format('time_format', $udate),
126: $tz
127: );
128: }
129:
130: if (is_null($udate)) {
131: return _("Unknown Date");
132: }
133:
134: if ($format === self::DATE_FORCE) {
135: return $this->_format('date_format', $udate) . ' [' .
136: $this->_format('time_format', $udate) . ' ' . strftime('%Z') .
137: ']';
138: }
139:
140: $this->_buildCache();
141:
142: if (($udate < self::$_cache['today_start']) ||
143: ($udate > self::$_cache['today_end'])) {
144: if ($udate > self::$_cache['yesterday_start']) {
145:
146: return sprintf(
147: _("Yesterday, %s"),
148: $this->_format('time_format_mini', $udate)
149: );
150: }
151:
152:
153: return ($format === self::DATE_FULL)
154: ? $this->_format('date_format', $udate) . ' [' . $this->_format('time_format', $udate) . ']'
155: : $this->_format('date_format_mini', $udate);
156: }
157:
158:
159: return $this->_format('time_format_mini', $udate);
160: }
161:
162: 163: 164:
165: private function _buildCache()
166: {
167: if (!isset(self::$_cache['today_start'])) {
168: $date = new DateTime('today');
169: self::$_cache['today_start'] = $date->format('U');
170:
171: $date = new DateTime('today + 1 day');
172: self::$_cache['today_end'] = $date->format('U');
173:
174: $date = new DateTime('today - 1 day');
175: self::$_cache['yesterday_start'] = $date->format('U');
176: }
177: }
178:
179: 180: 181: 182: 183: 184: 185: 186:
187: private function _format($type, $udate)
188: {
189: return ltrim(strftime($GLOBALS['prefs']->getValue($type), $udate));
190: }
191:
192: }
193: