Correct handling of default user themes.
[squirrelmail.git] / functions / date.php
1 <?php
2
3 /**
4 * date.php
5 *
6 * Takes a date and parses it into a usable format. The form that a
7 * date SHOULD arrive in is:
8 * <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT)
9 * (as specified in RFC 822) -- 'Tue' is optional
10 *
11 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @version $Id$
14 * @package squirrelmail
15 * @subpackage date
16 */
17
18 /**
19 * dependency information
20 * - none
21 */
22
23 /**
24 * Corrects a time stamp to be the local time.
25 *
26 * @param int stamp the timestamp to adjust
27 * @param string tzc the timezone correction
28 * @return int the corrected timestamp
29 */
30 function getGMTSeconds($stamp, $tzc) {
31 /* date couldn't be parsed */
32 if ($stamp == -1) {
33 return -1;
34 }
35 /* timezone correction, expressed as `shhmm' */
36 switch($tzc)
37 {
38 case 'Pacific':
39 case 'PST':
40 $tzc = '-0800';
41 break;
42 case 'Mountain':
43 case 'MST':
44 case 'PDT':
45 $tzc = '-0700';
46 break;
47 case 'Central':
48 case 'CST':
49 case 'MDT':
50 $tzc = '-0600';
51 break;
52 case 'Eastern':
53 case 'EST':
54 case 'CDT':
55 $tzc = '-0500';
56 break;
57 case 'EDT':
58 $tzc = '-0400';
59 break;
60 case 'GMT':
61 $tzc = '+0000';
62 break;
63 case 'BST':
64 case 'MET':
65 case 'CET':
66 $tzc = '+0100';
67 break;
68 case 'EET':
69 case 'IST':
70 case 'MET DST':
71 case 'METDST':
72 $tzc = '+0200';
73 break;
74 case 'HKT':
75 $tzc = '+0800';
76 break;
77 case 'JST':
78 case 'KST':
79 $tzc = '+0900';
80 break;
81 }
82 $neg = false;
83 if (substr($tzc, 0, 1) == '-') {
84 $neg = true;
85 } else if (substr($tzc, 0, 1) != '+') {
86 $tzc = '+'.$tzc;
87 }
88 $hh = substr($tzc,1,2);
89 $mm = substr($tzc,3,2);
90 $iTzc = ($hh * 60 + $mm) * 60;
91 if ($neg) $iTzc = -1 * (int) $iTzc;
92 /* stamp in gmt */
93 $stamp -= $iTzc;
94 /* now find what the server is at */
95 $current = date('Z', time());
96 /* stamp in local timezone */
97 $stamp += $current;
98
99 return $stamp;
100 }
101
102 /**
103 * Returns the (localized) string for a given day number.
104 * Switch system has been intentionaly chosen for the
105 * internationalization of month and day names. The reason
106 * is to make sure that _("") strings will go into the
107 * main po.
108 *
109 * @param int day_number the day number
110 * @return string the day in human readable form
111 */
112 function getDayName( $day_number ) {
113
114 switch( $day_number ) {
115 case 0:
116 $ret = _("Sunday");
117 break;
118 case 1:
119 $ret = _("Monday");
120 break;
121 case 2:
122 $ret = _("Tuesday");
123 break;
124 case 3:
125 $ret = _("Wednesday");
126 break;
127 case 4:
128 $ret = _("Thursday");
129 break;
130 case 5:
131 $ret = _("Friday");
132 break;
133 case 6:
134 $ret = _("Saturday");
135 break;
136 default:
137 $ret = '';
138 }
139 return( $ret );
140 }
141
142 /**
143 * Like getDayName, but returns the short form
144 * @param int day_number the day number
145 * @return string the day in short human readable form
146 */
147 function getDayAbrv( $day_number ) {
148
149 switch( $day_number ) {
150 case 0:
151 $ret = _("Sun");
152 break;
153 case 1:
154 $ret = _("Mon");
155 break;
156 case 2:
157 $ret = _("Tue");
158 break;
159 case 3:
160 $ret = _("Wed");
161 break;
162 case 4:
163 $ret = _("Thu");
164 break;
165 case 5:
166 $ret = _("Fri");
167 break;
168 case 6:
169 $ret = _("Sat");
170 break;
171 default:
172 $ret = '';
173 }
174 return( $ret );
175 }
176
177
178 /**
179 * Returns the (localized) string for a given month number.
180 *
181 * @param string month_number the month number (01..12)
182 * @return string the month name in human readable form
183 */
184 function getMonthName( $month_number ) {
185 switch( $month_number ) {
186 case '01':
187 $ret = _("January");
188 break;
189 case '02':
190 $ret = _("February");
191 break;
192 case '03':
193 $ret = _("March");
194 break;
195 case '04':
196 $ret = _("April");
197 break;
198 case '05':
199 $ret = _("May");
200 break;
201 case '06':
202 $ret = _("June");
203 break;
204 case '07':
205 $ret = _("July");
206 break;
207 case '08':
208 $ret = _("August");
209 break;
210 case '09':
211 $ret = _("September");
212 break;
213 case '10':
214 $ret = _("October");
215 break;
216 case '11':
217 $ret = _("November");
218 break;
219 case '12':
220 $ret = _("December");
221 break;
222 default:
223 $ret = '';
224 }
225 return( $ret );
226 }
227
228 /**
229 * Returns the (localized) string for a given month number,
230 * short representation.
231 *
232 * @param string month_number the month number (01..12)
233 * @return string the shortened month in human readable form
234 */
235 function getMonthAbrv( $month_number ) {
236 switch( $month_number ) {
237 case '01':
238 $ret = _("Jan");
239 break;
240 case '02':
241 $ret = _("Feb");
242 break;
243 case '03':
244 $ret = _("Mar");
245 break;
246 case '04':
247 $ret = _("Apr");
248 break;
249 case '05':
250 $ret = _("Ma&#121;");
251 break;
252 case '06':
253 $ret = _("Jun");
254 break;
255 case '07':
256 $ret = _("Jul");
257 break;
258 case '08':
259 $ret = _("Aug");
260 break;
261 case '09':
262 $ret = _("Sep");
263 break;
264 case '10':
265 $ret = _("Oct");
266 break;
267 case '11':
268 $ret = _("Nov");
269 break;
270 case '12':
271 $ret = _("Dec");
272 break;
273 default:
274 $ret = '';
275 }
276 return( $ret );
277 }
278
279 /**
280 * Returns the localized representation of the date/time.
281 *
282 * @param string date_format The format for the date, like the input for the PHP date() function.
283 * @param int stamp the timestamp to convert
284 * @return string a full date representation
285 */
286 function date_intl( $date_format, $stamp ) {
287 $ret = str_replace( array('D','F','l','M'), array('$1','$2','$3','$4'), $date_format );
288 // to reduce the date calls we retrieve m and w in the same call
289 $ret = date('w#m#'. $ret, $stamp );
290 // extract day and month in order to replace later by intl day and month
291 $aParts = explode('#',$ret);
292 $ret = str_replace(array('$1','$4','$2','$3',), array(getDayAbrv($aParts[0]),
293 getMonthAbrv($aParts[1]),
294 getMonthName($aParts[1]),
295 getDayName($aParts[0])),
296 $aParts[2]);
297 return( $ret );
298 }
299
300 /**
301 * This returns a date of the format "Wed, Oct 29, 2003 9:52 am",
302 * or the same in 24H format (depending on the user's settings),
303 * and taking localization into accout.
304 *
305 * @param int stamp the timestamp
306 * @return string the long date string
307 */
308 function getLongDateString( $stamp ) {
309
310 global $hour_format;
311
312 if ($stamp == -1) {
313 return '';
314 }
315
316 if ( $hour_format == SMPREF_TIME_12HR ) {
317 $date_format = _("D, F j, Y g:i a");
318 } else {
319 $date_format = _("D, F j, Y H:i");
320 }
321
322 return( date_intl( $date_format, $stamp ) );
323
324 }
325
326 /**
327 * Returns a short representation of the date,
328 * taking timezones and localization into account.
329 * Depending on user's settings, this string can be
330 * of the form: "14:23" or "Jun 14, 2003" depending
331 * on whether the stamp is "today" or not.
332 *
333 * @param int stamp the timestamp
334 * @return string the date string
335 */
336 function getDateString( $stamp ) {
337
338 global $invert_time, $hour_format, $show_full_date;
339
340 if ( $stamp == -1 ) {
341 return '';
342 }
343
344 $now = time();
345
346 $dateZ = date('Z', $now );
347
348 // FIXME: isn't this obsolete and introduced as a terrible workaround
349 // for bugs at other places which are fixed a long time ago?
350 if ($invert_time) {
351 $dateZ = - $dateZ;
352 }
353
354 // calculate when it was midnight and when it will be,
355 // in order to display dates differently if they're 'today'
356 $midnight = $now - ($now % 86400) - $dateZ;
357 // this is to correct if after calculations midnight is more than
358 // one whole day away.
359 if ($now - $midnight > 86400) {
360 $midnight += 86400;
361 }
362 $nextmid = $midnight + 86400;
363
364 if (($show_full_date == 1) || ($nextmid < $stamp)) {
365 $date_format = _("M j, Y");
366 } else if ($midnight < $stamp) {
367 /* Today */
368 if ( $hour_format == SMPREF_TIME_12HR ) {
369 $date_format = _("g:i a");
370 } else {
371 $date_format = _("H:i");
372 }
373 } else if ($midnight - 518400 < $stamp) {
374 /* This week */
375 if ( $hour_format == SMPREF_TIME_12HR ) {
376 $date_format = _("D, g:i a");
377 } else {
378 $date_format = _("D, H:i");
379 }
380 } else {
381 /* before this week */
382 $date_format = _("M j, Y");
383 }
384
385 return( date_intl( $date_format, $stamp ) );
386 }
387
388 /**
389 * Decodes a RFC 822 Date-header into a timestamp
390 *
391 * @param array dateParts the Date-header split by whitespace
392 * @return int the timestamp calculated from the header
393 */
394 function getTimeStamp($dateParts) {
395 /* $dateParts[0] == <day of week> Mon, Tue, Wed
396 * $dateParts[1] == <day of month> 23
397 * $dateParts[2] == <month> Jan, Feb, Mar
398 * $dateParts[3] == <year> 1999
399 * $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
400 * $dateParts[5] == <from GMT> +0100
401 * $dateParts[6] == <zone> (EDT)
402 *
403 * NOTE: In RFC 822, it states that <day of week> is optional.
404 * In that case, dateParts[0] would be the <day of month>
405 * and everything would be bumped up one.
406 */
407
408 /*
409 * Simply check to see if the first element in the dateParts
410 * array is an integer or not.
411 * Since the day of week is optional, this check is needed.
412 */
413 if (count($dateParts) <2) {
414 return -1;
415 } else if (count($dateParts) ==3) {
416 if (substr_count($dateParts[0],'-') == 2 &&
417 substr_count($dateParts[1],':') == 2) {
418 // dd-Month-yyyy 23:19:05 +0200
419 // redefine the date
420 $aDate = explode('-',$dateParts[0]);
421 $newDate = array($aDate[0],$aDate[1],$aDate[2],$dateParts[1],$dateParts[2]);
422 $dateParts = $newDate;
423 }
424 }
425
426 /* remove day of week */
427 if (!is_numeric(trim($dateParts[0]))) {
428 $dataParts = array_shift($dateParts);
429 }
430 /* calculate timestamp separated from the zone and obs-zone */
431 $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4)));
432 if (!isset($dateParts[0])) {
433 $dateParts[0] = '+0000';
434 }
435
436 if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
437 /* zone in obs-zone format */
438 if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) {
439 $obs_zone = $regs[1];
440 } else {
441 $obs_zone = $dateParts[0];
442 }
443 return getGMTSeconds($stamp, $obs_zone);
444 } else {
445 return getGMTSeconds($stamp, $dateParts[0]);
446 }
447 }