improve timezone detection

This commit is contained in:
Robin Appelman 2019-02-06 14:56:11 +01:00
commit 10fadc4da9
3 changed files with 89 additions and 2 deletions

View file

@ -16,6 +16,8 @@ class System {
private $stdbuf; private $stdbuf;
private $date;
public static function getFD($num) { public static function getFD($num) {
$folders = array( $folders = array(
'/proc/self/fd', '/proc/self/fd',
@ -43,6 +45,13 @@ class System {
return $this->net; return $this->net;
} }
public function getDatePath() {
if (!$this->date) {
$this->date = trim(`which date`);
}
return $this->date;
}
public function hasStdBuf() { public function hasStdBuf() {
if (!$this->stdbuf) { if (!$this->stdbuf) {
$result = null; $result = null;

View file

@ -41,8 +41,14 @@ class TimeZoneProvider {
escapeshellarg($this->host) escapeshellarg($this->host)
); );
$this->timeZone = exec($command); $this->timeZone = exec($command);
} else { // fallback to server timezone }
$this->timeZone = date_default_timezone_get(); if (!$this->timeZone) { // fallback to server timezone
$date = $this->system->getDatePath();
if ($date) {
$this->timeZone = exec($date . " +%z");
} else {
$this->timeZone = date_default_timezone_get();
}
} }
} }
return $this->timeZone; return $this->timeZone;

View file

@ -0,0 +1,72 @@
<?php
/**
* @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace Icewind\SMB\Test;
use Icewind\SMB\System;
use Icewind\SMB\TimeZoneProvider;
class TimeZoneProviderTest extends TestCase {
/** @var System|\PHPUnit_Framework_MockObject_MockObject */
private $system;
/** @var TimeZoneProvider */
private $provider;
protected function setUp() {
parent::setUp();
$this->system = $this->getMock('Icewind\SMB\System');
}
private function getDummyCommand($output) {
return "echo '$output' || false";
}
public function testFQDN() {
$this->system->method('getNetPath')
->willReturn($this->getDummyCommand("+800"));
$this->system->method('getDatePath')
->willReturn($this->getDummyCommand("+700"));
$this->provider = new TimeZoneProvider('foo.bar.com', $this->system);
$this->assertEquals('+800', $this->provider->get());
}
public function testNoNet() {
$this->system->method('getNetPath')
->willReturn(false);
$this->system->method('getDatePath')
->willReturn($this->getDummyCommand("+700"));
$this->provider = new TimeZoneProvider('foo.bar.com', $this->system);
$this->assertEquals('+700', $this->provider->get());
}
public function testNoNetNoDate() {
$this->system->method('getNetPath')
->willReturn(false);
$this->system->method('getDatePath')
->willReturn(false);
$this->provider = new TimeZoneProvider('foo.bar.com', $this->system);
$this->assertEquals(date_default_timezone_get(), $this->provider->get());
}
}