6 * Copyright (c) 1999-2004 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 G: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?
352 $midnight = $now - ($now %
86400) - $dateZ;
353 $nextmid = $midnight +
86400 - $dateZ;
355 if (($show_full_date == 1) ||
($nextmid < $stamp)) {
356 $date_format = _("M j, Y");
357 } else if ($midnight < $stamp) {
359 if ( $hour_format == SMPREF_TIME_12HR
) {
360 $date_format = _("g:i a");
362 $date_format = _("G:i");
364 } else if ($midnight - 518400 < $stamp) {
366 if ( $hour_format == SMPREF_TIME_12HR
) {
367 $date_format = _("D, g:i a");
369 $date_format = _("D, G:i");
372 /* before this week */
373 $date_format = _("M j, Y");
376 return( date_intl( $date_format, $stamp ) );
380 * Decodes a RFC 822 Date-header into a timestamp
382 * @param array dateParts the Date-header split by whitespace
383 * @return int the timestamp calculated from the header
385 function getTimeStamp($dateParts) {
386 /** $dateParts[0] == <day of week> Mon, Tue, Wed
387 ** $dateParts[1] == <day of month> 23
388 ** $dateParts[2] == <month> Jan, Feb, Mar
389 ** $dateParts[3] == <year> 1999
390 ** $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
391 ** $dateParts[5] == <from GMT> +0100
392 ** $dateParts[6] == <zone> (EDT)
394 ** NOTE: In RFC 822, it states that <day of week> is optional.
395 ** In that case, dateParts[0] would be the <day of month>
396 ** and everything would be bumped up one.
400 * Simply check to see if the first element in the dateParts
401 * array is an integer or not.
402 * Since the day of week is optional, this check is needed.
404 if (count($dateParts) <2) {
406 } else if (count($dateParts) ==3) {
407 if (substr_count($dateParts[0],'-') == 2 &&
408 substr_count($dateParts[1],':') == 2) {
409 // dd-Month-yyyy 23:19:05 +0200
411 $aDate = explode('-',$dateParts[0]);
412 $newDate = array($aDate[0],$aDate[1],$aDate[2],$dateParts[1],$dateParts[2]);
413 $dateParts = $newDate;
417 /* remove day of week */
418 if (!is_numeric(trim($dateParts[0]))) {
419 $dataParts = array_shift($dateParts);
421 /* calculate timestamp separated from the zone and obs-zone */
422 $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4)));
423 if (!isset($dateParts[0])) {
424 $dateParts[0] = '+0000';
427 if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
428 /* zone in obs-zone format */
429 if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) {
430 $obs_zone = $regs[1];
432 $obs_zone = $dateParts[0];
434 return getGMTSeconds($stamp, $obs_zone);
436 return getGMTSeconds($stamp, $dateParts[0]);
440 /* I use this function for profiling. Should never be called in
441 actual versions of squirrelmail released to public. */
443 function getmicrotime() {
444 $mtime = microtime();
445 $mtime = explode(' ',$mtime);
446 $mtime = $mtime[1] + $mtime[0];