CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Time zone issues
Pages: [1]

Author Topic: Time zone issues  (Read 6925 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Time zone issues
February 14, 2012, 06:46:21 pm
We have been having recent concerns about civimails going out too late due to the timezone of the user who created them & the timezone on the server being different.

In order to figure out how to deal with that I used a secret & highly-effective problem-solving techique ... I asked Tim who fished out some links about CiviCRM timestamp fields & shared a few thoughts.

Playing around with this over the last couple of hours it seems surprisingly simple. Mysql has a field type called 'timestamp' - data in this field is stored in UTC but when you run a query against it is converted using the current mysql time_zone variable (hence at first I thought nothing had changed when I converted the field :-).

I converted the datetime fields in the mailing & mailing job queries & saw no discernable difference in the data or CiviCRM behaviour. MySQL was seamlessly converting the data using the server settings.

However, by changing the time_zone for the connection I could see the date in a different time when I did a select. I added the small bit of (not very graceful) code to civicrm.module to detect the drupal timezone & set it in the civicrm connection & it seemlessly 'handled' the timezone issue.

I also tested sending mail from the cronjob  - at first I thought I had to set the timezone in the script scenario - but I realised that as long as it comes up with some form of valid timezone that's good enough - because it's stored in UTC.

So, in summary - the patch below solves the problem for fields converted to timestamp & has no impact if no fields are converted. (ie. I'm making the case for it going in 4.1 so I don't have to manage yet another 4.2 patch for the 4.1 cycle.)

Another happy thing is that if you make the column type change 'early' it doesn't matter as it won't kill your upgrade script in the way that adding an index or a table early does.

Code: [Select]
Index: drupal/civicrm.module
===================================================================
--- drupal/civicrm.module (revision 37365)
+++ drupal/civicrm.module (working copy)
@@ -270,7 +270,16 @@
 
         // initialize the system by creating a config object
         $config = CRM_Core_Config::singleton();
-     
+
+
+      global $user;
+      $hour = $user->timezone / 3600;
+      $timeZoneOffset = sprintf("%02s:00",$hour);
+      if($timeZoneOffset > 0){
+        $timeZoneOffset .= '+';
+      }
+       $sql = "SET time_zone = '$timeZoneOffset'";
+       CRM_Core_DAO::executequery($sql);   
         // Add module-specific header elements
         drupal_set_html_head(civicrm_html_head());
         require_once 'CRM/Core/BAO/Preferences.php';



Code: [Select]
ALTER TABLE `civicrm_mailing`
CHANGE COLUMN `created_date` `created_date` TIMESTAMP NULL DEFAULT NULL COMMENT 'Date and time this mailing was created.' AFTER `created_id`,
CHANGE COLUMN `scheduled_date` `scheduled_date` TIMESTAMP NULL DEFAULT NULL COMMENT 'Date and time this mailing was scheduled.' AFTER `is_archived`,
CHANGE COLUMN `approval_date` `approval_date` TIMESTAMP NULL DEFAULT NULL COMMENT 'Date and time this mailing was approved.' AFTER `approver_id`;

ALTER TABLE `civicrm_mailing_job`
CHANGE COLUMN `scheduled_date` `scheduled_date` TIMESTAMP NULL DEFAULT NULL COMMENT 'date on which this job was scheduled.' AFTER `mailing_id`,
CHANGE COLUMN `start_date` `start_date` TIMESTAMP NULL DEFAULT NULL COMMENT 'date on which this job was started.' AFTER `scheduled_date`,
CHANGE COLUMN `end_date` `end_date` TIMESTAMP NULL DEFAULT NULL COMMENT 'date on which this job ended.' AFTER `start_date`;
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Time zone issues
February 14, 2012, 07:13:36 pm
revised insert

Code: [Select]
        global $user;
        if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
           $timezone = $user->timezone;
        } else {
           $timezone = variable_get('date_default_timezone', 0);
        }
        if(isset($timezone)){
          $hour = $user->timezone / 3600;
          $timeZoneOffset = sprintf("%02d:%02d", $timezone / 3600, ($timezone/60)%60 );
          if($timeZoneOffset > 0){
            $timeZoneOffset .= '+';
          }
          $sql = "SET time_zone = '$timeZoneOffset'";
          CRM_Core_DAO::executequery($sql); 
        }
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Time zone issues
February 14, 2012, 11:17:14 pm
Thanks for that!!!  ;D
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Time zone issues
February 14, 2012, 11:30:37 pm
This way if you sent an email to the API team to meet & drink warm beer we won't have half the team being 12 hours late :-)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Time zone issues
February 14, 2012, 11:49:46 pm
I thought we implemented the sopa filter to remove all jokes about warm beers at the last sprint?
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Time zone issues
February 15, 2012, 12:00:41 am
Guess it needs debugging
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Time zone issues
December 02, 2012, 01:26:19 pm
In response to a PM I received today...

We are running timezone-fields on CiviMail and civievent using the technique described here. We are using the technique on versions from 3.4.8 to 4.2 on d6 & d7 and have been doing so for about 18 months without any obvious problems. The process for implementing it is simply to add a bit of code for civicrm.module and then convert fields as desired to timestamp fields - it then respects the drupal timezone settings. The storage within the database is in UTC.

http://issues.civicrm.org/jira/browse/CRM-9683

In order to get it into core the main issues are determining appropriate code for WordPress & Joomla - having said that defaulting to server time would do no harm as the storage is in UTC, testing, figuring out how to convert fields, testing the conversion and removing any php level timezone handling. I also think that you need to switch a bunch of functions to use mysql NOW() rather than php NOW(). It may be easier to only convert certain fields in the first instance.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

sggreener

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 0
    • The SpatialDB Advisor
  • CiviCRM version: 4.6.15
  • CMS version: Joomla 5.5.1
  • MySQL version: 5.6.29
  • PHP version: 5.3.8
Re: Time zone issues
December 02, 2012, 06:37:59 pm
Eileen,

I will take a look at this wrt CiviMail. Main issue is users complaining about default dates for new contributions that default to the server time and not local time.

It would be nice to see a fix for this.

regards
Simon

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Time zone issues
December 02, 2012, 06:55:12 pm
The fix is the same on any datetime field - alter the field to being a timestamp field and use civicrm.module to set your timezone so you see stuff in the right timezone.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

sougatab

  • I’m new here
  • *
  • Posts: 10
  • Karma: 0
  • CiviCRM version: 4.3
  • CMS version: Joomla 2.5
  • MySQL version: 5+
  • PHP version: 5.3
Re: Time zone issues
June 15, 2013, 02:53:11 pm
We are also facing a similar timezone issue. Users cannot schedule mailings at the right local time as the server timezone is different. How can we set the timezone for CiviCRM? Is it possible to allow CiviCRM to inherit timezone settings from Joomla?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Time zone issues
June 16, 2013, 02:15:09 pm
If you look at the above ticket I have been setting the timezone in drupal specific code - it's probably similar to do something like this in Joomla! (http://forum.joomla.org/viewtopic.php?p=2210541#p2347762)

Then you also need to convert the mysql fields from datetime to timestamp. I found this easy & it's pretty safe because if an upgrade script later tries to do it it doesn't fail if already done.
 
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Dennis Gray

  • Ask me questions
  • ****
  • Posts: 472
  • Karma: 1
  • CiviCRM version: Various. See post.
  • CMS version: Drupal, Wordpress and Joomla. See post.
  • MySQL version: TBA
  • PHP version: TBA
Re: Time zone issues
July 05, 2013, 08:04:12 pm
I just installed CiviCRM 4.3.4 with WordPress. I still have issue with the timezone difference between the user that sends email in Vietnam and the server's timezone, which is set to UTC. I even tried setting the server's timezone to Vietnam time but that didn't help so my work around is to set the WordPress time to UTC.

I'd appreciate any help I can get on this

KilakwaBT

  • I post occasionally
  • **
  • Posts: 101
  • Karma: 11
  • CiviCRM version: 4.5.5
  • CMS version: WordPress 4.1
  • MySQL version: 5.1.68
  • PHP version: 5.3
Re: Time zone issues
July 06, 2013, 01:04:46 pm
Take a look at this thread: http://forum.civicrm.org/index.php/topic,25625.msg123722.html#msg12372

Hope it helps.

BT

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Time zone issues

This forum was archived on 2017-11-26.