6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Takes a date and parses it into a usable format. The form that a
10 * date SHOULD arrive in is:
11 * <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT)
12 * (as specified in RFC 822) -- 'Tue' is optional
15 * @package squirrelmail
19 /** Load up some useful constants */
20 require_once(SM_PATH
. 'functions/constants.php');
23 * Corrects a time stamp to be the local time.
25 * @param int stamp the timestamp to adjust
26 * @param string tzc the timezone correction
27 * @return int the corrected timestamp
29 function getGMTSeconds($stamp, $tzc) {
30 /* date couldn't be parsed */
34 /* timezone correction, expressed as `shhmm' */
82 if (substr($tzc, 0, 1) == '-') {
84 } else if (substr($tzc, 0, 1) != '+') {
87 $hh = substr($tzc,1,2);
88 $mm = substr($tzc,3,2);
89 $iTzc = ($hh * 60 +
$mm) * 60;
90 if ($neg) $iTzc = -1 * (int) $iTzc;
93 /* now find what the server is at */
94 $current = date('Z', time());
95 /* stamp in local timezone */
102 * Returns the (localized) string for a given day number.
103 * Switch system has been intentionaly chosen for the
104 * internationalization of month and day names. The reason
105 * is to make sure that _("") strings will go into the
108 * @param int day_number the day number
109 * @return string the day in human readable form
111 function getDayName( $day_number ) {
113 switch( $day_number ) {
124 $ret = _("Wednesday");
127 $ret = _("Thursday");
133 $ret = _("Saturday");
142 * Like getDayName, but returns the short form
143 * @param int day_number the day number
144 * @return string the day in short human readable form
146 function getDayAbrv( $day_number ) {
148 switch( $day_number ) {
178 * Returns the (localized) string for a given month number.
180 * @param string month_number the month number (01..12)
181 * @return string the month name in human readable form
183 function getMonthName( $month_number ) {
184 switch( $month_number ) {
189 $ret = _("February");
210 $ret = _("September");
216 $ret = _("November");
219 $ret = _("December");
228 * Returns the (localized) string for a given month number,
229 * short representation.
231 * @param string month_number the month number (01..12)
232 * @return string the shortened month in human readable form
234 function getMonthAbrv( $month_number ) {
235 switch( $month_number ) {
249 $ret = _("May");
279 * Returns the localized representation of the date/time.
281 * @param string date_format The format for the date, like the input for the PHP date() function.
282 * @param int stamp the timestamp to convert
283 * @return string a full date representation
285 function date_intl( $date_format, $stamp ) {
286 $ret = str_replace( array('D','F','l','M'), array('$1','$2','$3','$4'), $date_format );
287 // to reduce the date calls we retrieve m and w in the same call
288 $ret = date('w#m#'. $ret, $stamp );
289 // extract day and month in order to replace later by intl day and month
290 $aParts = explode('#',$ret);
291 $ret = str_replace(array('$1','$4','$2','$3',), array(getDayAbrv($aParts[0]),
292 getMonthAbrv($aParts[1]),
293 getMonthName($aParts[1]),
294 getDayName($aParts[0])),
300 * This returns a date of the format "Wed, Oct 29, 2003 9:52 am",
301 * or the same in 24H format (depending on the user's settings),
302 * and taking localization into accout.
304 * @param int stamp the timestamp
305 * @return string the long date string
307 function getLongDateString( $stamp ) {
315 if ( $hour_format == SMPREF_TIME_12HR
) {
316 $date_format = _("D, F j, Y g:i a");
318 $date_format = _("D, F j, Y H:i");
321 return( date_intl( $date_format, $stamp ) );
326 * Returns a short representation of the date,
327 * taking timezones and localization into account.
328 * Depending on user's settings, this string can be
329 * of the form: "14:23" or "Jun 14, 2003" depending
330 * on whether the stamp is "today" or not.
332 * @param int stamp the timestamp
333 * @return string the date string
335 function getDateString( $stamp ) {
337 global $invert_time, $hour_format, $show_full_date;
339 if ( $stamp == -1 ) {
345 $dateZ = date('Z', $now );
347 // FIXME: isn't this obsolete and introduced as a terrible workaround
348 // for bugs at other places which are fixed a long time ago?
353 // calculate when it was midnight and when it will be,
354 // in order to display dates differently if they're 'today'
355 $midnight = $now - ($now %
86400) - $dateZ;
356 // this is to correct if after calculations midnight is more than
357 // one whole day away.
358 if ($now - $midnight > 86400) {
361 $nextmid = $midnight +
86400;
363 if (($show_full_date == 1) ||
($nextmid < $stamp)) {
364 $date_format = _("M j, Y");
365 } else if ($midnight < $stamp) {
367 if ( $hour_format == SMPREF_TIME_12HR
) {
368 $date_format = _("g:i a");
370 $date_format = _("H:i");
372 } else if ($midnight - 518400 < $stamp) {
374 if ( $hour_format == SMPREF_TIME_12HR
) {
375 $date_format = _("D, g:i a");
377 $date_format = _("D, H:i");
380 /* before this week */
381 $date_format = _("M j, Y");
384 return( date_intl( $date_format, $stamp ) );
388 * Decodes a RFC 822 Date-header into a timestamp
390 * @param array dateParts the Date-header split by whitespace
391 * @return int the timestamp calculated from the header
393 function getTimeStamp($dateParts) {
394 /* $dateParts[0] == <day of week> Mon, Tue, Wed
395 * $dateParts[1] == <day of month> 23
396 * $dateParts[2] == <month> Jan, Feb, Mar
397 * $dateParts[3] == <year> 1999
398 * $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
399 * $dateParts[5] == <from GMT> +0100
400 * $dateParts[6] == <zone> (EDT)
402 * NOTE: In RFC 822, it states that <day of week> is optional.
403 * In that case, dateParts[0] would be the <day of month>
404 * and everything would be bumped up one.
408 * Simply check to see if the first element in the dateParts
409 * array is an integer or not.
410 * Since the day of week is optional, this check is needed.
412 if (count($dateParts) <2) {
414 } else if (count($dateParts) ==3) {
415 if (substr_count($dateParts[0],'-') == 2 &&
416 substr_count($dateParts[1],':') == 2) {
417 // dd-Month-yyyy 23:19:05 +0200
419 $aDate = explode('-',$dateParts[0]);
420 $newDate = array($aDate[0],$aDate[1],$aDate[2],$dateParts[1],$dateParts[2]);
421 $dateParts = $newDate;
425 /* remove day of week */
426 if (!is_numeric(trim($dateParts[0]))) {
427 $dataParts = array_shift($dateParts);
429 /* calculate timestamp separated from the zone and obs-zone */
430 $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4)));
431 if (!isset($dateParts[0])) {
432 $dateParts[0] = '+0000';
435 if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
436 /* zone in obs-zone format */
437 if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) {
438 $obs_zone = $regs[1];
440 $obs_zone = $dateParts[0];
442 return getGMTSeconds($stamp, $obs_zone);
444 return getGMTSeconds($stamp, $dateParts[0]);
448 /* I use this function for profiling. Should never be called in
449 actual versions of SquirrelMail released to public. */
451 function getmicrotime() {
452 $mtime = microtime();
453 $mtime = explode(' ',$mtime);
454 $mtime = $mtime[1] + $mtime[0];