Changeset 654


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

Add timezone support

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/js/Utilities.js

    r590 r654  
    6565        $(this).text($(this).text().replace(' at ', '@').replace(' dot ', '.'));
    6666        if (this.href) {
    67             this.href = this.href.replace(' at ', '@').replace(' dot ', '.');
     67            this.href = decodeURIComponent(this.href).replace(' at ', '@').replace(' dot ', '.');
    6868        }
    6969    });
     
    100100};
    101101
     102/*
     103* Set the user's timezone in a cookie (which is used in Codebase's $app->start() method).
     104---------------------------------------------------------------------
     105Strangecode.setTimezoneCookie()
     106---------------------------------------------------------------------
     107* @access   public
     108* @version  1.0
     109* @since    24 Jan 2019
     110*/
     111Strangecode.setTimezoneCookie = function () {
     112    try {
     113        // use Intl API when available and returning valid time zone
     114        var tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
     115    } catch (e) {
     116        // Intl unavailable, fall back to manual guessing.
     117        tz = (new Date().getTimezoneOffset()/-60);
     118    }
     119    if ('' != tz) {
     120        document.cookie = 'tz=' + tz + ';path=/;max-age=86400';
     121    }
     122}
    102123
    103124/*
  • 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.