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 const SECONDS_IN_A_DAY = 86400;
58 // Standard DB date/time formats
59 // they are constant, vars are for compatibility
60 public $dbDayFormat = self::DB_DATE_FORMAT;
61 public $dbTimeFormat = self::DB_TIME_FORMAT;
64 * Regexp for matching format elements
67 protected static $format_to_regexp = array(
86 * Relation between date() and strftime() formats
89 public static $format_to_str = array(
114 * GMT timezone object
118 protected static $gmtTimezone;
138 protected $current_user_id;
143 protected $current_user_tz;
146 * Separator for current user time format
150 protected $time_separator;
153 * Always consider user TZ to be GMT and date format DB format - for SOAP etc.
157 protected $always_db = false;
160 * Global instance of TimeDate
163 protected static $timedate;
165 public $allow_cache = true;
167 public function __construct(User $user = null)
169 if (self::$gmtTimezone == null) {
170 self::$gmtTimezone = new DateTimeZone("UTC");
172 $this->now = new SugarDateTime();
173 $this->tzGMT($this->now);
178 * Set flag specifying we should always use DB format
182 public function setAlwaysDb($flag = true)
184 $this->always_db = $flag;
190 * Get "always use DB format" flag
193 public function isAlwaysDb()
195 return !empty($GLOBALS['disable_date_format']) || $this->always_db;
199 * Get TimeDate instance
202 public static function getInstance()
204 if(empty(self::$timedate)) {
205 if(ini_get('date.timezone') == '') {
206 // Remove warning about default timezone
207 date_default_timezone_set(@date('e'));
209 $tz = self::guessTimezone();
210 } catch(Exception $e) {
211 $tz = "UTC"; // guess failed, switch to UTC
213 if(isset($GLOBALS['log'])) {
214 $GLOBALS['log']->fatal("Configuration variable date.timezone is not set, guessed timezone $tz. Please set date.timezone=\"$tz\" in php.ini!");
216 date_default_timezone_set($tz);
218 self::$timedate = new self;
220 return self::$timedate;
224 * Set current user for this object
229 public function setUser(User $user = null)
237 * Figure out what the required user is
239 * The order is: supplied parameter, TimeDate's user, global current user
244 protected function _getUser(User $user = null)
250 $user = $GLOBALS['current_user'];
256 * Get timezone for the specified user
259 * @return DateTimeZone
261 protected function _getUserTZ(User $user = null)
263 $user = $this->_getUser($user);
265 if (empty($user) || $this->isAlwaysDb()) {
266 return self::$gmtTimezone;
269 if ($this->allow_cache && $user->id == $this->current_user_id && ! empty($this->current_user_tz)) {
270 // current user is cached
271 return $this->current_user_tz;
275 $usertimezone = $user->getPreference('timezone');
276 if(empty($usertimezone)) {
277 return self::$gmtTimezone;
279 $tz = new DateTimeZone($usertimezone);
280 } catch (Exception $e) {
281 $GLOBALS['log']->fatal('Unknown timezone: ' . $usertimezone);
282 return self::$gmtTimezone;
285 if (empty($this->current_user_id)) {
286 $this->current_user_id = $user->id;
287 $this->current_user_tz = $tz;
294 * Clears all cached data regarding current user
296 public function clearCache()
298 $this->current_user_id = null;
299 $this->current_user_tz = null;
300 $this->time_separator = null;
301 $this->now = new SugarDateTime();
305 * Get user date format.
308 * @param [User] $user user object, current user if not specified
311 public function get_date_format(User $user = null)
313 $user = $this->_getUser($user);
315 if (empty($user) || $this->isAlwaysDb()) {
316 return self::DB_DATE_FORMAT;
319 $datef = $user->getPreference('datef');
320 if(empty($datef) && isset($GLOBALS['current_user']) && $GLOBALS['current_user'] !== $user) {
321 // if we got another user and it has no date format, try current user
322 $datef = $GLOBALS['current_user']->getPreference('datef');
325 $datef = $GLOBALS['sugar_config']['default_date_format'];
335 * Get user time format.
338 * @param User $user user object, current user if not specified
341 public function get_time_format(/*User*/ $user = null)
343 if(is_bool($user) || func_num_args() > 1) {
344 // BC dance - old signature was boolean, User
345 $GLOBALS['log']->fatal('TimeDate::get_time_format(): Deprecated API used, please update you code - get_time_format() now has one argument of type User');
346 if(func_num_args() > 1) {
347 $user = func_get_arg(1);
352 $user = $this->_getUser($user);
354 if (empty($user) || $this->isAlwaysDb()) {
355 return self::DB_TIME_FORMAT;
358 $timef = $user->getPreference('timef');
359 if(empty($timef) && isset($GLOBALS['current_user']) && $GLOBALS['current_user'] !== $user) {
360 // if we got another user and it has no time format, try current user
361 $timef = $GLOBALS['current_user']->getPreference('$timef');
364 $timef = $GLOBALS['sugar_config']['default_time_format'];
373 * Get user datetime format.
376 * @param [User] $user user object, current user if not specified
379 public function get_date_time_format($user = null)
381 // BC fix - had (bool, user) signature before
382 if(!($user instanceof User)) {
383 if(func_num_args() > 1) {
384 $user = func_get_arg(1);
385 if(!($user instanceof User)) {
392 return $this->merge_date_time($this->get_date_format($user), $this->get_time_format($user));
396 * Make one datetime string from date string and time string
398 * @param string $date
399 * @param string $time
400 * @return string New datetime string
402 function merge_date_time($date, $time)
404 return $date . ' ' . $time;
408 * Split datetime string into date & time
410 * @param string $datetime
413 function split_date_time($datetime)
415 return explode(' ', $datetime);
418 function get_cal_date_format()
420 return str_replace(array_keys(self::$format_to_str), array_values(self::$format_to_str), $this->get_date_format());
423 function get_cal_time_format()
425 return str_replace(array_keys(self::$format_to_str), array_values(self::$format_to_str), $this->get_time_format());
428 function get_cal_date_time_format()
430 return str_replace(array_keys(self::$format_to_str), array_values(self::$format_to_str), $this->get_date_time_format());
434 * Verify if the date string conforms to a format
436 * @param string $date
437 * @param string $format Format to check
438 * @param string $toformat
439 * @return bool Is the date ok?
441 public function check_matching_format($date, $format)
444 $dt = SugarDateTime::createFromFormat($format, $date);
445 if (!is_object($dt)) {
448 } catch (Exception $e) {
455 * Format DateTime object as DB datetime
457 * @param DateTime $date
460 public function asDb(DateTime $date)
462 $date->setTimezone(self::$gmtTimezone);
463 return $date->format($this->get_db_date_time_format());
467 * Format date as DB-formatted field type
468 * @param DateTime $date
469 * @param string $type Field type - date, time, datetime[combo]
471 public function asDbType(DateTime $date, $type)
475 return $this->asDbDate($date);
478 return $this->asDbtime($date);
481 case 'datetimecombo':
483 return $this->asDb($date);
488 * Format DateTime object as user datetime
490 * @param DateTime $date
493 public function asUser(DateTime $date, User $user = null)
495 $this->tzUser($date, $user);
496 return $date->format($this->get_date_time_format($user));
500 * Format date as user-formatted field type
501 * @param DateTime $date
502 * @param string $type Field type - date, time, datetime[combo]
504 public function asUserType(DateTime $date, $type, User $user = null)
508 return $this->asUserDate($date, true, $user);
511 return $this->asUserTime($date, true, $user);
514 case 'datetimecombo':
516 return $this->asUser($date, $user);
521 * Produce timestamp offset by user's timezone
523 * So if somebody converts it to format assuming GMT, it would actually display user's time.
524 * This is used by Javascript.
526 * @param DateTime $date
529 public function asUserTs(DateTime $date, User $user = null)
531 return $date->format('U')+$this->_getUserTZ($user)->getOffset($date);
535 * Format DateTime object as DB date
536 * Note: by default does not convert TZ!
537 * @param DateTime $date
538 * @param boolean $tz Perform TZ conversion?
541 public function asDbDate(DateTime $date, $tz = false)
543 if($tz) $date->setTimezone(self::$gmtTimezone);
544 return $date->format($this->get_db_date_format());
548 * Format DateTime object as user date
549 * Note: by default does not convert TZ!
550 * @param DateTime $date
551 * @param boolean $tz Perform TZ conversion?
554 public function asUserDate(DateTime $date, $tz = false, User $user = null)
556 if($tz) $this->tzUser($date, $user);
557 return $date->format($this->get_date_format($user));
561 * Format DateTime object as DB time
563 * @param DateTime $date
566 public function asDbTime(DateTime $date)
568 $date->setTimezone(self::$gmtTimezone);
569 return $date->format($this->get_db_time_format());
573 * Format DateTime object as user time
575 * @param DateTime $date
578 public function asUserTime(DateTime $date, User $user = null)
580 $this->tzUser($date, $user);
581 return $date->format($this->get_time_format($user));
585 * Get DateTime from DB datetime string
587 * @param string $date
588 * @return SugarDateTime
590 public function fromDb($date)
593 return SugarDateTime::createFromFormat(self::DB_DATETIME_FORMAT, $date, self::$gmtTimezone);
594 } catch (Exception $e) {
595 $GLOBALS['log']->error("fromDb: Conversion of $date from DB format failed: {$e->getMessage()}");
601 * Create a date from a certain type of field in DB format
602 * The types are: date, time, datatime[combo]
603 * @param string $date the datetime string
604 * @param string $type string type
605 * @return SugarDateTime
607 public function fromDbType($date, $type)
611 return $this->fromDbDate($date);
614 return $this->fromDbFormat($date, self::DB_TIME_FORMAT);
617 case 'datetimecombo':
619 return $this->fromDb($date);
624 * Get DateTime from DB date string
626 * @param string $date
627 * @return SugarDateTime
629 public function fromDbDate($date)
632 return SugarDateTime::createFromFormat(self::DB_DATE_FORMAT, $date, self::$gmtTimezone);
633 } catch (Exception $e) {
634 $GLOBALS['log']->error("fromDbDate: Conversion of $date from DB format failed: {$e->getMessage()}");
640 * Get DateTime from DB datetime string using non-standard format
642 * Non-standard format usually would be only date, only time, etc.
644 * @param string $date
645 * @param string $format format to accept
646 * @return SugarDateTime
648 public function fromDbFormat($date, $format)
651 return SugarDateTime::createFromFormat($format, $date, self::$gmtTimezone);
652 } catch (Exception $e) {
653 $GLOBALS['log']->error("fromDbFormat: Conversion of $date from DB format $format failed: {$e->getMessage()}");
659 * Get DateTime from user datetime string
661 * @param string $date
662 * @return SugarDateTime
664 public function fromUser($date, User $user = null)
667 return SugarDateTime::createFromFormat($this->get_date_time_format($user), $date, $this->_getUserTZ($user));
668 } catch (Exception $e) {
669 $uf = $this->get_date_time_format($user);
670 $GLOBALS['log']->error("fromUser: Conversion of $date from user format $uf failed: {$e->getMessage()}");
676 * Create a date from a certain type of field in user format
677 * The types are: date, time, datatime[combo]
678 * @param string $date the datetime string
679 * @param string $type string type
681 * @return SugarDateTime
683 public function fromUserType($date, $type, $user = null)
687 return $this->fromUserDate($date, $user);
690 return $this->fromUserTime($date, $user);
693 case 'datetimecombo':
695 return $this->fromUser($date, $user);
700 * Get DateTime from user time string
702 * @param string $date
703 * @return SugarDateTime
705 public function fromUserTime($date, User $user = null)
708 return SugarDateTime::createFromFormat($this->get_time_format($user), $date, $this->_getUserTZ($user));
709 } catch (Exception $e) {
710 $uf = $this->get_time_format($user);
711 $GLOBALS['log']->error("fromUserTime: Conversion of $date from user format $uf failed: {$e->getMessage()}");
717 * Get DateTime from user date string
718 * Usually for calendar-related functions like holidays
719 * Note: by default does not convert tz!
720 * @param string $date
721 * @param bool $convert_tz perform TZ converson?
723 * @return SugarDateTime
725 public function fromUserDate($date, $convert_tz = false, User $user = null)
728 return SugarDateTime::createFromFormat($this->get_date_format($user), $date, $convert_tz?$this->_getUserTZ($user):self::$gmtTimezone);
729 } catch (Exception $e) {
730 $uf = $this->get_date_format($user);
731 $GLOBALS['log']->error("fromUserDate: Conversion of $date from user format $uf failed: {$e->getMessage()}");
737 * Create a date object from any string
739 * Same formats accepted as for DateTime ctor
741 * @param string $date
743 * @return SugarDateTime
745 public function fromString($date, User $user = null)
748 return new SugarDateTime($date, $this->_getUserTZ($user));
749 } catch (Exception $e) {
750 $GLOBALS['log']->error("fromString: Conversion of $date from string failed: {$e->getMessage()}");
756 * Create DateTime from timestamp
758 * @param interger|string $ts
759 * @return SugarDateTime
761 public function fromTimestamp($ts)
763 return new SugarDateTime("@$ts");
767 * Convert DateTime to GMT timezone
768 * @param DateTime $date
771 public function tzGMT(DateTime $date)
773 return $date->setTimezone(self::$gmtTimezone);
777 * Convert DateTime to user timezone
778 * @param DateTime $date
779 * @param [User] $user
782 public function tzUser(DateTime $date, User $user = null)
784 return $date->setTimezone($this->_getUserTZ($user));
788 * Get string defining midnight in current user's format
789 * @param string $format Time format to use
792 protected function _get_midnight($format = null)
794 $zero = new DateTime("@0", self::$gmtTimezone);
795 return $zero->format($format?$format:$this->get_time_format());
800 * Basic conversion function
802 * @param string $date
803 * @param string $fromFormat
804 * @param DateTimeZone $fromTZ
805 * @param string $toFormat
806 * @param DateTimeZone $toTZ
807 * @param bool $expand
809 protected function _convert($date, $fromFormat, $fromTZ, $toFormat, $toTZ, $expand = false)
816 if ($expand && strlen($date) <= 10) {
817 $date = $this->expandDate($date, $fromFormat);
819 $phpdate = SugarDateTime::createFromFormat($fromFormat, $date, $fromTZ);
820 if ($phpdate == false) {
821 // var_dump($date, $phpdate, $fromFormat, DateTime::getLastErrors() );
822 $GLOBALS['log']->error("convert: Conversion of $date from $fromFormat to $toFormat failed");
825 if ($fromTZ !== $toTZ) {
826 $phpdate->setTimeZone($toTZ);
828 return $phpdate->format($toFormat);
829 } catch (Exception $e) {
830 // var_dump($date, $phpdate, $fromFormat, $fromTZ, DateTime::getLastErrors() );
831 $GLOBALS['log']->error("Conversion of $date from $fromFormat to $toFormat failed: {$e->getMessage()}");
837 * Convert DB datetime to local datetime
839 * TZ conversion is controlled by parameter
841 * @param string $date Original date in DB format
842 * @param bool $meridiem Ignored for BC
843 * @param bool $convert_tz Perform TZ conversion?
844 * @param User $user User owning the conversion formats
845 * @return string Date in display format
847 function to_display_date_time($date, $meridiem = true, $convert_tz = true, $user = null)
849 return $this->_convert($date,
850 self::DB_DATETIME_FORMAT, self::$gmtTimezone, $this->get_date_time_format($user),
851 $convert_tz ? $this->_getUserTZ($user) : self::$gmtTimezone, true);
855 * Converts DB time string to local time string
857 * TZ conversion depends on parameter
859 * @param string $date Time in DB format
860 * @param bool $meridiem
861 * @param bool $convert_tz Perform TZ conversion?
862 * @return string Time in user-defined format
864 public function to_display_time($date, $meridiem = true, $convert_tz = true)
866 if($convert_tz && strpos($date, ' ') === false) {
867 // we need TZ adjustment but have no date, assume today
868 $date = $this->expandTime($date, self::DB_DATETIME_FORMAT, self::$gmtTimezone);
870 return $this->_convert($date,
871 $convert_tz ? self::DB_DATETIME_FORMAT : self::DB_TIME_FORMAT, self::$gmtTimezone,
872 $this->get_time_format(), $convert_tz ? $this->_getUserTZ() : self::$gmtTimezone);
876 * Splits time in given format into components
878 * Components: h, m, s, a (am/pm) if format requires it
879 * If format has am/pm, hour is 12-based, otherwise 24-based
881 * @param string $date
882 * @param string $format
885 public function splitTime($date, $format)
887 if (! ($date instanceof DateTime)) {
888 $date = SugarDateTime::createFromFormat($format, $date);
890 $ampm = strpbrk($format, 'aA');
892 "h" => ($ampm == false) ? $date->format("H") : $date->format("h"),
893 'm' => $date->format("i"),
894 's' => $date->format("s")
897 $datearr['a'] = ($ampm{0} == 'a') ? $date->format("a") : $date->format("A");
903 * Converts DB date string to local date string
905 * TZ conversion depens on parameter
907 * @param string $date Date in DB format
908 * @param bool $convert_tz Perform TZ conversion?
909 * @return string Date in user-defined format
911 public function to_display_date($date, $convert_tz = true)
913 return $this->_convert($date,
914 self::DB_DATETIME_FORMAT, self::$gmtTimezone,
915 $this->get_date_format(), $convert_tz ? $this->_getUserTZ() : self::$gmtTimezone, true);
919 * Convert date from format to format
921 * No TZ conversion is performed!
923 * @param string $date
924 * @param string $fromformat Source format
925 * @param string $toformat Target format
926 * @return string Converted date
928 function to_display($date, $from, $to)
930 return $this->_convert($date, $from, self::$gmtTimezone, $to, self::$gmtTimezone);
934 * Get DB datetime format
937 public function get_db_date_time_format()
939 return self::DB_DATETIME_FORMAT;
946 public function get_db_date_format()
948 return self::DB_DATE_FORMAT;
955 public function get_db_time_format()
957 return self::DB_TIME_FORMAT;
961 * Convert date from local datetime to GMT-based DB datetime
963 * Includes TZ conversion.
965 * @param string $date
966 * @return string Datetime in DB format
968 public function to_db($date)
970 return $this->_convert($date,
971 $this->get_date_time_format(), $this->_getUserTZ(),
972 $this->get_db_date_time_format(), self::$gmtTimezone,
977 * Convert local datetime to DB date
979 * TZ conversion depends on parameter. If false, only format conversion is performed.
981 * @param string $date Local date
982 * @param bool $convert_tz Should time and TZ be taken into account?
983 * @return string Date in DB format
985 public function to_db_date($date, $convert_tz = true)
987 return $this->_convert($date,
988 $this->get_date_time_format(), $convert_tz ? $this->_getUserTZ() : self::$gmtTimezone,
989 self::DB_DATE_FORMAT, self::$gmtTimezone, true);
993 * Convert local datetime to DB time
995 * TZ conversion depends on parameter. If false, only format conversion is performed.
997 * @param string $date Local date
998 * @param bool $convert_tz Should time and TZ be taken into account?
999 * @return string Time in DB format
1001 public function to_db_time($date, $convert_tz = true)
1003 $format = $this->get_date_time_format();
1004 $tz = $convert_tz ? $this->_getUserTZ() : self::$gmtTimezone;
1005 if($convert_tz && strpos($date, ' ') === false) {
1006 // we need TZ adjustment but have short string, expand it to full one
1007 // FIXME: if the string is short, should we assume date or time?
1008 $date = $this->expandTime($date, $format, $tz);
1010 return $this->_convert($date,
1011 $convert_tz ? $format : $this->get_time_format(),
1013 self::DB_TIME_FORMAT, self::$gmtTimezone);
1017 * Takes a Date & Time value in local format and converts them to DB format
1020 * @param string $date
1021 * @param string $time
1022 * @return array Date & time in DB format
1024 public function to_db_date_time($date, $time)
1027 $phpdate = SugarDateTime::createFromFormat($this->get_date_time_format(),
1028 $this->merge_date_time($date, $time), self::$gmtTimezone);
1029 if ($phpdate == false) {
1030 return array('', '');
1032 return array($this->asDbDate($phpdate), $this->asDbTime($phpdate));
1033 } catch (Exception $e) {
1034 $GLOBALS['log']->error("Conversion of $date,$time failed");
1035 return array('', '');
1040 * Return current time in DB format
1043 public function nowDb()
1045 if(!$this->allow_cache) {
1046 $nowGMT = $this->getNow();
1048 $nowGMT = $this->now;
1050 return $this->asDb($nowGMT);
1054 * Return current date in DB format
1057 public function nowDbDate()
1059 if(!$this->allow_cache) {
1060 $nowGMT = $this->getNow();
1062 $nowGMT = $this->now;
1064 return $this->asDbDate($nowGMT, true);
1068 * Get 'now' DateTime object
1069 * @param bool $userTz return in user timezone?
1070 * @return SugarDateTime
1072 public function getNow($userTz = false)
1074 if(!$this->allow_cache) {
1075 return new SugarDateTime("now", $userTz?$this->_getUserTz():self::$gmtTimezone);
1077 // TODO: should we return clone?
1078 $now = clone $this->now;
1080 return $this->tzUser($now);
1087 * For testability - predictable time value
1088 * @param DateTime $now
1090 public function setNow($now)
1097 * Return current datetime in local format
1100 public function now()
1102 return $this->asUser($this->getNow());
1106 * Return current date in User format
1109 public function nowDate()
1111 return $this->asUserDate($this->getNow());
1115 * Get user format's time separator
1118 public function timeSeparator()
1120 if (empty($this->time_separator)) {
1121 $this->time_separator = $this->timeSeparatorFormat($this->get_time_format());
1123 return $this->time_separator;
1127 * Find out format's time separator
1128 * @param string $timeformat Time format
1130 public function timeSeparatorFormat($timeformat)
1132 $date = $this->_convert("00:11:22", self::DB_TIME_FORMAT, null, $timeformat, null);
1133 if (preg_match('/\d+(.+?)11/', $date, $matches)) {
1142 * Returns start and end of a certain local date in GMT
1143 * Example: for May 19 in PDT start would be 2010-05-19 07:00:00, end would be 2010-05-20 06:59:59
1144 * @param string|DateTime $date Date in any suitable format
1145 * @return array Start & end date in start, startdate, starttime, end, enddate, endtime
1147 public function getDayStartEndGMT($date, User $user = null)
1149 if ($date instanceof DateTime) {
1151 $min->setTimezone($this->_getUserTZ($user));
1153 $max->setTimezone($this->_getUserTZ($user));
1155 $min = new DateTime($date, $this->_getUserTZ($user));
1156 $max = new DateTime($date, $this->_getUserTZ($user));
1158 $min->setTime(0, 0);
1159 $max->setTime(23, 59, 59);
1161 $min->setTimezone(self::$gmtTimezone);
1162 $max->setTimezone(self::$gmtTimezone);
1164 $result['start'] = $this->asDb($min);
1165 $result['startdate'] = $this->asDbDate($min);
1166 $result['starttime'] = $this->asDbTime($min);
1167 $result['end'] = $this->asDb($max);
1168 $result['enddate'] = $this->asDbDate($max);
1169 $result['endtime'] = $this->asDbtime($max);
1175 * Expand date format by adding midnight to it
1176 * Note: date is assumed to be in target format already
1177 * @param string $date
1178 * @param string $format Target format
1181 public function expandDate($date, $format)
1183 $formats = $this->split_date_time($format);
1184 if(isset($formats[1])) {
1185 return $this->merge_date_time($date, $this->_get_midnight($formats[1]));
1191 * Expand time format by adding today to it
1192 * Note: time is assumed to be in target format already
1193 * @param string $date
1194 * @param string $format Target format
1195 * @param DateTimeZone $tz
1197 public function expandTime($date, $format, $tz)
1199 $formats = $this->split_date_time($format);
1200 if(isset($formats[1])) {
1201 $now = clone $this->getNow();
1202 $now->setTimezone($tz);
1203 return $this->merge_date_time($now->format($formats[0]), $date);
1209 * Get midnight (start of the day) in local time format
1211 * @return Time string
1213 function get_default_midnight()
1215 return $this->_get_midnight($this->get_time_format());
1219 * Get the name of the timezone for the user
1220 * @param User $user User, default - current user
1223 public static function userTimezone(User $user = null)
1225 $user = self::getInstance()->_getUser($user);
1229 $tz = self::getInstance()->_getUserTZ($user);
1231 return $tz->getName();
1237 * Guess the timezone for the current user
1240 public static function guessTimezone($userOffset = 0)
1242 if(!is_numeric($userOffset)) {
1245 $defaultZones= array(
1246 'America/Anchorage', 'America/Los_Angeles', 'America/Phoenix', 'America/Chicago',
1247 'America/New_York', 'America/Argentina/Buenos_Aires', 'America/Montevideo',
1248 'Europe/London', 'Europe/Amsterdam', 'Europe/Athens', 'Europe/Moscow',
1249 'Asia/Tbilisi', 'Asia/Omsk', 'Asia/Jakarta', 'Asia/Hong_Kong',
1250 'Asia/Tokyo', 'Pacific/Guam', 'Australia/Sydney', 'Australia/Perth',
1253 $now = new DateTime();
1254 $tzlist = timezone_identifiers_list();
1255 if($userOffset == 0) {
1256 $gmtOffset = date('Z');
1258 if(in_array($nowtz, $tzlist)) {
1259 array_unshift($defaultZones, $nowtz);
1261 $nowtz = timezone_name_from_abbr(date('T'), $gmtOffset, date('I'));
1262 if(in_array($nowtz, $tzlist)) {
1263 array_unshift($defaultZones, $nowtz);
1267 $gmtOffset = $userOffset * 60;
1269 foreach($defaultZones as $zoneName) {
1270 $tz = new DateTimeZone($zoneName);
1271 if($tz->getOffset($now) == $gmtOffset) {
1272 return $tz->getName();
1276 foreach($tzlist as $zoneName) {
1277 $tz = new DateTimeZone($zoneName);
1278 if($tz->getOffset($now) == $gmtOffset) {
1279 return $tz->getName();
1286 * Get the description of the user timezone for specific date
1288 * We need the date because it can be DST or non-DST
1289 * Note it's different from TZ name in tzName() that relates to current date
1290 * @param User $user User, default - current user
1293 public static function userTimezoneSuffix(DateTime $date, User $user = null)
1295 $user = self::getInstance()->_getUser($user);
1299 self::getInstance()->tzUser($date, $user);
1300 return $date->format('T(P)');
1304 * Get display name for a certain timezone
1305 * Note: it uses current date for GMT offset, so it may be not suitable for displaying generic dates
1306 * @param string|DateTimeZone $name TZ name
1309 public static function tzName($name)
1314 if($name instanceof DateTimeZone) {
1317 $tz = timezone_open($name);
1322 $now = new DateTime("now", $tz);
1323 $off = $now->getOffset();
1324 $translated = translate('timezone_dom','',$name);
1325 if(is_string($translated) && !empty($translated)) {
1326 $name = $translated;
1328 return sprintf("%s (GMT%+2d:%02d)%s", str_replace('_',' ', $name), $off/3600, (abs($off)/60)%60, "");//$now->format('I')==1?"(+DST)":"");
1332 public static function _sortTz($a, $b)
1334 if($a[0] == $b[0]) {
1335 return strcmp($a[1], $b[1]);
1337 return $a[0]<$b[0]?-1:1;
1342 * Get list of all timezones in the system
1345 public static function getTimezoneList()
1347 $now = new DateTime();
1348 $res_zones = $zones = array();
1349 foreach(timezone_identifiers_list() as $zoneName) {
1350 $tz = new DateTimeZone($zoneName);
1351 $zones[$zoneName] = array($tz->getOffset($now), self::tzName($zoneName));
1353 uasort($zones, array('TimeDate', '_sortTz'));
1354 foreach($zones as $name => $zonedef) {
1355 $res_zones[$name] = $zonedef[1];
1361 * Print timestamp in RFC2616 format:
1364 public static function httpTime($ts = null)
1369 return gmdate(self::RFC2616_FORMAT, $ts);
1373 * Create datetime object from calendar array
1374 * @param array $time
1375 * @return SugarDateTime
1377 public function fromTimeArray($time)
1379 if (! isset( $time) || count($time) == 0 )
1381 return $this->nowDb();
1383 elseif ( isset( $time['ts']))
1385 return $this->fromTimestamp($time['ts']);
1387 elseif ( isset( $time['date_str']))
1389 return $this->fromDb($time['date_str']);
1396 $now = $this->getNow(true);
1398 $month = $now->month;
1400 if (isset($time['sec']))
1402 $sec = $time['sec'];
1404 if (isset($time['min']))
1406 $min = $time['min'];
1408 if (isset($time['hour']))
1410 $hour = $time['hour'];
1412 if (isset($time['day']))
1414 $day = $time['day'];
1416 if (isset($time['month']))
1418 $month = $time['month'];
1420 if (isset($time['year']) && $time['year'] >= 1970)
1422 $year = $time['year'];
1424 return $now->setDate($year, $month, $day)->setTime($hour, $min, $sec)->setTimeZone(self::$gmtTimezone);
1430 * Returns the date portion of a datetime string
1432 * @param string $datetime
1435 public function getDatePart($datetime)
1437 list($date, $time) = $this->split_date_time($datetime);
1442 * Returns the time portion of a datetime string
1444 * @param string $datetime
1447 public function getTimePart($datetime)
1449 list($date, $time) = $this->split_date_time($datetime);
1454 * Returns the offset from user's timezone to GMT
1456 * @param DateTime $time When the offset is taken, default is now
1457 * @return int Offset in minutes
1459 public function getUserUTCOffset(User $user = null, DateTime $time = null)
1464 return $this->_getUserTZ($user)->getOffset($time) / 60;
1468 * Create regexp from datetime format
1469 * @param string $format
1470 * @return string Regular expression string
1472 public static function get_regular_expression($format)
1475 $regPositions = array();
1476 $ignoreNextChar = false;
1478 foreach (str_split($format) as $char) {
1479 if (! $ignoreNextChar && isset(self::$format_to_regexp[$char])) {
1480 $newFormat .= '(' . self::$format_to_regexp[$char] . ')';
1481 $regPositions[$char] = $count;
1484 $ignoreNextChar = false;
1485 $newFormat .= $char;
1488 if ($char == "\\") {
1489 $ignoreNextChar = true;
1493 return array('format' => $newFormat, 'positions' => $regPositions);
1496 // format - date expression ('' means now) for start and end of the range
1497 protected $date_expressions = array(
1498 'yesterday' => array("-1 day", "-1 day"),
1499 'today' => array("", ""),
1500 'tomorrow' => array("+1 day", "+1 day"),
1501 'last_7_days' => array("-6 days", ""),
1502 'next_7_days' => array("", "+6 days"),
1503 'last_30_days' => array("-29 days", ""),
1504 'next_30_days' => array("", "+29 days"),
1508 * Parse date template
1509 * @param string $template Date expression
1510 * @param bool $daystart Do we want start or end of the day?
1513 protected function parseFromTemplate($template, $daystart, User $user = null)
1515 $now = $this->tzUser($this->getNow(), $user);
1516 if(!empty($template[0])) {
1517 $now->modify($template[0]);
1520 return $now->get_day_begin();
1522 return $now->get_day_end();
1527 * Get month-long range mdiff months from now
1529 protected function diffMon($mdiff, User $user)
1531 $now = $this->tzUser($this->getNow(), $user);
1532 $now->setDate($now->year, $now->month+$mdiff, 1);
1533 $start = $now->get_day_begin();
1534 $end = $now->setDate($now->year, $now->month, $now->days_in_month)->setTime(23, 59, 59);
1535 return array($start, $end);
1539 * Get year-long range ydiff years from now
1541 protected function diffYear($ydiff, User $user)
1543 $now = $this->tzUser($this->getNow(), $user);
1544 $now->setDate($now->year+$ydiff, 1, 1);
1545 $start = $now->get_day_begin();
1546 $end = $now->setDate($now->year, 12, 31)->setTime(23, 59, 59);
1547 return array($start, $end);
1551 * Parse date range expression
1552 * Returns beginning and end of the range as a date
1553 * @param string $range
1557 public function parseDateRange($range, User $user = null)
1559 if(isset($this->date_expressions[$range])) {
1560 return array($this->parseFromTemplate($this->date_expressions[$range][0], true, $user),
1561 $this->parseFromTemplate($this->date_expressions[$range][1], false, $user)
1566 return $this->diffMon(1, $user);
1568 return $this->diffMon(-1, $user);
1570 return $this->diffMon(0, $user);
1572 return $this->diffYear(-1, $user);
1574 return $this->diffYear(0, $user);
1576 return $this->diffYear(1, $user);
1582 /********************* OLD functions, should not be used publicly anymore ****************/
1584 * Merge time without am/pm with am/pm string
1585 * @TODO find better way to do this!
1586 * @deprecated for public use
1587 * @param string $date
1588 * @param string $format User time format
1589 * @param string $mer
1592 function merge_time_meridiem($date, $format, $mer)
1594 $date = trim($date);
1598 $fakeMerFormat = str_replace(array('a', 'A'), array('@~@', '@~@'), $format);
1599 $noMerFormat = str_replace(array('a', 'A'), array('', ''), $format);
1600 $newDate = $this->swap_formats($date, $noMerFormat, $fakeMerFormat);
1601 return str_replace('@~@', $mer, $newDate);
1605 * @deprecated for public use
1606 * Convert date from one format to another
1608 * @param string $date
1609 * @param string $from
1613 public function swap_formats($date, $from, $to)
1615 return $this->_convert($date, $from, self::$gmtTimezone, $to, self::$gmtTimezone);
1619 * @deprecated for public use
1620 * handles offset values for Timezones and DST
1621 * @param $date string date/time formatted in user's selected format
1622 * @param $format string destination format value as passed to PHP's date() funtion
1623 * @param $to boolean
1624 * @param $user object user object from which Timezone and DST
1625 * @param $usetimezone string timezone name
1626 * values will be derived
1627 * @return string date formatted and adjusted for TZ and DST
1629 function handle_offset($date, $format, $to = true, $user = null, $usetimezone = null)
1631 $tz = empty($usetimezone)?$this->_getUserTZ($user):new DateTimeZone($usetimezone);
1632 $dateobj = new SugarDateTime($date, $to? self::$gmtTimezone : $tz);
1633 $dateobj->setTimezone($to ? $tz: self::$gmtTimezone);
1634 return $dateobj->format($format);
1635 // return $this->_convert($date, $format, $to ? self::$gmtTimezone : $tz, $format, $to ? $tz : self::$gmtTimezone);
1639 * @deprecated for public use
1640 * Get current GMT datetime in DB format
1643 function get_gmt_db_datetime()
1645 return $this->nowDb();
1649 * @deprecated for public use
1650 * Get current GMT date in DB format
1653 function get_gmt_db_date()
1655 return $this->nowDbDate();
1659 * @deprecated for public use
1660 * this method will take an input $date variable (expecting Y-m-d format)
1661 * and get the GMT equivalent - with an hour-level granularity :
1662 * return the max value of a given locale's
1663 * date+time in GMT metrics (i.e., if in PDT, "2005-01-01 23:59:59" would be
1664 * "2005-01-02 06:59:59" in GMT metrics)
1666 function handleOffsetMax($date)
1668 $min = new DateTime($date, $this->_getUserTZ());
1669 $min->setTime(0, 0);
1670 $max = new DateTime($date, $this->_getUserTZ());
1671 $max->setTime(23, 59, 59);
1673 $min->setTimezone(self::$gmtTimezone);
1674 $max->setTimezone(self::$gmtTimezone);
1676 $gmtDateTime['date'] = $this->asDbDate($max, false);
1677 $gmtDateTime['time'] = $this->asDbDate($max, false);
1678 $gmtDateTime['min'] = $this->asDb($min);
1679 $gmtDateTime['max'] = $this->asDb($max);
1681 return $gmtDateTime;
1685 * @deprecated for public use
1686 * this returns the adjustment for a user against the server time
1688 * @return integer number of minutes to adjust a time by to get the appropriate time for the user
1690 public function adjustmentForUserTimeZone()
1692 $tz = $this->_getUserTZ();
1693 $server_tz = new DateTimeZone(date_default_timezone_get());
1694 if ($tz && $server_tz) {
1695 return ($server_tz->getOffset($this->now) - $tz->getOffset($this->now)) / 60;
1701 * @deprecated for public use
1702 * assumes that olddatetime is in Y-m-d H:i:s format
1704 function convert_to_gmt_datetime($olddatetime)
1706 if (! empty($olddatetime)) {
1707 return date('Y-m-d H:i:s', strtotime($olddatetime) - date('Z'));
1712 * @deprecated for public use
1713 * get user timezone info
1715 public function getUserTimeZone(User $user = null)
1717 $tz = $this->_getUserTZ($user);
1718 return array("gmtOffset" => $tz->getOffset($this->now) / 60);
1722 * @deprecated for public use
1723 * get timezone start & end
1725 public function getDSTRange($year, $zone)
1727 $year_date = SugarDateTime::createFromFormat("Y", $year, self::$gmtTimezone);
1728 $year_end = clone $year_date;
1729 $year_end->setDate((int) $year, 12, 31);
1730 $year_end->setTime(23, 59, 59);
1731 $year_date->setDate((int) $year, 1, 1);
1732 $year_date->setTime(0, 0, 0);
1733 $tz = $this->_getUserTZ();
1734 $transitions = $tz->getTransitions($year_date->getTimestamp(), $year_end->getTimestamp());
1736 while (! $transitions[$idx]["isdst"])
1738 $startdate = new DateTime("@" . $transitions[$idx]["ts"], self::$gmtTimezone);
1739 while ($transitions[$idx]["isdst"])
1741 $enddate = new DateTime("@" . $transitions[$idx]["ts"], self::$gmtTimezone);
1742 return array("start" => $this->asDb($startdate), "end" => $this->asDb($enddate));
1745 /****************** GUI stuff that really shouldn't be here, will be moved ************/
1747 * Get Javascript variables setup for user date format validation
1748 * @deprecated moved to SugarView
1749 * @return string JS code
1751 function get_javascript_validation()
1753 return SugarView::getJavascriptValidation();
1758 * This method renders a <select> HTML form element based on the
1759 * user's time format preferences, with give date's value highlighted.
1761 * If user's prefs have no AM/PM string, returns empty string.
1763 * @todo There is hardcoded HTML in here that does not allow for localization
1764 * of the AM/PM am/pm Strings in this drop down menu. Also, perhaps
1765 * change to the substr_count function calls to strpos
1766 * TODO: Remove after full switch to fields
1768 * @param string $prefix Prefix for SELECT
1769 * @param string $date Date in display format
1770 * @param string $attrs Additional attributes for SELECT
1771 * @return string SELECT HTML
1773 function AMPMMenu($prefix, $date, $attrs = '')
1775 $tf = $this->get_time_format();
1776 $am = strpbrk($tf, 'aA');
1780 $selected = array("am" => "", "pm" => "", "AM" => "", "PM" => "");
1781 if (preg_match('/([ap]m)/i', $date, $match)) {
1782 $selected[$match[1]] = " selected";
1785 $menu = "<select name='" . $prefix . "meridiem' " . $attrs . ">";
1786 if ($am{0} == 'a') {
1787 $menu .= "<option value='am'{$selected["am"]}>am";
1788 $menu .= "<option value='pm'{$selected["pm"]}>pm";
1790 $menu .= "<option value='AM'{$selected["AM"]}>AM";
1791 $menu .= "<option value='PM'{$selected["PM"]}>PM";
1794 return $menu . '</select>';
1798 * TODO: Remove after full switch to fields
1800 function get_user_date_format()
1802 return str_replace(array('Y', 'm', 'd'), array('yyyy', 'mm', 'dd'), $this->get_date_format());
1806 * TODO: Remove after full switch to fields
1810 function get_user_time_format()
1812 global $sugar_config;
1813 $time_pref = $this->get_time_format();
1815 if (! empty($time_pref) && ! empty($sugar_config['time_formats'][$time_pref])) {
1816 return $sugar_config['time_formats'][$time_pref];
1819 return '23:00'; //default