|
iTx Technologies offre gratuitement
|
||
[Vue sommaire] [Imprimer] [Vue textuelle]
1 <?php 2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); 3 /********************************************************************************* 4 * SugarCRM is a customer relationship management program developed by 5 * SugarCRM, Inc. Copyright (C) 2004 - 2009 SugarCRM Inc. 6 * 7 * This program is free software; you can redistribute it and/or modify it under 8 * the terms of the GNU General Public License version 3 as published by the 9 * Free Software Foundation with the addition of the following permission added 10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK 11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY 12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 13 * 14 * This program is distributed in the hope that it will be useful, but WITHOUT 15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 * details. 18 * 19 * You should have received a copy of the GNU General Public License along with 20 * this program; if not, see http://www.gnu.org/licenses or write to the Free 21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 22 * 02110-1301 USA. 23 * 24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road, 25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com. 26 * 27 * The interactive user interfaces in modified source and object code versions 28 * of this program must display Appropriate Legal Notices, as required under 29 * Section 5 of the GNU General Public License version 3. 30 * 31 * In accordance with Section 7(b) of the GNU General Public License version 3, 32 * these Appropriate Legal Notices must retain the display of the "Powered by 33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for 34 * technical reasons, the Appropriate Legal Notices must display the words 35 * "Powered by SugarCRM". 36 ********************************************************************************/ 37 require_once ('include/timezone/timezones.php'); 38 39 class TimeDate { 40 var $dbDayFormat = 'Y-m-d'; 41 var $dbTimeFormat = 'H:i:s'; 42 var $supported_strings = array( 43 'a' => '[ap]m', 44 'A' => '[AP]M', 45 'd' => '[0-9]{1,2}', 46 'h' => '[0-9]{1,2}', 47 'H' => '[0-9]{1,2}', 48 'i' => '[0-9]{1,2}', 49 'm' => '[0-9]{1,2}', 50 'Y' => '[0-9]{4}', 51 's' => '[0-9]{1,2}' 52 ); 53 54 /** 55 * Map the tokens passed into this as a "format" string to 56 * PHP's internal date() format string values. 57 * 58 * @var array 59 * @access private 60 * @see build_format() 61 */ 62 var $time_token_map = array( 63 'a' => 'a', // meridiem: am or pm 64 'A' => 'A', // meridiem: AM or PM 65 'd' => 'd', // days: 1 through 31 66 'h' => 'h', // hours: 01 through 12 67 'H' => 'H', // hours: 00 through 23 68 'i' => 'i', // minutes: 00 through 59 69 'm' => 'm', // month: 1 - 12 70 'Y' => 'Y', // year: four digits 71 's' => 's', // seconds 72 ); 73 74 /** 75 * The current timezone information for the current user 76 */ 77 private $current_user_timezone = null; 78 79 /** 80 * The current user timezone adjustment 81 */ 82 private $current_user_adjustment = null; 83 84 /** 85 * Constructor 86 */ 87 public function __construct() 88 { 89 // avoids a potential E_STRICT warning when using any date function 90 if ( function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) 91 date_default_timezone_set(@date_default_timezone_get()); 92 } 93 94 /** 95 * function getUserTimeZone() 96 * Returns the current users timezone info or another user; 97 * 98 * $user user object for which you want to display, null for current user 99 * @return Array of timezone info 100 */ 101 102 function getUserTimeZone($user = null){ 103 global $timezones, $current_user; 104 $usertimezone = array(); 105 if(empty($user) || (!empty($user->id) && $user->id == $current_user->id)) { 106 if(isset($this->current_user_timezone)) return $this->current_user_timezone; // current user, return saved timezone info 107 $user = $current_user; 108 } 109 110 if(isset($user)) 111 { 112 if($usertimezone = $user->getPreference('timezone')) { 113 if(empty($timezones[$usertimezone])) { 114 $GLOBALS['log']->fatal('TIMEZONE:NOT DEFINED-'. $usertimezone); 115 $usertimezone = array(); 116 } else { 117 $usertimezone = $timezones[$usertimezone]; 118 } 119 } 120 } 121 122 if(!empty($user->id) && $user->id == $current_user->id) $this->current_user_timezone = $usertimezone; // save current_user 123 return $usertimezone; 124 } 125 126 /** 127 * function adjustmentForUserTimeZone() 128 * this returns the adjustment for a user against the server time 129 * 130 * $timezone pass in a timezone to adjust for 131 * @return INT number of minutes to adjust a time by to get the appropriate time for the user 132 */ 133 function adjustmentForUserTimeZone($timezone_to_adjust = null){ 134 static $tz_to_adjust; 135 if(isset($this->current_user_adjustment) && $tz_to_adjust == $timezone_to_adjust){ 136 return $this->current_user_adjustment; 137 } 138 139 $adjustment = 0; 140 $tz_to_adjust = $timezone_to_adjust; 141 142 if(empty($timezone_to_adjust)) { 143 $timezone = $this->getUserTimeZone(); 144 } 145 else { 146 $timezone = $timezone_to_adjust; 147 } 148 if(empty($timezone)) { 149 return $adjustment; 150 } 151 152 $server_offset = date('Z')/60; 153 $server_in_ds = date('I'); 154 $user_in_ds = $this->inDST(date('Y-m-d H:i:s'), $timezone); 155 $user_offset = $timezone['gmtOffset'] ; 156 157 //compensate for ds for user 158 if($user_in_ds) { 159 $user_offset += 60; 160 } 161 162 //either both + or - 163 $adjustment += $server_offset - $user_offset; 164 if(empty($timezone_to_adjust)) $this->current_user_adjustment = $adjustment; // save current_user adj 165 166 return $adjustment; 167 } 168 169 /** 170 * function getWeekDayName($indexOfDay) 171 * Returns a days name 172 * 173 * @param INT(WEEKDAY INDEX) $indexOfDay 174 * @return STRING representing the given weekday 175 */ 176 function getWeekDayName($indexOfDay){ 177 static $dow = array ( 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' ); 178 return $dow[$indexOfDay]; 179 } 180 /** 181 * function getMonthName($indexMonth) 182 * Returns a Months Name 183 * 184 * @param INT(MONTH INDEX) $indexMonth 185 * @return STRING representation of the month 186 */ 187 function getMonthName($indexMonth){ 188 static $months = array ( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ); 189 return $months[$indexMonth]; 190 } 191 192 /** 193 * function getDateFromRules($year, $startMonth, $startDate, $weekday, $startTime ) 194 * Converts the rules for a timezones dst into a string representation of a date for the given year 195 * 196 * @param STRING(YEAR) $year 197 * @param INT(MONTH INDEX) $startMonth 198 * @param INT(DATE INDEX) $startDate 199 * @param INT(WEEKDAY INDEX) $weekday 200 * @param INT(START TIME IN SECONDS) $startTime 201 * @return unknown 202 */ 203 function getDateFromRules($year, $startMonth, $startDate, $weekday, $startTime ){ 204 if($weekday < 0)return date( 'Y-m-d H:i:s', strtotime("$year-$startMonth-$startDate") + $startTime); 205 $dayname = TimeDate::getWeekDayName($weekday); 206 if($startDate > 0)$startMonth--; 207 $month = TimeDate::getMonthName($startMonth); 208 $startWeek = floor($startDate/7); 209 //echo "$startWeek week $dayname - $month 1, $year<br>"; 210 return date( 'Y-m-d H:i:s', strtotime( "$startWeek week $dayname", strtotime( "$month 1, $year" ) ) + $startTime ); 211 212 } 213 214 /** 215 * function getDSTRange($year, $zone) 216 * 217 * returns the start and end date for dst for a given timezone and year or false if that zone doesn't support dst 218 * 219 * @param STRING(Year e.g. 2005) $year 220 * @param ARRAY (TIME ZONE INFO) $zone 221 * @return ARRAY OF DATE REPRESENTING THE START AND END OF DST or FALSE if the zone doesn't support dst 222 */ 223 function getDSTRange($year, $zone){ 224 $range = array(); 225 if(empty($zone['dstOffset'])){ 226 return false; 227 } 228 229 $range['start'] = $this->getDateFromRules($year, $zone['dstMonth'], $zone['dstStartday'], $zone['dstWeekday'], $zone['dstStartTimeSec']); 230 $range['end'] = $this->getDateFromRules($year, $zone['stdMonth'], $zone['stdStartday'], $zone['stdWeekday'], $zone['stdStartTimeSec']); 231 return $range; 232 } 233 234 function inDST($date, $zone){ 235 $datetime = explode(' ', $date); 236 $dateSplit = explode('-', $datetime[0]); 237 if(empty($dateSplit[2]))return false; 238 $dstRange = $this->getDSTRange($dateSplit[0], $zone); 239 if(!$dstRange){ 240 return false; 241 } 242 $datestamp = strtotime($date); 243 $startstamp = strtotime($dstRange['start']); 244 $endstamp = strtotime($dstRange['end']); 245 if((($datestamp >= $startstamp || $datestamp < $endstamp) && $startstamp > $endstamp) 246 || ($datestamp >= $startstamp && $datestamp < $endstamp) 247 ){ 248 return true; 249 } 250 return false; 251 } 252 253 function get_regular_expression($format) { 254 $newFormat = ''; 255 $regPositions = array(); 256 $ignoreNextChar = false; 257 $count = 1; 258 $format_characters = str_split($format, 1); 259 foreach ($format_characters as $char) { 260 if (!$ignoreNextChar && isset($this->supported_strings[$char])) { 261 $newFormat.= '('.$this->supported_strings[$char].')'; 262 $regPositions[$char] = $count; 263 $count++; 264 } else { 265 $ignoreNextChar = false; 266 267 $newFormat.= $char; 268 269 } 270 if ($char == "\\") { 271 $ignoreNextChar = true; 272 } 273 } 274 275 return array('format'=>$newFormat, 'positions'=>$regPositions); 276 277 } 278 279 function check_matching_format($date, $format, $toformat = '') { 280 $regs = array(); 281 $startreg = $this->get_regular_expression($format); 282 if (!empty($toformat)) { 283 $otherreg = $this->get_regular_expression($toformat); 284 //if the other format has the same regular expression then maybe it is shifting month and day position or something similar let it go for formating 285 if ($startreg['format'] == $otherreg['format']) { 286 return false; 287 } 288 } 289 ereg($startreg['format'], $date, $regs); 290 if (empty($regs)) { 291 return false; 292 } 293 return true; 294 } 295 296 /** 297 * Build a true PHP format string from a user supplied format string 298 * 299 * @param string $format 300 * @return string 301 * @access private 302 * @see $time_token_map 303 */ 304 function build_format($format) 305 { 306 $format = str_split($format, 1); 307 $return = ''; 308 foreach ($format as $char) { 309 $return .= (isset($this->time_token_map[$char])) ? 310 $this->time_token_map[$char] : 311 $char; 312 } 313 return $return; 314 } 315 316 function swap_formats($date, $startFormat, $endFormat) { 317 $startreg = $this->get_regular_expression($startFormat); 318 ereg($startreg['format'], $date, $regs); 319 $newDate = $endFormat; 320 321 //handle 12 to 24 hour conversion 322 if (isset($startreg['positions']['h']) && !isset($startreg['positions']['H']) && $regs[$startreg['positions']['h']] !== '' && strpos($endFormat, 'H') > -1) { 323 $startreg['positions']['H'] = sizeof($startreg['positions']) + 1; 324 $regs[$startreg['positions']['H']] = $regs[$startreg['positions']['h']]; 325 if ((isset($startreg['positions']['A']) && isset($regs[$startreg['positions']['A']]) && $regs[$startreg['positions']['A']] == 'PM') || (isset($startreg['positions']['a']) && isset($regs[$startreg['positions']['a']]) && $regs[$startreg['positions']['a']] == 'pm')) { 326 if ($regs[$startreg['positions']['h']] != 12) { 327 $regs[$startreg['positions']['H']] = $regs[$startreg['positions']['h']] + 12; 328 } 329 } 330 if ((isset($startreg['positions']['A']) && isset($regs[$startreg['positions']['A']])&& $regs[$startreg['positions']['A']] == 'AM') || (isset($startreg['positions']['a']) && isset($regs[$startreg['positions']['a']]) && $regs[$startreg['positions']['a']] == 'am')) { 331 if ($regs[$startreg['positions']['h']] == 12) { 332 $regs[$startreg['positions']['H']] = 0; 333 } 334 } 335 } 336 if (!empty($startreg['positions']['H']) && !empty($regs[$startreg['positions']['H']]) && !isset($startreg['positions']['h']) && strpos($endFormat, 'h') > -1) { 337 $startreg['positions']['h'] = sizeof($startreg['positions']) + 1; 338 $regs[$startreg['positions']['h']] = $regs[$startreg['positions']['H']]; 339 if (!isset($startreg['positions']['A'])) { 340 $startreg['positions']['A'] = sizeof($startreg['positions']) + 1; 341 $regs[$startreg['positions']['A']] = 'AM'; 342 } 343 if (!isset($startreg['positions']['a'])) { 344 $startreg['positions']['a'] = sizeof($startreg['positions']) + 1; 345 $regs[$startreg['positions']['a']] = 'am'; 346 } 347 if ($regs[$startreg['positions']['H']] > 11) { 348 $regs[$startreg['positions']['h']] = $regs[$startreg['positions']['H']] - 12; 349 if ($regs[$startreg['positions']['h']] == 0) { 350 $regs[$startreg['positions']['h']] = 12; 351 } 352 $regs[$startreg['positions']['a']] = 'pm'; 353 $regs[$startreg['positions']['A']] = 'PM'; 354 } 355 if ($regs[$startreg['positions']['H']] == 0) { 356 $regs[$startreg['positions']['h']] = 12; 357 $regs[$startreg['positions']['a']] = 'am'; 358 $regs[$startreg['positions']['A']] = 'AM'; 359 } 360 } 361 if (!empty($startreg['positions']['h'])) { 362 if (!isset($regs[$startreg['positions']['h']])) { 363 $regs[$startreg['positions']['h']] = '12'; 364 } else if (strlen($regs[$startreg['positions']['h']]) < 2) 365 $regs[$startreg['positions']['h']] = '0'.$regs[$startreg['positions']['h']]; 366 } 367 if (!empty($startreg['positions']['H'])) { 368 // if no hour is set or it is equal to 0, set it explicitly to "00" 369 if (empty($regs[$startreg['positions']['H']])) { 370 $regs[$startreg['positions']['H']] = '00'; 371 } else if (strlen($regs[$startreg['positions']['H']]) < 2) 372 $regs[$startreg['positions']['H']] = '0'.$regs[$startreg['positions']['H']]; 373 } 374 if (!empty($startreg['positions']['d'])) { 375 if (!isset($regs[$startreg['positions']['d']])) { 376 $regs[$startreg['positions']['d']] = '01'; 377 } else if (strlen($regs[$startreg['positions']['d']]) < 2) 378 $regs[$startreg['positions']['d']] = '0'.$regs[$startreg['positions']['d']]; 379 } 380 if (!empty($startreg['positions']['i'])) { 381 // if no minute is set or it is equal to 0, set it explicitly to "00" 382 if (empty($regs[$startreg['positions']['i']])) { 383 $regs[$startreg['positions']['i']] = '00'; 384 } else if (strlen($regs[$startreg['positions']['i']]) < 2) 385 $regs[$startreg['positions']['i']] = '0'.$regs[$startreg['positions']['i']]; 386 } 387 if (!empty($startreg['positions']['m'])) { 388 if (!isset($regs[$startreg['positions']['m']])) { 389 $regs[$startreg['positions']['m']] = '01'; 390 } elseif(strlen($regs[$startreg['positions']['m']]) < 2) 391 $regs[$startreg['positions']['m']] = '0'.$regs[$startreg['positions']['m']]; 392 } 393 if (!empty($startreg['positions']['Y'])) { 394 if (!isset($regs[$startreg['positions']['Y']])) { 395 $regs[$startreg['positions']['Y']] = '2000'; 396 } 397 } 398 if (!empty($startreg['positions']['s'])) { 399 if (!isset($regs[$startreg['positions']['s']])) { 400 $regs[$startreg['positions']['s']] = '00'; 401 } else if (strlen($regs[$startreg['positions']['s']]) < 2) 402 $regs[$startreg['positions']['s']] = '0'.$regs[$startreg['positions']['s']]; 403 } else { 404 $startreg['positions']['s'] = sizeof($startreg['positions']) + 1; 405 $regs[$startreg['positions']['s']] = '00'; 406 } 407 foreach($startreg['positions'] as $key=>$val) { 408 if (isset($regs[$val])) { 409 $newDate = str_replace($key, $regs[$val], $newDate); 410 } 411 } 412 return $newDate; 413 414 } 415 function to_display_time($date, $meridiem = true, $offset = true) { 416 417 $date = trim($date); 418 if (empty($date)) { 419 return $date; 420 } 421 if ($offset) { 422 $date = $this->handle_offset($date, $this->get_db_date_time_format(), true); 423 } 424 return $this->to_display($date, $this->dbTimeFormat, $this->get_time_format($meridiem)); 425 } 426 427 function to_display_date($date, $use_offset = true) { 428 $date = trim($date); 429 430 if (empty($date)) { 431 return $date; 432 } 433 if ($use_offset) 434 $date = $this->handle_offset($date, $this->get_db_date_time_format(), true); 435 436 return $this->to_display($date, $this->dbDayFormat, $this->get_date_format()); 437 } 438 439 function to_display_date_time($date, $meridiem = true, $offset = true, $user = null) { 440 $date = trim($date); 441 442 if (empty($date)) { 443 return $date; 444 } 445 446 $args = array( 447 'time' => $date, 448 'meridiem' => $meridiem, 449 'offset' => $offset, 450 'user' => $user, 451 ); 452 453 // todo use __METHOD__ once PHP5 minimum verison is required 454 $cache_key = md5('TimeDate::to_display_date_time_' . serialize($args)); 455 $cached_value = sugar_cache_retrieve($cache_key); 456 if (!is_null($cached_value)) { 457 return $cached_value; 458 } 459 460 if ($offset) { 461 $date = $this->handle_offset($date, $this->get_db_date_time_format(), true, $user); 462 } 463 464 $return_value = $this->to_display($date, $this->get_db_date_time_format(), $this->get_date_time_format($meridiem, $user)); 465 466 sugar_cache_put($cache_key, $return_value); 467 return $return_value; 468 } 469 470 function to_display($date, $fromformat, $toformat) { 471 $date = trim($date); 472 if (empty($date)) { 473 return $date; 474 } 475 return $this->swap_formats($date, $fromformat, $toformat); 476 } 477 478 function to_db($date) { 479 $date = trim($date); 480 $date = trim($date); 481 if (empty($date)) { 482 return $date; 483 } 484 if (strlen($date) <= 10) { 485 $date .= ' ' . $this->get_default_midnight(); 486 } 487 $date = $this->swap_formats($date, $this->get_date_time_format(), $this->get_db_date_time_format()); 488 return $this->handle_offset($date, $this->get_db_date_time_format(), false); 489 } 490 491 /** 492 * @todo This should return the raw text to be included within a <script> tag. 493 * Having this display it's own <script> keeps it from being able to be embedded 494 * in another Javascript file to allow for better caching 495 */ 496 function get_javascript_validation() { 497 $cal_date_format = $this->get_cal_date_format(); 498 $timereg = $this->get_regular_expression($this->get_time_format()); 499 $datereg = $this->get_regular_expression($this->get_date_format()); 500 $date_pos = ''; 501 foreach($datereg['positions'] as $type=>$pos) { 502 if (empty($date_pos)) { 503 $date_pos.= "'$type': $pos"; 504 } else { 505 $date_pos.= ",'$type': $pos"; 506 } 507 508 } 509 $date_pos = '{'.$date_pos.'}'; 510 if (preg_match('/\)([^\d])\(/', $timereg['format'], $match)) { 511 $time_separator = $match[1]; 512 } else { 513 $time_separator = ":"; 514 } 515 $hour_offset = $this->get_hour_offset() * 60 * 60; 516 517 // Add in the number formatting styles here as well, we have been handling this with individual modules. 518 require_once ('modules/Currencies/Currency.php'); 519 list($num_grp_sep, $dec_sep) = get_number_seperators(); 520 521 $the_script = "<script type=\"text/javascript\">\n" 522 ."\tvar time_reg_format = '".$timereg['format']."';\n" 523 ."\tvar date_reg_format = '".$datereg['format']."';\n" 524 ."\tvar date_reg_positions = $date_pos;\n" 525 ."\tvar time_separator = '$time_separator';\n" 526 ."\tvar cal_date_format = '$cal_date_format';\n" 527 ."\tvar time_offset = $hour_offset;\n" 528 ."\tvar num_grp_sep = '$num_grp_sep';\n" 529 ."\tvar dec_sep = '$dec_sep';\n" 530 ."</script>"; 531 532 return $the_script; 533 534 } 535 536 function to_db_date($date, $use_offset = true) { 537 $date = trim($date); 538 if (empty($date)) { 539 return $date; 540 } 541 if ($use_offset) { 542 $date = $this->to_db($date); 543 $date = $this->swap_formats($date, $this->dbDayFormat, $this->dbDayFormat); 544 } else { 545 $date = $this->swap_formats($date, $this->get_date_format(), $this->dbDayFormat); 546 } 547 548 return $date; 549 } 550 551 function to_db_time($date, $use_offset = true) { 552 $date = trim($date); 553 if (empty($date)) { 554 return $date; 555 } 556 if ($use_offset){ 557 $date =$this->to_db($date, $use_offset); 558 $date = $this->swap_formats($date, $this->get_db_date_time_format(), $this->dbTimeFormat); 559 }else{ 560 $date = $this->swap_formats($date, $this->get_time_format(), $this->dbTimeFormat); 561 } 562 return $date; 563 564 565 } 566 567 /* takes a local Date & Time value, concats it, and returns the separate but 568 * properly calculated GMT value as an array*/ 569 function to_db_date_time($date, $time) { 570 global $current_user; 571 if(is_object($current_user)) { 572 $timeFormat = $current_user->getUserDateTimePreferences(); 573 } else { 574 $timeFormat['date'] = $this->dbDayFormat; 575 $timeFormat['time'] = $this->dbTimeFormat; 576 } 577 $dt = ''; 578 $newDate = ''; 579 $retDateTime = array(); 580 581 // concat: ('.' breaks strtotime()) 582 $time = str_replace('.',':',$time); 583 $dt = $date." ".$time; 584 $newDate = $this->swap_formats($dt, $timeFormat['date'].' '.$timeFormat['time'] , $this->dbDayFormat.' '.$this->dbTimeFormat); 585 $retDateTime = explode(' ', $newDate); 586 return $retDateTime; 587 } 588 589 function getUserEventOffset($user_in_dst, $event_in_dst){ 590 if($user_in_dst && !$event_in_dst ){ 591 return -3600; 592 } 593 if(!$user_in_dst && $event_in_dst ){ 594 return 3600; 595 } 596 return 0; 597 } 598 599 /************************************************************** 600 U S E Time GMT Delta Server Client U/E Delta Server GMT 601 USER IN LA and server in NY 602 D D D 12 19 -3 0 -4 603 D D S 12 20 -3 -1 -4 604 D S D 12 19 -2 0 -5 605 D S S 12 20 -2 -1 -5 606 S D D 12 19 -4 1 -4 607 S D S 12 20 -4 0 -4 608 S S D 12 19 -3 1 -5 609 S S S 12 20 -3 0 -5 610 611 612 User in LA and server in gmt there are no DST for server 613 D S D 12 19 -7 0 0 614 D S S 12 20 -7 -1 0 615 616 S S D 12 19 -8 1 0 617 S S S 12 20 -8 0 0 618 619 ***************************************************************/ 620 621 /** 622 * handles offset values for Timezones and DST 623 * @param $date string date/time formatted in user's selected 624 * format 625 * @param $format string destination format value as passed to PHP's 626 * date() funtion 627 * @param $to boolean 628 * @param $user object user object from which Timezone and DST 629 * @param $usetimezone string timezone name as it appears in timezones.php 630 * values will be derived 631 * @return string date formatted and adjusted for TZ and DST 632 */ 633 function handle_offset($date, $format, $to = true, $user = null, $usetimezone = null) { 634 global $sugar_config; 635 $date = trim($date); 636 // Samir Gandhi 637 // This has been commented out because it is going through the wrong code path 638 // Email module was broken and thats why its commented 639 //if($this->use_old_gmt()){ 640 //return $this->handle_offset_depricated($date, $format, $to); 641 //} 642 if (empty($date)) { 643 return $date; 644 } 645 $args = array( 646 'date' => $date, 647 'format' => $format, 648 'to' => $to, 649 'user' => $user, 650 'usetimezone' => $usetimezone, 651 ); 652 $cache_key = md5('TimeDate::handle_offset_' . serialize($args)); 653 $cached_result = sugar_cache_retrieve($cache_key); 654 if (!is_null($cached_result)) { 655 return $cached_result; 656 } 657 if (strtotime($date) == -1) { 658 return $date; 659 } 660 $deltaServerGMT = date('Z'); 661 662 if ( !empty($usetimezone) ) 663 $timezone = $GLOBALS['timezones'][$usetimezone]; 664 else 665 $timezone = $this->getUserTimeZone($user); 666 $deltaServerUser = $this->get_hour_offset($to, $timezone); 667 $event_in_ds = $this->inDST($date,$timezone ); 668 $user_in_ds = $this->inDST(date('Y-m-d H:i:s'),$timezone ); 669 $server_in_ds = date('I'); 670 $ue = $this->getUserEventOffset($user_in_ds, $event_in_ds); 671 $zone = 1; 672 if (!$to) { 673 $zone = -1; 674 } 675 $result = date($format, strtotime($date) + $deltaServerUser * 3600 + ($ue + $deltaServerGMT) * $zone); 676 sugar_cache_put($cache_key, $result); 677 return $result; 678 } 679 680 function use_old_gmt(){ 681 if(isset($_SESSION['GMTO'])){ 682 return $_SESSION['GMTO']; 683 } 684 $db = DBManagerFactory::getInstance(); 685 $fix_name = 'DST Fix'; 686 $result =$db->query("Select * from versions where name='$fix_name'"); 687 $valid = $db->fetchByAssoc($result); 688 if($valid){ 689 $_SESSION['GMTO'] = false; 690 }else{ 691 $_SESSION['GMTO'] = true; 692 } 693 return $_SESSION['GMTO']; 694 } 695 696 /** 697 *This function is depricated don't use it. It is only for backwards compatibility until the admin runs the upgrade script 698 * 699 * @param unknown_type $date 700 * @param unknown_type $format 701 * @param unknown_type $to 702 * @return unknown 703 */ 704 function handle_offset_depricated($date, $format, $to = true) { 705 706 $date = trim($date); 707 if (empty($date)) { 708 return $date; 709 } 710 if (strtotime($date) == -1) { 711 return $date; 712 } 713 $zone = date('Z'); 714 if (!$to) { 715 $zone *= -1; 716 } 717 return date($format, strtotime($date) + $this->get_hour_offset($to) * 60 * 60 + $zone); 718 } 719 720 /* this method will take an input $date variable (expecting Y-m-d format) 721 * and get the GMT equivalent - with an hour-level granularity : 722 * return the max value of a given locale's 723 * date+time in GMT metrics (i.e., if in PDT, "2005-01-01 23:59:59" would be 724 * "2005-01-02 06:59:59" in GMT metrics) 725 */ 726 function handleOffsetMax($date, $format, $to = true) { 727 global $current_user; 728 $gmtDateTime = array($date); // for errors 729 /* check for bad date formatting */ 730 $date = trim($date); 731 732 if (empty($date)) { 733 return $gmtDateTime; 734 } 735 736 if (strtotime($date) == -1) { 737 return $gmtDateTime; 738 } 739 740 /* cn: passed $date var will be a "MAX" value, which we need to return 741 as a GMT date/time pair to provide for hour-level granularity */ 742 /* this ridiculousness b/c PHP returns current time when passing "today" 743 or "tomorrow" as strtotime() args */ 744 $dateNoTime = date('Y-m-d', strtotime($date)); 745 746 /* handle timezone and daylight savings */ 747 $dateWithTimeMin = $dateNoTime.' 00:00:00'; 748 $dateWithTimeMax = $dateNoTime.' 23:59:59'; 749 750 751 $offsetDateMin = $this->handle_offset($dateWithTimeMin, $this->dbDayFormat.' '.$this->dbTimeFormat, false); 752 $offsetDateMax = $this->handle_offset($dateWithTimeMax, $this->dbDayFormat.' '.$this->dbTimeFormat, false); 753 754 755 $exOffsetDateMax = explode(' ', $offsetDateMax); 756 $exOffsetDateMin = explode(' ', $offsetDateMin); 757 $gmtDateTime['date'] = $exOffsetDateMax[0]; 758 $gmtDateTime['time'] = $exOffsetDateMax[1]; 759 $gmtDateTime['min'] = $offsetDateMin; 760 $gmtDateTime['max'] = $offsetDateMax; 761 762 return $gmtDateTime; 763 } 764 765 766 function get_gmt_db_datetime() { 767 return gmdate('Y-m-d H:i:s'); 768 } 769 770 function get_gmt_db_date() { 771 return gmdate($this->dbDayFormat); 772 } 773 774 /* 775 * assumes that olddatetime is in Y-m-d H:i:s format 776 */ 777 function convert_to_gmt_datetime($olddatetime) { 778 if (!empty($olddatetime)) { 779 return date('Y-m-d H:i:s', strtotime($olddatetime) - date('Z')); 780 } 781 } 782 783 784 785 function merge_date_time($date, $time) { 786 $merged = $date.' '.$time; 787 788 return $merged; 789 } 790 function merge_time_meridiem($date, $format, $mer) { 791 $date = trim($date); 792 if (empty($date)) { 793 return $date; 794 } 795 $fakeMerFormat = str_replace(array('a', 'A'), array('!@!', '!@!'), $format); 796 $noMerFormat = str_replace(array('a', 'A'), array('', ''), $format); 797 $newDate = $this->swap_formats($date, $noMerFormat, $fakeMerFormat); 798 return str_replace('!@!', $mer, $newDate); 799 800 } 801 802 /* 803 * AMPMMenu 804 * This method renders a <select> HTML form element based on the 805 * user's time format preferences. 806 * 807 * todo: There is hardcoded HTML in here that does not allow for localization 808 * of the AM/PM am/pm Strings in this drop down menu. Also, perhaps 809 * change to the substr_count function calls to strpos 810 * 811 */ 812 function AMPMMenu($prefix, $date, $attrs = '') { 813 814 if (substr_count($this->get_time_format(), 'a') == 0 && substr_count($this->get_time_format(), 'A') == 0) { 815 return ''; 816 } 817 $menu = "<select name='".$prefix."meridiem' ".$attrs.">"; 818 819 if (strpos($this->get_time_format(), 'a') > -1) { 820 821 if (substr_count($date, 'am') > 0) 822 $menu.= "<option value='am' selected>am"; 823 else 824 $menu.= "<option value='am'>am"; 825 if (substr_count($date, 'pm') > 0) 826 $menu.= "<option value='pm' selected>pm"; 827 else 828 $menu.= "<option value='pm'>pm"; 829 830 } else { 831 832 if (substr_count($date, 'AM') > 0) 833 $menu.= "<option value='AM' selected>AM"; 834 else 835 $menu.= "<option value='AM'>AM"; 836 if (substr_count($date, 'PM') > 0) { 837 $menu.= "<option value='PM' selected>PM"; 838 } else 839 $menu.= "<option value='PM'>PM"; 840 841 } 842 843 return $menu.'</select>'; 844 } 845 846 function get_hour_offset($to = true, $timezone = null) { 847 848 $timeDelta = $this->adjustmentForUserTimeZone($timezone) /60.0; 849 if ($to) { 850 return -1.0 * $timeDelta; 851 } 852 return 1.0 * $timeDelta; 853 } 854 855 function get_time_format($meridiem = true, $user = null) { 856 global $current_user; 857 global $sugar_config; 858 859 if(empty($user)) $user = $current_user; 860 861 if ($user instanceof User && $user->getPreference('timef')) { 862 $timeFormat = $user->getPreference('timef'); 863 } else { 864 $timeFormat = $sugar_config['default_time_format']; 865 } 866 if (!$meridiem) { 867 $timeFormat = str_replace(array('a', 'A'), array('', ''), $timeFormat); 868 } 869 return $timeFormat; 870 } 871 872 function get_date_format($user = null) { 873 global $current_user; 874 global $sugar_config; 875 876 if(empty($user)) $user = $current_user; 877 878 if ($user instanceof User && $user->getPreference('datef')) { 879 return $user->getPreference('datef'); 880 } 881 if(!empty($sugar_config['default_date_format'])){ 882 return $sugar_config['default_date_format']; 883 }else{ 884 return ''; 885 } 886 } 887 888 function get_date_time_format($meridiem = true, $user = null) { 889 return $this->get_date_format($user).' '.$this->get_time_format($meridiem, $user); 890 } 891 892 function get_db_date_time_format() { 893 return $this->dbDayFormat.' '.$this->dbTimeFormat; 894 } 895 function get_cal_date_format() { 896 $format = str_replace('Y', '%Y', $this->get_date_format()); 897 $format = str_replace('m', '%m', $format); 898 $format = str_replace('d', '%d', $format); 899 return $format; 900 } 901 function get_cal_time_format() { 902 $format = str_replace('a', '%P', $this->get_date_format()); 903 $format = str_replace('A', '%p', $format); 904 $format = str_replace('h', '%I', $format); 905 $format = str_replace('H', '%H', $format); 906 $format = str_replace('i', '%M', $format); 907 $format = str_replace('s', '%S', $format); 908 return $format; 909 } 910 function get_cal_date_time_format() { 911 return $this->get_cal_date_format().' '.$this->get_cal_time_format(); 912 } 913 914 function get_user_date_format() { 915 $format = str_replace('Y', 'yyyy', $this->get_date_format()); 916 $format = str_replace('m', 'mm', $format); 917 $format = str_replace('d', 'dd', $format); 918 return $format; 919 } 920 921 function get_user_time_format() { 922 global $sugar_config; 923 $time_pref = $this->get_time_format(); 924 925 if(!empty($time_pref) && !empty($sugar_config['time_formats'][$time_pref])) { 926 return $sugar_config['time_formats'][$time_pref]; 927 } 928 929 return '23:00'; //default 930 /* 931 // Commented out by Collin (doesn't seem to work properly) 932 return $this->to_display_time('23:00:00', true, false); 933 */ 934 } 935 936 function get_microtime_string() { 937 $now = (string) microtime(); 938 $now = explode(' ', $now); 939 $unique_id = $now[1].str_replace('.', '', $now[0]); 940 unset($now); 941 942 return $unique_id; 943 } 944 945 function get_default_midnight($refresh = false) 946 { 947 static $default_midnight = null; 948 if (is_null($default_midnight) || $refresh) { 949 $time_mapping = array( 950 'H' => '00', 951 'h' => '12', 952 'i' => '00', 953 's' => '00', 954 'a' => 'am', 955 'A' => 'AM', 956 ); 957 $default_midnight = str_replace( 958 array_keys($time_mapping), 959 $time_mapping, 960 $this->get_time_format() // should this be using get_user_time_format()? 961 ); 962 } 963 return $default_midnight; 964 } 965 } 966 967 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
|
|
|
|