Author Topic: views 2 event dates returned based on server time zone  (Read 1603 times)

Offline jalama

  • I post frequently
  • ***
  • Posts: 176
  • Karma: 22
    • Rooty Hollow LLC
  • CiviCRM version: 3.3.5
  • CMS version: Drupal 6 and 7
  • MySQL version: 5.1
  • PHP version: 5.2.5 and 5.3
views 2 event dates returned based on server time zone
« on: April 24, 2009, 06:22:28 pm »
I tried to put up a patch for this, but my brain is hurting form all the date/time formatting issues, here's to hoping some else will know how to do this.  Though I think this feature in jira will goa long way towards fixing this?  http://issues.civicrm.org/jira/browse/CRM-2709

When the datetime handler for views submits a date to the views date handler (views_handler_field_date) views is expecting it to be the timestamp for the server's time zone.  If the the date is entered for a different time zone than that of the server then views presents the date according to the users time zone but assuming the events takes place in the server timezone, which isn't always true.

i.e. An event is entered in as 10 am EST for a site sitting on a server in PST.  Views, strtotime(), converts that time to 10am PST and then returns it to an EST user as 1pm because it thinks the time is 10am PST.  The issue is strtotime() function calculates the timestamp based on the server's time zone.  Drupal saves the user's, and a default, timezone.  The only thing I couldn't pull off what figuring out it the users timezone was affected by Day Light Savings.

adding the $timezone variable in the following code hacked copy of handler, using code from Drupal's format_date, to the $value get about 90% of the way there.

you can get around this by setting the default time zone to that of the server & disabling the user defined time zones, which of course sucks for the users but eliminates confusion.

Code: [Select]
class civicrm_handler_field_datetime extends views_handler_field_date {

    /*
     * Convert the DATETIME from the database into unixtime then allow
     * views_handler_field_date to render as usual.
     */
    function render($values) {
        $value = $values->{$this->field_alias};
        if($value) {

        global $user;
        if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
          $timezone = $user->timezone;
        }
        else {
          $timezone = variable_get('date_default_timezone', 0);
        }
            $value = strtotime($value);
            $value += $timezone;
            if($value) {
                $values->{$this->field_alias} = $value;
            }
        }

        return parent::render($values);
    }
}

Jim

Offline Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 14731
  • Karma: 440
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: views 2 event dates returned based on server time zone
« Reply #1 on: April 25, 2009, 11:33:03 am »

hey jim:

this might be a good code hack-a-thon for developer camp. are u interested in working on it?

lobo
Found this reply helpful? Contribute NOW and help improve CiviCRM with the Make it Happen! initiative.

Offline jalama

  • I post frequently
  • ***
  • Posts: 176
  • Karma: 22
    • Rooty Hollow LLC
  • CiviCRM version: 3.3.5
  • CMS version: Drupal 6 and 7
  • MySQL version: 5.1
  • PHP version: 5.2.5 and 5.3
Re: views 2 event dates returned based on server time zone
« Reply #2 on: April 25, 2009, 08:20:53 pm »
Sounds good to me

Here's my latest.  The idea is get the timestamp back to UTC and then to the site's default timezone.  It's not perfect but a huge improvement over the above hack job.

Code: [Select]
class civicrm_handler_field_datetime extends views_handler_field_date {

    /*
     * Convert the DATETIME from the database into unixtime then allow
     * views_handler_field_date to render as usual.
     */
    function render($values) {
        // get default time zone from Drupal
        $timezone = variable_get('date_default_timezone', 0);
        $value = $values->{$this->field_alias};
        $date = new DateTime($value);
        $gmt = $date->getOffset();  //gives me the offset to GMT
        if($value) {
            $value = strtotime($value);
             //set the timestamp to GMT
            $value = $value + $gmt;
            //set the timestamp to site default
            $value = $value - $timezone;
            if($value) {
                $values->{$this->field_alias} = $value;
            }
        }

        return parent::render($values);
    }
}

Offline jalama

  • I post frequently
  • ***
  • Posts: 176
  • Karma: 22
    • Rooty Hollow LLC
  • CiviCRM version: 3.3.5
  • CMS version: Drupal 6 and 7
  • MySQL version: 5.1
  • PHP version: 5.2.5 and 5.3
Re: views 2 event dates returned based on server time zone
« Reply #3 on: April 29, 2009, 03:28:30 pm »