Changeset 654 for trunk/lib


Ignore:
Timestamp:
Jan 24, 2019 7:13:52 PM (5 years ago)
Author:
anonymous
Message:

Add timezone support

Location:
trunk/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/App.inc.php

    r630 r654  
    368368            mb_language('en');
    369369            break;
     370        }
     371
     372        // If the 'timezone' parameter is not set, check for a tz cookie.
     373        if (!$this->getParam('timezone') && isset($_COOKIE['tz']) && '' != $_COOKIE['tz']) {
     374            if (preg_match('![A-Z]{3,}|\w+/\w+!', $_COOKIE['tz'])) {
     375                // Valid: tz cookie has a timezone name, like "UTC" or "America/Mexico_City".
     376                $this->setParam(array('timezone' => $_COOKIE['tz']));
     377            } else if (is_numeric($_COOKIE['tz']) && $_COOKIE['tz'] > -12 && $_COOKIE['tz'] < 14) { // https://en.wikipedia.org/wiki/List_of_UTC_time_offsets#UTC+14:00,_M%E2%80%A0
     378                // tz cookie has a timezone offset, like "-6" (assume UTC).
     379                $tz = timezone_name_from_abbr('', $_COOKIE['tz'] * 3600, 0);
     380                if ($tz) {
     381                    // Valid.
     382                    $this->setParam(array('timezone' => $tz));
     383                } else {
     384                    $this->logMsg(sprintf('Failed to convert UTC offset to timezone: %s', $_COOKIE['tz']), LOG_NOTICE, __FILE__, __LINE__);
     385                }
     386            } else {
     387                $this->logMsg(sprintf('Invalid timezone cookie value: %s', $_COOKIE['tz']), LOG_NOTICE, __FILE__, __LINE__);
     388            }
     389        }
     390        if ($this->getParam('timezone')) {
     391            // Set timezone of the user.
     392            if (date_default_timezone_set($this->getParam('timezone'))) {
     393                $this->logMsg(sprintf('Using timezone: %s', $this->getParam('timezone')), LOG_DEBUG, __FILE__, __LINE__);
     394            } else {
     395                // Failed: unset the timezone parameter so it isn't used to set the database timezone.
     396                $this->setParam(array('timezone' => null));
     397                $this->logMsg(sprintf('Failed to use timezone: %s', $this->getParam('timezone')), LOG_NOTICE, __FILE__, __LINE__);
     398            }
    370399        }
    371400
  • trunk/lib/DB.inc.php

    r630 r654  
    201201        }
    202202
     203        // Set timezone.
     204        $tz = date_default_timezone_get();
     205        // Check that PHP is configured with a valid timezone name, e.g., "UTC" or "America/Chicago".
     206        if ($app->getParam('timezone') && $app->getParam('timezone') == $tz && preg_match('![A-Z]{3,}|\w+/\w+!', $tz)) {
     207            $this->query(sprintf("SET SESSION time_zone = '%s'", $tz));
     208        }
     209
    203210        return true;
    204211    }
Note: See TracChangeset for help on using the changeset viewer.