Set the default timezone.

PHP 5.4 has removed the autodetection of the system timezone,
so it needs to be done manually.
UTC is the fallback in case autodetection fails.
199401
protected function setDefaultTimezone()
    {
        $timezone = 'UTC';
        if (is_link('/etc/localtime')) {
            // Mac OS X (and older Linuxes)
            // /etc/localtime is a symlink to the timezone in /usr/share/zoneinfo.
            $filename = readlink('/etc/localtime');
            if (strpos($filename, '/usr/share/zoneinfo/') === 0) {
                $timezone = substr($filename, 20);
            }
        } elseif (file_exists('/etc/timezone')) {
            // Ubuntu / Debian.
            $data = file_get_contents('/etc/timezone');
            if ($data) {
                $timezone = trim($data);
            }
        } elseif (file_exists('/etc/sysconfig/clock')) {
            // RHEL/CentOS
            $data = parse_ini_file('/etc/sysconfig/clock');
            if (!empty($data['ZONE'])) {
                $timezone = trim($data['ZONE']);
            }
        }
        date_default_timezone_set($timezone);
    }
142708
public static function setTimeZone()
    {
        $timeZone = Config::get('app.timezone');

        if (
            ($timeZone == '')
             || (strtoupper($timeZone) == 'UTC')
             || (empty($timeZone))
        )
            $timeZone = 'America/Toronto';

        date_default_timezone_set($timeZone);

        return;
    }
694219
public final void setUtc(final boolean utc) {
        if (!Value.DATE.equals(type)) {
            for (Date date: this) {
                ((DateTime) date).setUtc(utc);
            }
        }
        this.timeZone = null;
        this.utc = utc;
    }
1219729
public function set(string $timezone): bool {
    $auth = false;
    try {
      if (!empty($timezone)) {
        date_default_timezone_set($timezone);
        $auth = true;
      }
    } catch (Exception $ex) {
      throw $ex;
    }
    return $auth;
  }
799089
private function getTimezone()
    {
        if (!$this->timezone) {
            $this->timezone = $this->coreParametersHelper->getParameter(
                'default_timezone'
            );
        }

        return $this->timezone;
    }
858719
public final void setUtc(final boolean utc) {
		// reset the timezone associated with this instance..
		this.timezone = null;
		if (utc) {
			getFormat().setTimeZone(TimeZones.getUtcTimeZone());
		} else {
			resetTimeZone();
		}
		time = new Time(time, getFormat().getTimeZone(), utc);
	}
15824
public function setDefault($timeZone): void
    {
        if (is_string($timeZone) || $timeZone instanceof DateTimeZone) {
            $timeZone = $this->getTimeZone($timeZone);
        }
        $this->timeZoneClass->setDefault($timeZone);
        self::$defaultTz = $timeZone;
    }
185949
public function setTimezone($timezone = null)
	{
		$this->date->setTimezone($timezone === null ? self::$utc : is_string($timezone) ? ($timezone === 'UTC' ? self::$utc : new \DateTimeZone($timezone)) : ($timezone instanceof \DateTimeZone ? $timezone : $timezone->getTimezone()));
		return $this;
	}
908228
private function client_timezone()
    {
        // @TODO: remove this legacy timezone handling in the future
        $props = $this->fix_legacy_props(array('timezone' => $_SESSION['timezone']));

        if (!empty($props['timezone'])) {
            try {
                $tz = new DateTimeZone($props['timezone']);
                return $tz->getName();
            }
            catch (Exception $e) { /* gracefully ignore */ }
        }

        // fallback to server's timezone
        return date_default_timezone_get();
    }
366685
public static function setTimeZone($timezone) {
		if (self::_validateTimezone($timezone)) {
			self::$_timezone = $timezone;
			return TRUE;
		}
		return FALSE;
	}
173986
function setTimezone (timezone, relative) {
  debug('Date#setTimezone(%s, %s)', timezone, relative);

  // If `true` is passed in as the second argument, then the Date instance
  // will have it's timezone set, but it's current local values will remain
  // the same (i.e. the Date's internal time value will be changed)
  var ms, s, m, h, d, mo, y
  if (relative) {
    y  = this.getFullYear()
    mo = this.getMonth()
    d  = this.getDate()
    h  = this.getHours()
    m  = this.getMinutes()
    s  = this.getSeconds()
    ms = this.getMilliseconds()
  }

  // If the current process timezone doesn't match the desired timezone, then call
  // tzset() to change the current timezone of the process.
  var oldTz = exports.currentTimezone
    , tz = exports._currentZoneinfo;
  if (!tz || oldTz !== timezone) {
    debug('current timezone is not "%s", calling tzset()', timezone);
    tz = exports.tzset(timezone);
  }

  // Get the zoneinfo for this Date instance's time value
  var zoneInfo = exports.localtime(this.getTime() / 1000);

  // Change the timezone back if we changed it originally
  if (oldTz != timezone) {
    debug('setting timezone back to "%s"', oldTz);
    exports.tzset(oldTz);
  }
  oldTz = null;

  // If we got to here without throwing an Error, then
  // a valid timezone was requested, and we should have
  // a valid zoneInfo Object.
  this.getTimezone = function getTimezone() {
    return timezone;
  }

  // Returns the day of the month (1-31) for the specified date according to local time.
  this.getDate = function getDate() {
    return zoneInfo.dayOfMonth;
  }
  // Returns the day of the week (0-6) for the specified date according to local time.
  this.getDay = function getDay() {
    return zoneInfo.dayOfWeek;
  }
  // Deprecated. Returns the year (usually 2-3 digits) in the specified date according
  // to local time. Use `getFullYear()` instead.
  this.getYear = function getYear() {
    return zoneInfo.year;
  }
  // Returns the year (4 digits for 4-digit years) of the specified date according to local time.
  this.getFullYear = function getFullYear() {
    return zoneInfo.year + 1900;
  }
  // Returns the hour (0-23) in the specified date according to local time.
  this.getHours = function getHours() {
    return zoneInfo.hours;
  }
  // Returns the minutes (0-59) in the specified date according to local time.
  this.getMinutes = function getMinutes() {
    return zoneInfo.minutes;
  }
  // Returns the month (0-11) in the specified date according to local time.
  this.getMonth = function getMonth() {
    return zoneInfo.month;
  }
  // Returns the seconds (0-59) in the specified date according to local time.
  this.getSeconds = function getSeconds() {
    return zoneInfo.seconds;
  }
  // Returns the timezone offset from GMT the Date instance currently is in,
  // in minutes. Also, left of GMT is positive, right of GMT is negative.
  this.getTimezoneOffset = function getTimezoneOffset() {
    return -zoneInfo.gmtOffset / 60;
  }
  // NON-STANDARD: Returns the abbreviation (e.g. EST, EDT) for the specified time zone.
  this.getTimezoneAbbr = function getTimezoneAbbr() {
    return tz.tzname[zoneInfo.isDaylightSavings ? 1 : 0];
  }

  // Sets day, month and year at once
  this.setAllDateFields = function setAllDateFields(y,mo,d) {
    return this.setFullYear(y,mo,d);
  }
  // Sets the day of the month (from 1-31) in the current timezone
  this.setDate = function setDate(d) {
    zoneInfo.dayOfMonth = d;
    return mktime.call(this);
  }
  // Sets the year (four digits) in the current timezone
  this.setFullYear = function setFullYear(y,mo,d) {
    zoneInfo.year = y - 1900;
    if(arguments.length > 1)
      zoneInfo.month = mo;
    if(arguments.length > 2)
      zoneInfo.dayOfMonth = d;
    return mktime.call(this);
  }
  // Sets the hour (from 0-23) in the current timezone
  this.setHours = function setHours(h,m,s,ms) {
    zoneInfo.hours = h;
    if(arguments.length > 1)
      zoneInfo.minutes = m;
    if(arguments.length > 2)
      zoneInfo.seconds = s;
    if(arguments.length > 3) {
      mktime.call(this);
      var diff = ms - this.getMilliseconds();
      return this.setTime(this.getTime() + diff);
    } else
      return mktime.call(this);
  }
  // Sets the milliseconds (from 0-999) in the current timezone
  this.setMilliseconds = function setMilliseconds(ms) {
    var diff = ms - this.getMilliseconds();
    return this.setTime(this.getTime() + diff);
  }
  // Set the minutes (from 0-59) in the current timezone
  this.setMinutes = function setMinutes(m,s,ms) {
    zoneInfo.minutes = m;
    if(arguments.length > 1)
      zoneInfo.seconds = s;
    if(arguments.length > 2) {
      mktime.call(this);
      var diff = ms - this.getMilliseconds();
      return this.setTime(this.getTime() + diff);
    } else
      return mktime.call(this);
  }
  // Sets the month (from 0-11) in the current timezone
  this.setMonth = function setMonth(mo,d) {
    zoneInfo.month = mo;
    if(arguments.length > 1)
      zoneInfo.dayOfMonth = d;
    return mktime.call(this);
  }
  // Sets the seconds (from 0-59) in the current timezone
  this.setSeconds = function setSeconds(s,ms) {
    zoneInfo.seconds = s;
    if(arguments.length > 1) {
      mktime.call(this);
      var diff = ms - this.getMilliseconds();
      return this.setTime(this.getTime() + diff);
    } else
      return mktime.call(this);
  }
  // Sets a date and time by adding or subtracting a specified number of
  // milliseconds to/from midnight January 1, 1970.
  this.setTime = function setTime(v) {
    var rtn = _Date.prototype.setTime.call(this, v);
    // Since this function changes the internal UTC epoch date value, we need to
    // re-setup these timezone translation functions to reflect the new value
    reset.call(this);
    return rtn;
  }
  // Sets the day of the month, according to universal time (from 1-31)
  this.setUTCDate = function setUTCDate(d) {
    var rtn = _Date.prototype.setUTCDate.call(this, d);
    reset.call(this);
    return rtn;
  }
  // Sets the year, according to universal time (four digits)
  this.setUTCFullYear = function setUTCFullYear(y,mo,d) {
    var rtn;
    switch(arguments.length) {
      case 1:
        rtn = _Date.prototype.setUTCFullYear.call(this, y); break;
      case 2:
        rtn = _Date.prototype.setUTCFullYear.call(this, y,mo); break;
      case 3:
        rtn = _Date.prototype.setUTCFullYear.call(this, y,mo,d); break;
    }
    reset.call(this);
    return rtn;
  }
  // Sets the hour, according to universal time (from 0-23)
  this.setUTCHours = function setUTCHours(h,m,s,ms) {
    var rtn;
    switch(arguments.length) {
      case 1:
        rtn = _Date.prototype.setUTCHours.call(this, h); break;
      case 2:
        rtn = _Date.prototype.setUTCHours.call(this, h,m); break;
      case 3:
        rtn = _Date.prototype.setUTCHours.call(this, h,m,s); break;
      case 4:
        rtn = _Date.prototype.setUTCHours.call(this, h,m,s,ms); break;
    }
    reset.call(this);
    return rtn;
  }
  // Sets the milliseconds, according to universal time (from 0-999)
  this.setUTCMilliseconds = function setUTCMillseconds(ms) {
    var rtn = _Date.prototype.setUTCMilliseconds.call(this, ms);
    reset.call(this);
    return rtn;
  }
  // Set the minutes, according to universal time (from 0-59)
  this.setUTCMinutes = function setUTCMinutes(m,s,ms) {
    var rtn;
    switch(arguments.length) {
      case 1:
        rtn = _Date.prototype.setUTCMinutes.call(this, m); break;
      case 2:
        rtn = _Date.prototype.setUTCMinutes.call(this, m,s); break;
      case 3:
        rtn = _Date.prototype.setUTCMinutes.call(this, m,s,ms); break;
    }
    reset.call(this);
    return rtn;
  }
  // Sets the month, according to universal time (from 0-11)
  this.setUTCMonth = function setUTCMonth(mo,d) {
    var rtn;
    switch(arguments.length) {
      case 1:
        rtn = _Date.prototype.setUTCMonth.call(this, mo); break;
      case 2:
        rtn = _Date.prototype.setUTCMonth.call(this, mo,d); break;
    }
    reset.call(this);
    return rtn;
  }
  // Set the seconds, according to universal time (from 0-59)
  this.setUTCSeconds = function setUTCSeconds(s,ms) {
    var rtn;
    switch(arguments.length) {
      case 1:
        rtn = _Date.prototype.setUTCSeconds.call(this, s); break;
      case 2:
        rtn = _Date.prototype.setUTCSeconds.call(this, s,ms); break;
    }
    reset.call(this);
    return rtn;
  }

  this.toDateString = function toDateString() {
    return DAYS_OF_WEEK[this.getDay()].substring(0, 3) + ' ' + MONTHS[this.getMonth()].substring(0, 3) + ' ' + pad(this.getDate(), 2) + ' ' + this.getFullYear();
  }

  this.toTimeString = function toTimeString() {
    var offset = Math.abs(zoneInfo.gmtOffset / 60); // total minutes
    // split into HHMM:
    var hours = pad(Math.floor(offset / 60), 2);
    var minutes = pad(offset % 60, 2);
    return this.toLocaleTimeString() + ' GMT' + (zoneInfo.gmtOffset >= 0 ? '+' : '-') + hours + minutes
      + ' (' + tz.tzname[zoneInfo.isDaylightSavings ? 1 : 0] + ')';
  }

  this.toString = function toString() {
    return this.toDateString() + ' ' + this.toTimeString();
  }

  this.toLocaleDateString = function toLocaleDateString() {
    return DAYS_OF_WEEK[this.getDay()] + ', ' + MONTHS[this.getMonth()] + ' ' + pad(this.getDate(), 2) + ', ' + this.getFullYear();
  }

  this.toLocaleTimeString = function toLocaleTimeString() {
    return pad(this.getHours(), 2) + ':' + pad(this.getMinutes(), 2) + ':' + pad(this.getSeconds(), 2);
  }

  this.toLocaleString = this.toString;

  if (relative) {
    this.setAllDateFields(y,mo,d)
    this.setHours(h)
    this.setMinutes(m)
    this.setSeconds(s)
    this.setMilliseconds(ms)
    ms = s = m = h = d = mo = y = null
  }


  // Used internally by the 'set*' functions above...
  function reset () {
    this.setTimezone(this.getTimezone());
  }
  // 'mktime' calls 'reset' implicitly through 'setTime()'
  function mktime () {
    var oldTz = process.env.TZ;
    exports.tzset(this.getTimezone());
    zoneInfo.isDaylightSavings = -1; // Auto-detect the timezone
    var t = exports.mktime(zoneInfo);
    if (oldTz) {
      exports.tzset(oldTz);
      oldTz = null;
    }
    return this.setTime( (t * MILLIS_PER_SECOND) + this.getMilliseconds() );
  }

  return this;
}
1040141
public function get_timezone()
    {
        if ($tz = $this->get('timezone')) {
            try {
                $tz = new DateTimeZone($tz);
                return $tz->getOffset(new DateTime('now')) / 3600;
            }
            catch (Exception $e) {
            }
        }

        return 0;
    }
1020301
public function setValue($val)
    {

        // Fuzzy matching through strtotime() to support a wider range of times,
        // e.g. 11am. This means that validate() might not fire.
        // Note: Time formats are assumed to be less ambiguous than dates across locales.
        if ($this->getConfig('use_strtotime') && !empty($val)) {
            if ($parsedTimestamp = strtotime($val)) {
                $parsedObj = new Zend_Date($parsedTimestamp, Zend_Date::TIMESTAMP);
                $val = $parsedObj->get($this->getConfig('timeformat'));
                unset($parsedObj);
            }
        }

        if (empty($val)) {
            $this->value = null;
            $this->valueObj = null;
        }
        // load ISO time from database (usually through Form->loadDataForm())
        // Requires exact format to prevent false positives from locale specific times
        else if ($this->valueObj = $this->parseTime($val, $this->getConfig('datavalueformat'), null, true)) {
            $this->value = $this->valueObj->get($this->getConfig('timeformat'));
        }
        // Set in current locale (as string)
        else if ($this->valueObj = $this->parseTime($val, $this->getConfig('timeformat'), $this->locale)) {
            $this->value = $this->valueObj->get($this->getConfig('timeformat'));
        }
        // Fallback: Set incorrect value so validate() can pick it up
        elseif (is_string($val)) {
            $this->value = $val;
            $this->valueObj = null;
        } else {
            $this->value = null;
            $this->valueObj = null;
        }

        return $this;
    }
1231037
public static function setTimeZone($timezone)
    {
        if (self::_validateTimezone($timezone)) {
            self::$timezone = $timezone;
            return true;
        }
        return false;
    }
1019363
def enforce_timezone(self, value):
        
        field_timezone = getattr(self, 'timezone', self.default_timezone())

        if (field_timezone is not None) and not timezone.is_aware(value):
            return timezone.make_aware(value, field_timezone)
        elif (field_timezone is None) and timezone.is_aware(value):
            return timezone.make_naive(value, timezone.UTC())
        return value
1020153
@Nonnull
  public static ESuccess setDefaultDateTimeZoneID (@Nonnull @Nonempty final String sDateTimeZoneID)
  {
    ValueEnforcer.notEmpty (sDateTimeZoneID, "DateTimeZoneID");

    try
    {
      // Try to resolve ID -> throws exception if unknown
      final ZoneId aDefaultZoneId = ZoneId.of (sDateTimeZoneID);

      // getTimeZone falls back to GMT if unknown
      final TimeZone aDefaultTimeZone = TimeZone.getTimeZone (aDefaultZoneId);

      s_aDefaultZoneId = aDefaultZoneId;
      TimeZone.setDefault (aDefaultTimeZone);
      return ESuccess.SUCCESS;
    }
    catch (final DateTimeException ex)
    {
      // time zone ID is unknown
      LOGGER.warn ("Unsupported ZoneId '" + sDateTimeZoneID + "'", ex);
      return ESuccess.FAILURE;
    }
  }
504328
public boolean setTimezone(TimeZone zone) throws CommandExecutionException {
        if (zone == null) throw new CommandExecutionException(CommandExecutionException.Error.INVALID_PARAMETERS);
        JSONArray tz = new JSONArray();
        tz.put(zone.getID());
        return sendOk("set_timezone", tz);
    }