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