b362d15c3b9edfc1838bef9a1a617353fc2921b6
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
11 * @copyright © 1999-2006 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @package squirrelmail
18 /** Load up some useful constants */
19 require_once(SM_PATH
. 'functions/constants.php');
22 * Corrects a time stamp to be the local time.
24 * @param int stamp the timestamp to adjust
25 * @param string tzc the timezone correction
26 * @return int the corrected timestamp
28 function getGMTSeconds($stamp, $tzc) {
29 /* date couldn't be parsed */
33 /* timezone correction, expressed as `shhmm' */
81 if (substr($tzc, 0, 1) == '-') {
83 } else if (substr($tzc, 0, 1) != '+') {
86 $hh = substr($tzc,1,2);
87 $mm = substr($tzc,3,2);
88 $iTzc = ($hh * 60 +
$mm) * 60;
89 if ($neg) $iTzc = -1 * (int) $iTzc;
92 /* now find what the server is at */
93 $current = date('Z', time());
94 /* stamp in local timezone */
101 * Returns the (localized) string for a given day number.
102 * Switch system has been intentionaly chosen for the
103 * internationalization of month and day names. The reason
104 * is to make sure that _("") strings will go into the
107 * @param int day_number the day number
108 * @return string the day in human readable form
110 function getDayName( $day_number ) {
112 switch( $day_number ) {
123 $ret = _("Wednesday");
126 $ret = _("Thursday");
132 $ret = _("Saturday");
141 * Like getDayName, but returns the short form
142 * @param int day_number the day number
143 * @return string the day in short human readable form
145 function getDayAbrv( $day_number ) {
147 switch( $day_number ) {
177 * Returns the (localized) string for a given month number.
179 * @param string month_number the month number (01..12)
180 * @return string the month name in human readable form
182 function getMonthName( $month_number ) {
183 switch( $month_number ) {
188 $ret = _("February");
209 $ret = _("September");
215 $ret = _("November");
218 $ret = _("December");
227 * Returns the (localized) string for a given month number,
228 * short representation.
230 * @param string month_number the month number (01..12)
231 * @return string the shortened month in human readable form
233 function getMonthAbrv( $month_number ) {
234 switch( $month_number ) {
248 $ret = _("May");
278 * Returns the localized representation of the date/time.
280 * @param string date_format The format for the date, like the input for the PHP date() function.
281 * @param int stamp the timestamp to convert
282 * @return string a full date representation
284 function date_intl( $date_format, $stamp ) {
285 $ret = str_replace( array('D','F','l','M'), array('$1','$2','$3','$4'), $date_format );
286 // to reduce the date calls we retrieve m and w in the same call
287 $ret = date('w#m#'. $ret, $stamp );
288 // extract day and month in order to replace later by intl day and month
289 $aParts = explode('#',$ret);
290 $ret = str_replace(array('$1','$4','$2','$3',), array(getDayAbrv($aParts[0]),
291 getMonthAbrv($aParts[1]),
292 getMonthName($aParts[1]),
293 getDayName($aParts[0])),
299 * This returns a date of the format "Wed, Oct 29, 2003 9:52 am",
300 * or the same in 24H format (depending on the user's settings),
301 * and taking localization into accout.
303 * @param int stamp the timestamp
304 * @return string the long date string
306 function getLongDateString( $stamp ) {
314 if ( $hour_format == SMPREF_TIME_12HR
) {
315 $date_format = _("D, F j, Y g:i a");
317 $date_format = _("D, F j, Y H:i");
320 return( date_intl( $date_format, $stamp ) );
325 * Returns a short representation of the date,
326 * taking timezones and localization into account.
327 * Depending on user's settings, this string can be
328 * of the form: "14:23" or "Jun 14, 2003" depending
329 * on whether the stamp is "today" or not.
331 * @param int stamp the timestamp
332 * @return string the date string
334 function getDateString( $stamp ) {
336 global $invert_time, $hour_format, $show_full_date;
338 if ( $stamp == -1 ) {
344 $dateZ = date('Z', $now );
346 // FIXME: isn't this obsolete and introduced as a terrible workaround
347 // for bugs at other places which are fixed a long time ago?
352 // calculate when it was midnight and when it will be,
353 // in order to display dates differently if they're 'today'
354 $midnight = $now - ($now %
86400) - $dateZ;
355 // this is to correct if after calculations midnight is more than
356 // one whole day away.
357 if ($now - $midnight > 86400) {
360 $nextmid = $midnight +
86400;
362 if (($show_full_date == 1) ||
($nextmid < $stamp)) {
363 $date_format = _("M j, Y");
364 } else if ($midnight < $stamp) {
366 if ( $hour_format == SMPREF_TIME_12HR
) {
367 $date_format = _("g:i a");
369 $date_format = _("H:i");
371 } else if ($midnight - 518400 < $stamp) {
373 if ( $hour_format == SMPREF_TIME_12HR
) {
374 $date_format = _("D, g:i a");
376 $date_format = _("D, H:i");
379 /* before this week */
380 $date_format = _("M j, Y");
383 return( date_intl( $date_format, $stamp ) );
387 * Decodes a RFC 822 Date-header into a timestamp
389 * @param array dateParts the Date-header split by whitespace
390 * @return int the timestamp calculated from the header
392 function getTimeStamp($dateParts) {
393 /* $dateParts[0] == <day of week> Mon, Tue, Wed
394 * $dateParts[1] == <day of month> 23
395 * $dateParts[2] == <month> Jan, Feb, Mar
396 * $dateParts[3] == <year> 1999
397 * $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
398 * $dateParts[5] == <from GMT> +0100
399 * $dateParts[6] == <zone> (EDT)
401 * NOTE: In RFC 822, it states that <day of week> is optional.
402 * In that case, dateParts[0] would be the <day of month>
403 * and everything would be bumped up one.
407 * Simply check to see if the first element in the dateParts
408 * array is an integer or not.
409 * Since the day of week is optional, this check is needed.
411 if (count($dateParts) <2) {
413 } else if (count($dateParts) ==3) {
414 if (substr_count($dateParts[0],'-') == 2 &&
415 substr_count($dateParts[1],':') == 2) {
416 // dd-Month-yyyy 23:19:05 +0200
418 $aDate = explode('-',$dateParts[0]);
419 $newDate = array($aDate[0],$aDate[1],$aDate[2],$dateParts[1],$dateParts[2]);
420 $dateParts = $newDate;
424 /* remove day of week */
425 if (!is_numeric(trim($dateParts[0]))) {
426 $dataParts = array_shift($dateParts);
428 /* calculate timestamp separated from the zone and obs-zone */
429 $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4)));
430 if (!isset($dateParts[0])) {
431 $dateParts[0] = '+0000';
434 if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
435 /* zone in obs-zone format */
436 if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) {
437 $obs_zone = $regs[1];
439 $obs_zone = $dateParts[0];
441 return getGMTSeconds($stamp, $obs_zone);
443 return getGMTSeconds($stamp, $dateParts[0]);
447 /* I use this function for profiling. Should never be called in
448 actual versions of SquirrelMail released to public. */
450 function getmicrotime() {
451 $mtime = microtime();
452 $mtime = explode(' ',$mtime);
453 $mtime = $mtime[1] + $mtime[0];