CRM-13885 - Set timezone for WP on Cron
authorDonald A. Lobo <lobo@civicrm.org>
Tue, 3 Dec 2013 17:01:03 +0000 (09:01 -0800)
committerDonald A. Lobo <lobo@civicrm.org>
Tue, 3 Dec 2013 17:01:03 +0000 (09:01 -0800)
http://issues.civicrm.org/jira/browse/CRM-13885

CRM/Utils/System/Base.php
CRM/Utils/System/WordPress.php

index e9acb7df1a1e9e58a8b0969715213585f344cb13..e9767fc162db54a7b2132a91eb73d3c149abee3b 100644 (file)
@@ -175,11 +175,41 @@ abstract class CRM_Utils_System_Base {
     }
   }
 
+
   /**
    * Get timezone from CMS
    * @return boolean|string
    */
+  /**
+   * Get timezone from Drupal
+   * @return boolean|string
+   */
   function getTimeZoneOffset(){
+    $timezone = $this->getTimeZoneString();
+    if($timezone){
+      $tzObj = new DateTimeZone($timezone);
+      $dateTime = new DateTime("now", $tzObj);
+      $tz = $tzObj->getOffset($dateTime);
+
+      if(empty($tz)){
+        return false;
+      }
+
+      $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, ($tz/60)%60 );
+
+      if($timeZoneOffset > 0){
+        $timeZoneOffset = '+' . $timeZoneOffset;
+      }
+      return $timeZoneOffset;
+    }
+  }
+
+  /**
+   * Over-ridable function to get timezone as a string eg.
+   * @return string Timezone e.g. 'America/Los_Angeles'
+   */
+  function getTimeZoneString() {
+    return NULL;
   }
 }
 
index 42649a736d038fa008e3e9fdc450a767d871cea2..389c2c06927d8f84687b5367bc196fddf1ec3e9f 100644 (file)
@@ -433,6 +433,11 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
     }
 
     require_once ($cmsRootPath . DIRECTORY_SEPARATOR . 'wp-load.php');
+    $wpUserTimezone = get_option('timezone_string');
+    if ($wpUserTimezone) {
+      date_default_timezone_set($wpUserTimezone);
+      CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
+    }
     return true;
   }
 
@@ -622,5 +627,13 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
       return 'Unknown';
     }
   }
+
+  /**
+   * get timezone as a string
+   * @return string Timezone e.g. 'America/Los_Angeles'
+   */
+  function getTimeZoneString() {
+    return get_option('timezone_string');
+  }
 }