From 48ec57abb9e224d72d51b8492a7d0652e3b137b7 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 4 Apr 2014 00:27:41 -0700 Subject: [PATCH] CRM-14423 - CRM_Utils_System - Simplify time-zone lookups --- CRM/Utils/System/Base.php | 2 +- CRM/Utils/System/Drupal.php | 22 ++++++---------------- CRM/Utils/System/Drupal6.php | 17 ++++++----------- CRM/Utils/System/UnitTests.php | 8 ++++++++ 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index 9cccc92be5..0a4d508e4b 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -215,7 +215,7 @@ abstract class CRM_Utils_System_Base { * @return string Timezone e.g. 'America/Los_Angeles' */ function getTimeZoneString() { - return NULL; + return date_default_timezone_get(); } /** diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php index 29af79e2c7..c9c9bf5675 100644 --- a/CRM/Utils/System/Drupal.php +++ b/CRM/Utils/System/Drupal.php @@ -960,30 +960,20 @@ AND u.status = 1 } /** - * Get timezone from Drupal - * @return boolean|string + * Over-ridable function to get timezone as a string eg. + * @return string Timezone e.g. 'America/Los_Angeles' */ - function getTimeZoneOffset(){ + function getTimeZoneString() { global $user; if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) { $timezone = $user->timezone; } else { $timezone = variable_get('date_default_timezone', null); } - $tzObj = new DateTimeZone($timezone); - $dateTime = new DateTime("now", $tzObj); - $tz = $tzObj->getOffset($dateTime); - - if(empty($tz)){ - return false; - } - - $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, abs(($tz/60)%60)); - - if($timeZoneOffset > 0){ - $timeZoneOffset = '+' . $timeZoneOffset; + if (!$timezone) { + $timezone = parent::getTimeZoneString(); } - return $timeZoneOffset; + return $timezone; } /** * Reset any system caches that may be required for proper CiviCRM diff --git a/CRM/Utils/System/Drupal6.php b/CRM/Utils/System/Drupal6.php index 363ec0c931..2345de532b 100644 --- a/CRM/Utils/System/Drupal6.php +++ b/CRM/Utils/System/Drupal6.php @@ -938,25 +938,20 @@ class CRM_Utils_System_Drupal6 extends CRM_Utils_System_DrupalBase { } /** - * Get timezone from Drupal - * @return boolean|string + * Over-ridable function to get timezone as a string eg. + * @return string Timezone e.g. 'America/Los_Angeles' */ - function getTimeZoneOffset(){ + function getTimeZoneString() { global $user; if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) { $timezone = $user->timezone; } else { $timezone = variable_get('date_default_timezone', null); } - if(empty($timezone)){ - return false; + if (!$timezone) { + $timezone = parent::getTimeZoneString(); } - $hour = $user->timezone / 3600; - $timeZoneOffset = sprintf("%02d:%02d", $timezone / 3600, abs(($timeZoneOffset/60)%60)); - if($timeZoneOffset > 0){ - $timeZoneOffset = '+' . $timeZoneOffset; - } - return $timeZoneOffset; + return $timezone; } diff --git a/CRM/Utils/System/UnitTests.php b/CRM/Utils/System/UnitTests.php index c45dec9427..b7afc8b2c1 100644 --- a/CRM/Utils/System/UnitTests.php +++ b/CRM/Utils/System/UnitTests.php @@ -174,5 +174,13 @@ class CRM_Utils_System_UnitTests extends CRM_Utils_System_Drupal { throw new Exception("Method not implemented: getLoginURL"); } + /** + * Over-ridable function to get timezone as a string eg. + * @return string Timezone e.g. 'America/Los_Angeles' + */ + function getTimeZoneString() { + // This class extends Drupal, but we don't want Drupal's behavior; reproduce CRM_Utils_System_Base::getTimeZoneString + return date_default_timezone_get(); + } } -- 2.25.1