2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero 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.
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 Affero General Public License for more
19 * You should have received a copy of the GNU Affero 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
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.
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 Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero 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 ********************************************************************************/
38 require_once 'include/SugarDateTime.php';
42 * New Time & Date handling class
44 * - to_db_time() requires either full datetime or time, won't work with just date
45 * The reason is that it's not possible to know if short string has only date or only time,
46 * and it makes more sense to assume time for the time conversion function.
50 const DB_DATE_FORMAT = 'Y-m-d';
51 const DB_TIME_FORMAT = 'H:i:s';
52 // little optimization
53 const DB_DATETIME_FORMAT = 'Y-m-d H:i:s';
54 const RFC2616_FORMAT = 'D, d M Y H:i:s \G\M\T';
56 // Standard DB date/time formats
57 // they are constant, vars are for compatibility
58 public $dbDayFormat = self::DB_DATE_FORMAT;
59 public $dbTimeFormat = self::DB_TIME_FORMAT;
62 * Regexp for matching format elements
65 protected static $format_to_regexp = array(
84 * Relation between date() and strftime() formats
87 public static $format_to_str = array(
112 * GMT timezone object
116 protected static $gmtTimezone;
136 protected $current_user_id;
141 protected $current_user_tz;
144 * Separator for current user time format
148 protected $time_separator;
151 * Always consider user TZ to be GMT and date format DB format - for SOAP etc.
155 protected $always_db = false;
158 * Global instance of TimeDate
161 protected static $timedate;
163 public $allow_cache = true;
165 public function __construct(User $user = null)
167 if (self::$gmtTimezone == null) {
168 self::$gmtTimezone = new DateTimeZone("UTC");
170 $this->now = new SugarDateTime();
171 $this->tzGMT($this->now);
176 * Set flag specifying we should always use DB format
180 public function setAlwaysDb($flag = true)
182 $this->always_db = $flag;
188 * Get "always use DB format" flag
191 public function isAlwaysDb()
193 return !empty($GLOBALS['disable_date_format']) || $this->always_db;
197 * Get TimeDate instance
200 public static function getInstance()
202 if(empty(self::$timedate)) {
203 if(ini_get('date.timezone') == '') {
204 // Remove warning about default timezone
205 date_default_timezone_set(@date('e'));
207 $tz = self::guessTimezone();
208 } catch(Exception $e) {
209 $tz = "UTC"; // guess failed, switch to UTC
211 if(isset($GLOBALS['log'])) {
212 $GLOBALS['log']->fatal("Configuration variable date.timezone is not set, guessed timezone $tz. Please set date.timezone=\"$tz\" in php.ini!");
214 date_default_timezone_set($tz);
216 self::$timedate = new self;
218 return self::$timedate;
222 * Set current user for this object
227 public function setUser(User $user = null)
235 * Figure out what the required user is
237 * The order is: supplied parameter, TimeDate's user, global current user
242 protected function _getUser(User $user = null)
248 $user = $GLOBALS['current_user'];
254 * Get timezone for the specified user
257 * @return DateTimeZone
259 protected function _getUserTZ(User $user = null)
261 $user = $this->_getUser($user);
263 if (empty($user) || $this->isAlwaysDb()) {
264 return self::$gmtTimezone;
267 if ($this->allow_cache && $user->id == $this->current_user_id && ! empty($this->current_user_tz)) {
268 // current user is cached
269 return $this->current_user_tz;
273 $usertimezone = $user->getPreference('timezone');
274 if(empty($usertimezone)) {
275 return self::$gmtTimezone;
277 $tz = new DateTimeZone($usertimezone);
278 } catch (Exception $e) {
279 $GLOBALS['log']->fatal('Unknown timezone: ' . $usertimezone);
280 return self::$gmtTimezone;
283 if (empty($this->current_user_id)) {
284 $this->current_user_id = $user->id;
285 $this->current_user_tz = $tz;
292 * Clears all cached data regarding current user
294 public function clearCache()
296 $this->current_user_id = null;
297 $this->current_user_tz = null;
298 $this->time_separator = null;
299 $this->now = new SugarDateTime();
303 * Get user date format.
306 * @param [User] $user user object, current user if not specified
309 public function get_date_format(User $user = null)
311 $user = $this->_getUser($user);
313 if (empty($user) || $this->isAlwaysDb()) {
314 return self::DB_DATE_FORMAT;
317 $datef = $user->getPreference('datef');
318 if(empty($datef) && isset($GLOBALS['current_user']) && $GLOBALS['current_user'] !== $user) {
319 // if we got another user and it has no date format, try current user
320 $datef = $GLOBALS['current_user']->getPreference('datef');
323 $datef = $GLOBALS['sugar_config']['default_date_format'];
333 * Get user time format.
336 * @param User $user user object, current user if not specified
339 public function get_time_format(/*User*/ $user = null)
341 if(is_bool($user) || func_num_args() > 1) {
342 // BC dance - old signature was boolean, User
343 $GLOBALS['log']->fatal('TimeDate::get_time_format(): Deprecated API used, please update you code - get_time_format() now has one argument of type User');
344 if(func_num_args() > 1) {
345 $user = func_get_arg(1);
350 $user = $this->_getUser($user);
352 if (empty($user) || $this->isAlwaysDb()) {
353 return self::DB_TIME_FORMAT;
356 $timef = $user->getPreference('timef');
357 if(empty($timef) && isset($GLOBALS['current_user']) && $GLOBALS['current_user'] !== $user) {
358 // if we got another user and it has no time format, try current user
359 $timef = $GLOBALS['current_user']->getPreference('$timef');
362 $timef = $GLOBALS['sugar_config']['default_time_format'];
371 * Get user datetime format.
374 * @param [User] $user user object, current user if not specified
377 public function get_date_time_format($user = null)
379 // BC fix - had (bool, user) signature before
380 if(!($user instanceof User)) {
381 if(func_num_args() > 1) {
382 $user = func_get_arg(1);
383 if(!($user instanceof User)) {
390 return $this->merge_date_time($this->get_date_format($user), $this->get_time_format($user));
394 * Make one datetime string from date string and time string
396 * @param string $date
397 * @param string $time
398 * @return string New datetime string
400 function merge_date_time($date, $time)
402 return $date . ' ' . $time;
406 * Split datetime string into date & time
408 * @param string $datetime
411 function split_date_time($datetime)
413 return explode(' ', $datetime);
416 function get_cal_date_format()
418 return str_replace(array_keys(self::$format_to_str), array_values(self::$format_to_str), $this->get_date_format());
421 function get_cal_time_format()
423 return str_replace(array_keys(self::$format_to_str), array_values(self::$format_to_str), $this->get_time_format());
426 function get_cal_date_time_format()
428 return str_replace(array_keys(self::$format_to_str), array_values(self::$format_to_str), $this->get_date_time_format());
432 * Verify if the date string conforms to a format
434 * @param string $date
435 * @param string $format Format to check
436 * @param string $toformat
437 * @return bool Is the date ok?
439 public function check_matching_format($date, $format)
442 $dt = SugarDateTime::createFromFormat($format, $date);
443 if (!is_object($dt)) {
446 } catch (Exception $e) {
453 * Format DateTime object as DB datetime
455 * @param DateTime $date
458 public function asDb(DateTime $date)
460 $date->setTimezone(self::$gmtTimezone);
461 return $date->format($this->get_db_date_time_format());
465 * Format date as DB-formatted field type
466 * @param DateTime $date
467 * @param string $type Field type - date, time, datetime[combo]
469 public function asDbType(DateTime $date, $type)
473 return $this->asDbDate($date);
476 return $this->asDbtime($date);
479 case 'datetimecombo':
481 return $this->asDb($date);
486 * Format DateTime object as user datetime
488 * @param DateTime $date
491 public function asUser(DateTime $date, User $user = null)
493 $this->tzUser($date, $user);
494 return $date->format($this->get_date_time_format($user));
498 * Format date as user-formatted field type
499 * @param DateTime $date
500 * @param string $type Field type - date, time, datetime[combo]
502 public function asUserType(DateTime $date, $type, User $user = null)
506 return $this->asUserDate($date, true, $user);
509 return $this->asUserTime($date, true, $user);
512 case 'datetimecombo':
514 return $this->asUser($date, $user);
519 * Produce timestamp offset by user's timezone
521 * So if somebody converts it to format assuming GMT, it would actually display user's time.
522 * This is used by Javascript.
524 * @param DateTime $date
527 public function asUserTs(DateTime $date, User $user = null)
529 return $date->format('U')+$this->_getUserTZ($user)->getOffset($date);
533 * Format DateTime object as DB date
534 * Note: by default does not convert TZ!
535 * @param DateTime $date
536 * @param boolean $tz Perform TZ conversion?
539 public function asDbDate(DateTime $date, $tz = false)
541 if($tz) $date->setTimezone(self::$gmtTimezone);
542 return $date->format($this->get_db_date_format());
546 * Format DateTime object as user date
547 * Note: by default does not convert TZ!
548 * @param DateTime $date
549 * @param boolean $tz Perform TZ conversion?
552 public function asUserDate(DateTime $date, $tz = false, User $user = null)
554 if($tz) $this->tzUser($date, $user);
555 return $date->format($this->get_date_format($user));
559 * Format DateTime object as DB time
561 * @param DateTime $date
564 public function asDbTime(DateTime $date)
566 $date->setTimezone(self::$gmtTimezone);
567 return $date->format($this->get_db_time_format());
571 * Format DateTime object as user time
573 * @param DateTime $date
576 public function asUserTime(DateTime $date, User $user = null)
578 $this->tzUser($date, $user);
579 return $date->format($this->get_time_format($user));
583 * Get DateTime from DB datetime string
585 * @param string $date
586 * @return SugarDateTime
588 public function fromDb($date)
591 return SugarDateTime::createFromFormat(self::DB_DATETIME_FORMAT, $date, self::$gmtTimezone);
592 } catch (Exception $e) {
593 $GLOBALS['log']->error("fromDb: Conversion of $date from DB format failed: {$e->getMessage()}");
599 * Create a date from a certain type of field in DB format
600 * The types are: date, time, datatime[combo]
601 * @param string $date the datetime string
602 * @param string $type string type
603 * @return SugarDateTime
605 public function fromDbType($date, $type)
609 return $this->fromDbDate($date);
612 return $this->fromDbFormat($date, self::DB_TIME_FORMAT);
615 case 'datetimecombo':
617 return $this->fromDb($date);
622 * Get DateTime from DB date string
624 * @param string $date
625 * @return SugarDateTime
627 public function fromDbDate($date)
630 return SugarDateTime::createFromFormat(self::DB_DATE_FORMAT, $date, self::$gmtTimezone);
631 } catch (Exception $e) {
632 $GLOBALS['log']->error("fromDbDate: Conversion of $date from DB format failed: {$e->getMessage()}");
638 * Get DateTime from DB datetime string using non-standard format
640 * Non-standard format usually would be only date, only time, etc.
642 * @param string $date
643 * @param string $format format to accept
644 * @return SugarDateTime
646 public function fromDbFormat($date, $format)
649 return SugarDateTime::createFromFormat($format, $date, self::$gmtTimezone);
650 } catch (Exception $e) {
651 $GLOBALS['log']->error("fromDbFormat: Conversion of $date from DB format $format failed: {$e->getMessage()}");
657 * Get DateTime from user datetime string
659 * @param string $date
660 * @return SugarDateTime
662 public function fromUser($date, User $user = null)
665 return SugarDateTime::createFromFormat($this->get_date_time_format($user), $date, $this->_getUserTZ($user));
666 } catch (Exception $e) {
667 $uf = $this->get_date_time_format($user);
668 $GLOBALS['log']->error("fromUser: Conversion of $date from user format $uf failed: {$e->getMessage()}");
674 * Create a date from a certain type of field in user format
675 * The types are: date, time, datatime[combo]
676 * @param string $date the datetime string
677 * @param string $type string type
679 * @return SugarDateTime
681 public function fromUserType($date, $type, $user = null)
685 return $this->fromUserDate($date, $user);
688 return $this->fromUserTime($date, $user);
691 case 'datetimecombo':
693 return $this->fromUser($date, $user);
698 * Get DateTime from user time string
700 * @param string $date
701 * @return SugarDateTime
703 public function fromUserTime($date, User $user = null)
706 return SugarDateTime::createFromFormat($this->get_time_format($user), $date, $this->_getUserTZ($user));
707 } catch (Exception $e) {
708 $uf = $this->get_time_format($user);
709 $GLOBALS['log']->error("fromUserTime: Conversion of $date from user format $uf failed: {$e->getMessage()}");
715 * Get DateTime from user date string
716 * Usually for calendar-related functions like holidays
717 * Note: by default does not convert tz!
718 * @param string $date
719 * @param bool $convert_tz perform TZ converson?
721 * @return SugarDateTime
723 public function fromUserDate($date, $convert_tz = false, User $user = null)
726 return SugarDateTime::createFromFormat($this->get_date_format($user), $date, $convert_tz?$this->_getUserTZ($user):self::$gmtTimezone);
727 } catch (Exception $e) {
728 $uf = $this->get_date_format($user);
729 $GLOBALS['log']->error("fromUserDate: Conversion of $date from user format $uf failed: {$e->getMessage()}");
735 * Create a date object from any string
737 * Same formats accepted as for DateTime ctor
739 * @param string $date
741 * @return SugarDateTime
743 public function fromString($date, User $user = null)
746 return new SugarDateTime($date, $this->_getUserTZ($user));
747 } catch (Exception $e) {
748 $GLOBALS['log']->error("fromString: Conversion of $date from string failed: {$e->getMessage()}");
754 * Create DateTime from timestamp
756 * @param interger|string $ts
757 * @return SugarDateTime
759 public function fromTimestamp($ts)
761 return new SugarDateTime("@$ts");
765 * Convert DateTime to GMT timezone
766 * @param DateTime $date
769 public function tzGMT(DateTime $date)
771 return $date->setTimezone(self::$gmtTimezone);
775 * Convert DateTime to user timezone
776 * @param DateTime $date
777 * @param [User] $user
780 public function tzUser(DateTime $date, User $user = null)
782 return $date->setTimezone($this->_getUserTZ($user));
786 * Get string defining midnight in current user's format
787 * @param string $format Time format to use
790 protected function _get_midnight($format = null)
792 $zero = new DateTime("@0", self::$gmtTimezone);
793 return $zero->format($format?$format:$this->get_time_format());
798 * Basic conversion function
800 * @param string $date
801 * @param string $fromFormat
802 * @param DateTimeZone $fromTZ
803 * @param string $toFormat
804 * @param DateTimeZone $toTZ
805 * @param bool $expand
807 protected function _convert($date, $fromFormat, $fromTZ, $toFormat, $toTZ, $expand = false)
814 if ($expand && strlen($date) <= 10) {
815 $date = $this->expandDate($date, $fromFormat);
817 $phpdate = SugarDateTime::createFromFormat($fromFormat, $date, $fromTZ);
818 if ($phpdate == false) {
819 // var_dump($date, $phpdate, $fromFormat, DateTime::getLastErrors() );
820 $GLOBALS['log']->error("convert: Conversion of $date from $fromFormat to $toFormat failed");
823 if ($fromTZ !== $toTZ) {
824 $phpdate->setTimeZone($toTZ);
826 return $phpdate->format($toFormat);
827 } catch (Exception $e) {
828 // var_dump($date, $phpdate, $fromFormat, $fromTZ, DateTime::getLastErrors() );
829 $GLOBALS['log']->error("Conversion of $date from $fromFormat to $toFormat failed: {$e->getMessage()}");
835 * Convert DB datetime to local datetime
837 * TZ conversion is controlled by parameter
839 * @param string $date Original date in DB format
840 * @param bool $meridiem Ignored for BC
841 * @param bool $convert_tz Perform TZ conversion?
842 * @param User $user User owning the conversion formats
843 * @return string Date in display format
845 function to_display_date_time($date, $meridiem = true, $convert_tz = true, $user = null)
847 return $this->_convert($date,
848 self::DB_DATETIME_FORMAT, self::$gmtTimezone, $this->get_date_time_format($user),
849 $convert_tz ? $this->_getUserTZ($user) : self::$gmtTimezone, true);
853 * Converts DB time string to local time string
855 * TZ conversion depends on parameter
857 * @param string $date Time in DB format
858 * @param bool $meridiem
859 * @param bool $convert_tz Perform TZ conversion?
860 * @return string Time in user-defined format
862 public function to_display_time($date, $meridiem = true, $convert_tz = true)
864 if($convert_tz && strpos($date, ' ') === false) {
865 // we need TZ adjustment but have no date, assume today
866 $date = $this->expandTime($date, self::DB_DATETIME_FORMAT, self::$gmtTimezone);
868 return $this->_convert($date,
869 $convert_tz ? self::DB_DATETIME_FORMAT : self::DB_TIME_FORMAT, self::$gmtTimezone,
870 $this->get_time_format(), $convert_tz ? $this->_getUserTZ() : self::$gmtTimezone);
874 * Splits time in given format into components
876 * Components: h, m, s, a (am/pm) if format requires it
877 * If format has am/pm, hour is 12-based, otherwise 24-based
879 * @param string $date
880 * @param string $format
883 public function splitTime($date, $format)
885 if (! ($date instanceof DateTime)) {
886 $date = SugarDateTime::createFromFormat($format, $date);
888 $ampm = strpbrk($format, 'aA');
890 "h" => ($ampm == false) ? $date->format("H") : $date->format("h"),
891 'm' => $date->format("i"),
892 's' => $date->format("s")
895 $datearr['a'] = ($ampm{0} == 'a') ? $date->format("a") : $date->format("A");
901 * Converts DB date string to local date string
903 * TZ conversion depens on parameter
905 * @param string $date Date in DB format
906 * @param bool $convert_tz Perform TZ conversion?
907 * @return string Date in user-defined format
909 public function to_display_date($date, $convert_tz = true)
911 return $this->_convert($date,
912 self::DB_DATETIME_FORMAT, self::$gmtTimezone,
913 $this->get_date_format(), $convert_tz ? $this->_getUserTZ() : self::$gmtTimezone, true);
917 * Convert date from format to format
919 * No TZ conversion is performed!
921 * @param string $date
922 * @param string $fromformat Source format
923 * @param string $toformat Target format
924 * @return string Converted date
926 function to_display($date, $from, $to)
928 return $this->_convert($date, $from, self::$gmtTimezone, $to, self::$gmtTimezone);
932 * Get DB datetime format
935 public function get_db_date_time_format()
937 return self::DB_DATETIME_FORMAT;
944 public function get_db_date_format()
946 return self::DB_DATE_FORMAT;
953 public function get_db_time_format()
955 return self::DB_TIME_FORMAT;
959 * Convert date from local datetime to GMT-based DB datetime
961 * Includes TZ conversion.
963 * @param string $date
964 * @return string Datetime in DB format
966 public function to_db($date)
968 return $this->_convert($date,
969 $this->get_date_time_format(), $this->_getUserTZ(),
970 $this->get_db_date_time_format(), self::$gmtTimezone,
975 * Convert local datetime to DB date
977 * TZ conversion depends on parameter. If false, only format conversion is performed.
979 * @param string $date Local date
980 * @param bool $convert_tz Should time and TZ be taken into account?
981 * @return string Date in DB format
983 public function to_db_date($date, $convert_tz = true)
985 return $this->_convert($date,
986 $this->get_date_time_format(), $convert_tz ? $this->_getUserTZ() : self::$gmtTimezone,
987 self::DB_DATE_FORMAT, self::$gmtTimezone, true);
991 * Convert local datetime to DB time
993 * TZ conversion depends on parameter. If false, only format conversion is performed.
995 * @param string $date Local date
996 * @param bool $convert_tz Should time and TZ be taken into account?
997 * @return string Time in DB format
999 public function to_db_time($date, $convert_tz = true)
1001 $format = $this->get_date_time_format();
1002 $tz = $convert_tz ? $this->_getUserTZ() : self::$gmtTimezone;
1003 if($convert_tz && strpos($date, ' ') === false) {
1004 // we need TZ adjustment but have short string, expand it to full one
1005 // FIXME: if the string is short, should we assume date or time?
1006 $date = $this->expandTime($date, $format, $tz);
1008 return $this->_convert($date,
1009 $convert_tz ? $format : $this->get_time_format(),
1011 self::DB_TIME_FORMAT, self::$gmtTimezone);
1015 * Takes a Date & Time value in local format and converts them to DB format
1018 * @param string $date
1019 * @param string $time
1020 * @return array Date & time in DB format
1022 public function to_db_date_time($date, $time)
1025 $phpdate = SugarDateTime::createFromFormat($this->get_date_time_format(),
1026 $this->merge_date_time($date, $time), self::$gmtTimezone);
1027 if ($phpdate == false) {
1028 return array('', '');
1030 return array($this->asDbDate($phpdate), $this->asDbTime($phpdate));
1031 } catch (Exception $e) {
1032 $GLOBALS['log']->error("Conversion of $date,$time failed");
1033 return array('', '');
1038 * Return current time in DB format
1041 public function nowDb()
1043 if(!$this->allow_cache) {
1044 $nowGMT = $this->getNow();
1046 $nowGMT = $this->now;
1048 return $this->asDb($nowGMT);
1052 * Return current date in DB format
1055 public function nowDbDate()
1057 if(!$this->allow_cache) {
1058 $nowGMT = $this->getNow();
1060 $nowGMT = $this->now;
1062 return $this->asDbDate($nowGMT, true);
1066 * Get 'now' DateTime object
1067 * @param bool $userTz return in user timezone?
1068 * @return SugarDateTime
1070 public function getNow($userTz = false)
1072 if(!$this->allow_cache) {
1073 return new SugarDateTime("now", $userTz?$this->_getUserTz():self::$gmtTimezone);
1075 // TODO: should we return clone?
1076 $now = clone $this->now;
1078 return $this->tzUser($now);
1085 * For testability - predictable time value
1086 * @param DateTime $now
1088 public function setNow($now)
1095 * Return current datetime in local format
1098 public function now()
1100 return $this->asUser($this->getNow());
1104 * Return current date in User format
1107 public function nowDate()
1109 return $this->asUserDate($this->getNow());
1113 * Get user format's time separator
1116 public function timeSeparator()
1118 if (empty($this->time_separator)) {
1119 $this->time_separator = $this->timeSeparatorFormat($this->get_time_format());
1121 return $this->time_separator;
1125 * Find out format's time separator
1126 * @param string $timeformat Time format
1128 public function timeSeparatorFormat($timeformat)
1130 $date = $this->_convert("00:11:22", self::DB_TIME_FORMAT, null, $timeformat, null);
1131 if (preg_match('/\d+(.+?)11/', $date, $matches)) {
1140 * Returns start and end of a certain local date in GMT
1141 * Example: for May 19 in PDT start would be 2010-05-19 07:00:00, end would be 2010-05-20 06:59:59
1142 * @param string|DateTime $date Date in any suitable format
1143 * @return array Start & end date in start, startdate, starttime, end, enddate, endtime
1145 public function getDayStartEndGMT($date, User $user = null)
1147 if ($date instanceof DateTime) {
1149 $min->setTimezone($this->_getUserTZ($user));
1151 $max->setTimezone($this->_getUserTZ($user));
1153 $min = new DateTime($date, $this->_getUserTZ($user));
1154 $max = new DateTime($date, $this->_getUserTZ($user));
1156 $min->setTime(0, 0);
1157 $max->setTime(23, 59, 59);
1159 $min->setTimezone(self::$gmtTimezone);
1160 $max->setTimezone(self::$gmtTimezone);
1162 $result['start'] = $this->asDb($min);
1163 $result['startdate'] = $this->asDbDate($min);
1164 $result['starttime'] = $this->asDbTime($min);
1165 $result['end'] = $this->asDb($max);
1166 $result['enddate'] = $this->asDbDate($max);
1167 $result['endtime'] = $this->asDbtime($max);
1173 * Expand date format by adding midnight to it
1174 * Note: date is assumed to be in target format already
1175 * @param string $date
1176 * @param string $format Target format
1179 public function expandDate($date, $format)
1181 $formats = $this->split_date_time($format);
1182 if(isset($formats[1])) {
1183 return $this->merge_date_time($date, $this->_get_midnight($formats[1]));
1189 * Expand time format by adding today to it
1190 * Note: time is assumed to be in target format already
1191 * @param string $date
1192 * @param string $format Target format
1193 * @param DateTimeZone $tz
1195 public function expandTime($date, $format, $tz)
1197 $formats = $this->split_date_time($format);
1198 if(isset($formats[1])) {
1199 $now = clone $this->getNow();
1200 $now->setTimezone($tz);
1201 return $this->merge_date_time($now->format($formats[0]), $date);
1207 * Get midnight (start of the day) in local time format
1209 * @return Time string
1211 function get_default_midnight()
1213 return $this->_get_midnight($this->get_time_format());
1217 * Get the name of the timezone for the user
1218 * @param User $user User, default - current user
1221 public static function userTimezone(User $user = null)
1223 $user = self::getInstance()->_getUser($user);
1227 $tz = self::getInstance()->_getUserTZ($user);
1229 return $tz->getName();
1235 * Guess the timezone for the current user
1238 public static function guessTimezone($userOffset = 0)
1240 if(!is_numeric($userOffset)) {
1243 $defaultZones= array(
1244 'America/Anchorage', 'America/Los_Angeles', 'America/Phoenix', 'America/Chicago',
1245 'America/New_York', 'America/Argentina/Buenos_Aires', 'America/Montevideo',
1246 'Europe/London', 'Europe/Amsterdam', 'Europe/Athens', 'Europe/Moscow',
1247 'Asia/Tbilisi', 'Asia/Omsk', 'Asia/Jakarta', 'Asia/Hong_Kong',
1248 'Asia/Tokyo', 'Pacific/Guam', 'Australia/Sydney', 'Australia/Perth',
1251 $now = new DateTime();
1252 $tzlist = timezone_identifiers_list();
1253 if($userOffset == 0) {
1254 $gmtOffset = date('Z');
1256 if(in_array($nowtz, $tzlist)) {
1257 array_unshift($defaultZones, $nowtz);
1259 $nowtz = timezone_name_from_abbr(date('T'), $gmtOffset, date('I'));
1260 if(in_array($nowtz, $tzlist)) {
1261 array_unshift($defaultZones, $nowtz);
1265 $gmtOffset = $userOffset * 60;
1267 foreach($defaultZones as $zoneName) {
1268 $tz = new DateTimeZone($zoneName);
1269 if($tz->getOffset($now) == $gmtOffset) {
1270 return $tz->getName();
1274 foreach($tzlist as $zoneName) {
1275 $tz = new DateTimeZone($zoneName);
1276 if($tz->getOffset($now) == $gmtOffset) {
1277 return $tz->getName();
1284 * Get the description of the user timezone for specific date
1286 * We need the date because it can be DST or non-DST
1287 * Note it's different from TZ name in tzName() that relates to current date
1288 * @param User $user User, default - current user
1291 public static function userTimezoneSuffix(DateTime $date, User $user = null)
1293 $user = self::getInstance()->_getUser($user);
1297 self::getInstance()->tzUser($date, $user);
1298 return $date->format('T(P)');
1302 * Get display name for a certain timezone
1303 * Note: it uses current date for GMT offset, so it may be not suitable for displaying generic dates
1304 * @param string|DateTimeZone $name TZ name
1307 public static function tzName($name)
1312 if($name instanceof DateTimeZone) {
1315 $tz = timezone_open($name);
1320 $now = new DateTime("now", $tz);
1321 $off = $now->getOffset();
1322 $translated = translate('timezone_dom','',$name);
1323 if(is_string($translated) && !empty($translated)) {
1324 $name = $translated;
1326 return sprintf("%s (GMT%+2d:%02d)%s", str_replace('_',' ', $name), $off/3600, (abs($off)/60)%60, "");//$now->format('I')==1?"(+DST)":"");
1330 public static function _sortTz($a, $b)
1332 if($a[0] == $b[0]) {
1333 return strcmp($a[1], $b[1]);
1335 return $a[0]<$b[0]?-1:1;
1340 * Get list of all timezones in the system
1343 public static function getTimezoneList()
1345 $now = new DateTime();
1346 $res_zones = $zones = array();
1347 foreach(timezone_identifiers_list() as $zoneName) {
1348 $tz = new DateTimeZone($zoneName);
1349 $zones[$zoneName] = array($tz->getOffset($now), self::tzName($zoneName));
1351 uasort($zones, array('TimeDate', '_sortTz'));
1352 foreach($zones as $name => $zonedef) {
1353 $res_zones[$name] = $zonedef[1];
1359 * Print timestamp in RFC2616 format:
1362 public static function httpTime($ts = null)
1367 return gmdate(self::RFC2616_FORMAT, $ts);
1371 * Create datetime object from calendar array
1372 * @param array $time
1373 * @return SugarDateTime
1375 public function fromTimeArray($time)
1377 if (! isset( $time) || count($time) == 0 )
1379 return $this->nowDb();
1381 elseif ( isset( $time['ts']))
1383 return $this->fromTimestamp($time['ts']);
1385 elseif ( isset( $time['date_str']))
1387 return $this->fromDb($time['date_str']);
1394 $now = $this->getNow(true);
1396 $month = $now->month;
1398 if (isset($time['sec']))
1400 $sec = $time['sec'];
1402 if (isset($time['min']))
1404 $min = $time['min'];
1406 if (isset($time['hour']))
1408 $hour = $time['hour'];
1410 if (isset($time['day']))
1412 $day = $time['day'];
1414 if (isset($time['month']))
1416 $month = $time['month'];
1418 if (isset($time['year']) && $time['year'] >= 1970)
1420 $year = $time['year'];
1422 return $now->setDate($year, $month, $day)->setTime($hour, $min, $sec)->setTimeZone(self::$gmtTimezone);
1428 * Returns the date portion of a datetime string
1430 * @param string $datetime
1433 public function getDatePart($datetime)
1435 list($date, $time) = $this->split_date_time($datetime);
1440 * Returns the time portion of a datetime string
1442 * @param string $datetime
1445 public function getTimePart($datetime)
1447 list($date, $time) = $this->split_date_time($datetime);
1452 * Returns the offset from user's timezone to GMT
1454 * @param DateTime $time When the offset is taken, default is now
1455 * @return int Offset in minutes
1457 public function getUserUTCOffset(User $user = null, DateTime $time = null)
1462 return $this->_getUserTZ($user)->getOffset($time) / 60;
1466 * Create regexp from datetime format
1467 * @param string $format
1468 * @return string Regular expression string
1470 public static function get_regular_expression($format)
1473 $regPositions = array();
1474 $ignoreNextChar = false;
1476 foreach (str_split($format) as $char) {
1477 if (! $ignoreNextChar && isset(self::$format_to_regexp[$char])) {
1478 $newFormat .= '(' . self::$format_to_regexp[$char] . ')';
1479 $regPositions[$char] = $count;
1482 $ignoreNextChar = false;
1483 $newFormat .= $char;
1486 if ($char == "\\") {
1487 $ignoreNextChar = true;
1491 return array('format' => $newFormat, 'positions' => $regPositions);
1494 // format - date expression ('' means now) for start and end of the range
1495 protected $date_expressions = array(
1496 'yesterday' => array("-1 day", "-1 day"),
1497 'today' => array("", ""),
1498 'tomorrow' => array("+1 day", "+1 day"),
1499 'last_7_days' => array("-6 days", ""),
1500 'next_7_days' => array("", "+6 days"),
1501 'last_30_days' => array("-29 days", ""),
1502 'next_30_days' => array("", "+29 days"),
1506 * Parse date template
1507 * @param string $template Date expression
1508 * @param bool $daystart Do we want start or end of the day?
1511 protected function parseFromTemplate($template, $daystart, User $user = null)
1513 $now = $this->tzUser($this->getNow(), $user);
1514 if(!empty($template[0])) {
1515 $now->modify($template[0]);
1518 return $now->get_day_begin();
1520 return $now->get_day_end();
1525 * Get month-long range mdiff months from now
1527 protected function diffMon($mdiff, User $user)
1529 $now = $this->tzUser($this->getNow(), $user);
1530 $now->setDate($now->year, $now->month+$mdiff, 1);
1531 $start = $now->get_day_begin();
1532 $end = $now->setDate($now->year, $now->month, $now->days_in_month)->setTime(23, 59, 59);
1533 return array($start, $end);
1537 * Get year-long range ydiff years from now
1539 protected function diffYear($ydiff, User $user)
1541 $now = $this->tzUser($this->getNow(), $user);
1542 $now->setDate($now->year+$ydiff, 1, 1);
1543 $start = $now->get_day_begin();
1544 $end = $now->setDate($now->year, 12, 31)->setTime(23, 59, 59);
1545 return array($start, $end);
1549 * Parse date range expression
1550 * Returns beginning and end of the range as a date
1551 * @param string $range
1555 public function parseDateRange($range, User $user = null)
1557 if(isset($this->date_expressions[$range])) {
1558 return array($this->parseFromTemplate($this->date_expressions[$range][0], true, $user),
1559 $this->parseFromTemplate($this->date_expressions[$range][1], false, $user)
1564 return $this->diffMon(1, $user);
1566 return $this->diffMon(-1, $user);
1568 return $this->diffMon(0, $user);
1570 return $this->diffYear(-1, $user);
1572 return $this->diffYear(0, $user);
1574 return $this->diffYear(1, $user);
1580 /********************* OLD functions, should not be used publicly anymore ****************/
1582 * Merge time without am/pm with am/pm string
1583 * @TODO find better way to do this!
1584 * @deprecated for public use
1585 * @param string $date
1586 * @param string $format User time format
1587 * @param string $mer
1590 function merge_time_meridiem($date, $format, $mer)
1592 $date = trim($date);
1596 $fakeMerFormat = str_replace(array('a', 'A'), array('@~@', '@~@'), $format);
1597 $noMerFormat = str_replace(array('a', 'A'), array('', ''), $format);
1598 $newDate = $this->swap_formats($date, $noMerFormat, $fakeMerFormat);
1599 return str_replace('@~@', $mer, $newDate);
1603 * @deprecated for public use
1604 * Convert date from one format to another
1606 * @param string $date
1607 * @param string $from
1611 public function swap_formats($date, $from, $to)
1613 return $this->_convert($date, $from, self::$gmtTimezone, $to, self::$gmtTimezone);
1617 * @deprecated for public use
1618 * handles offset values for Timezones and DST
1619 * @param $date string date/time formatted in user's selected format
1620 * @param $format string destination format value as passed to PHP's date() funtion
1621 * @param $to boolean
1622 * @param $user object user object from which Timezone and DST
1623 * @param $usetimezone string timezone name
1624 * values will be derived
1625 * @return string date formatted and adjusted for TZ and DST
1627 function handle_offset($date, $format, $to = true, $user = null, $usetimezone = null)
1629 $tz = empty($usetimezone)?$this->_getUserTZ($user):new DateTimeZone($usetimezone);
1630 $dateobj = new SugarDateTime($date, $to? self::$gmtTimezone : $tz);
1631 $dateobj->setTimezone($to ? $tz: self::$gmtTimezone);
1632 return $dateobj->format($format);
1633 // return $this->_convert($date, $format, $to ? self::$gmtTimezone : $tz, $format, $to ? $tz : self::$gmtTimezone);
1637 * @deprecated for public use
1638 * Get current GMT datetime in DB format
1641 function get_gmt_db_datetime()
1643 return $this->nowDb();
1647 * @deprecated for public use
1648 * Get current GMT date in DB format
1651 function get_gmt_db_date()
1653 return $this->nowDbDate();
1657 * @deprecated for public use
1658 * this method will take an input $date variable (expecting Y-m-d format)
1659 * and get the GMT equivalent - with an hour-level granularity :
1660 * return the max value of a given locale's
1661 * date+time in GMT metrics (i.e., if in PDT, "2005-01-01 23:59:59" would be
1662 * "2005-01-02 06:59:59" in GMT metrics)
1664 function handleOffsetMax($date)
1666 $min = new DateTime($date, $this->_getUserTZ());
1667 $min->setTime(0, 0);
1668 $max = new DateTime($date, $this->_getUserTZ());
1669 $max->setTime(23, 59, 59);
1671 $min->setTimezone(self::$gmtTimezone);
1672 $max->setTimezone(self::$gmtTimezone);
1674 $gmtDateTime['date'] = $this->asDbDate($max, false);
1675 $gmtDateTime['time'] = $this->asDbDate($max, false);
1676 $gmtDateTime['min'] = $this->asDb($min);
1677 $gmtDateTime['max'] = $this->asDb($max);
1679 return $gmtDateTime;
1683 * @deprecated for public use
1684 * this returns the adjustment for a user against the server time
1686 * @return integer number of minutes to adjust a time by to get the appropriate time for the user
1688 public function adjustmentForUserTimeZone()
1690 $tz = $this->_getUserTZ();
1691 $server_tz = new DateTimeZone(date_default_timezone_get());
1692 if ($tz && $server_tz) {
1693 return ($server_tz->getOffset($this->now) - $tz->getOffset($this->now)) / 60;
1699 * @deprecated for public use
1700 * assumes that olddatetime is in Y-m-d H:i:s format
1702 function convert_to_gmt_datetime($olddatetime)
1704 if (! empty($olddatetime)) {
1705 return date('Y-m-d H:i:s', strtotime($olddatetime) - date('Z'));
1710 * @deprecated for public use
1711 * get user timezone info
1713 public function getUserTimeZone(User $user = null)
1715 $tz = $this->_getUserTZ($user);
1716 return array("gmtOffset" => $tz->getOffset($this->now) / 60);
1720 * @deprecated for public use
1721 * get timezone start & end
1723 public function getDSTRange($year, $zone = null)
1726 $tz = timezone_open($zone);
1729 $tz = $this->_getUserTZ();
1732 $year_date = SugarDateTime::createFromFormat("Y", $year, self::$gmtTimezone);
1733 $year_end = clone $year_date;
1734 $year_end->setDate((int) $year, 12, 31);
1735 $year_end->setTime(23, 59, 59);
1736 $year_date->setDate((int) $year, 1, 1);
1737 $year_date->setTime(0, 0, 0);
1739 $transitions = $tz->getTransitions($year_date->ts, $year_end->ts);
1741 if(version_compare(PHP_VERSION, '5.3.0', '<')) {
1742 // <5.3.0 ignores parameters, advance manually to current year
1743 $start_ts = $year_date->ts;
1744 while(isset($transitions[$idx]) && $transitions[$idx]["ts"] < $start_ts) $idx++;
1747 while (isset($transitions[$idx]) && !$transitions[$idx]["isdst"]) $idx++;
1748 if(isset($transitions[$idx])) {
1749 $result["start"] = $this->fromTimestamp($transitions[$idx]["ts"])->asDb();
1752 while (isset($transitions[$idx]) && $transitions[$idx]["isdst"]) $idx++;
1753 if(isset($transitions[$idx])) {
1754 $result["end"] = $this->fromTimestamp($transitions[$idx]["ts"])->asDb();
1759 /****************** GUI stuff that really shouldn't be here, will be moved ************/
1761 * Get Javascript variables setup for user date format validation
1762 * @deprecated moved to SugarView
1763 * @return string JS code
1765 function get_javascript_validation()
1767 return SugarView::getJavascriptValidation();
1772 * This method renders a <select> HTML form element based on the
1773 * user's time format preferences, with give date's value highlighted.
1775 * If user's prefs have no AM/PM string, returns empty string.
1777 * @todo There is hardcoded HTML in here that does not allow for localization
1778 * of the AM/PM am/pm Strings in this drop down menu. Also, perhaps
1779 * change to the substr_count function calls to strpos
1780 * TODO: Remove after full switch to fields
1782 * @param string $prefix Prefix for SELECT
1783 * @param string $date Date in display format
1784 * @param string $attrs Additional attributes for SELECT
1785 * @return string SELECT HTML
1787 function AMPMMenu($prefix, $date, $attrs = '')
1789 $tf = $this->get_time_format();
1790 $am = strpbrk($tf, 'aA');
1794 $selected = array("am" => "", "pm" => "", "AM" => "", "PM" => "");
1795 if (preg_match('/([ap]m)/i', $date, $match)) {
1796 $selected[$match[1]] = " selected";
1799 $menu = "<select name='" . $prefix . "meridiem' " . $attrs . ">";
1800 if ($am{0} == 'a') {
1801 $menu .= "<option value='am'{$selected["am"]}>am";
1802 $menu .= "<option value='pm'{$selected["pm"]}>pm";
1804 $menu .= "<option value='AM'{$selected["AM"]}>AM";
1805 $menu .= "<option value='PM'{$selected["PM"]}>PM";
1808 return $menu . '</select>';
1812 * TODO: Remove after full switch to fields
1814 function get_user_date_format()
1816 return str_replace(array('Y', 'm', 'd'), array('yyyy', 'mm', 'dd'), $this->get_date_format());
1820 * TODO: Remove after full switch to fields
1824 function get_user_time_format()
1826 global $sugar_config;
1827 $time_pref = $this->get_time_format();
1829 if (! empty($time_pref) && ! empty($sugar_config['time_formats'][$time_pref])) {
1830 return $sugar_config['time_formats'][$time_pref];
1833 return '23:00'; //default