diff --git a/module/VuFind/src/VuFind/Date/Converter.php b/module/VuFind/src/VuFind/Date/Converter.php index 784da795f10066d40c67a232ae897f34a9bc3355..2d066c73d60252bd98f85d8589c83f952a028caf 100644 --- a/module/VuFind/src/VuFind/Date/Converter.php +++ b/module/VuFind/src/VuFind/Date/Converter.php @@ -27,7 +27,7 @@ * @link http://vufind.org/wiki/vufind2:developer_manual Wiki */ namespace VuFind\Date; -use DateTime, VuFind\Exception\Date as DateException; +use DateTime, DateTimeZone, VuFind\Exception\Date as DateException; /** * Date/time conversion functionality. @@ -58,7 +58,7 @@ class Converter /** * Time zone to use for conversions * - * @var string + * @var DateTimeZone */ protected $timezone; @@ -81,8 +81,9 @@ class Converter ? $config->Site->displayTimeFormat : "H:i"; // Set time zone - $this->timezone = isset($config->Site->timezone) + $zone = isset($config->Site->timezone) ? $config->Site->timezone : 'America/New_York'; + $this->timezone = new DateTimeZone($zone); } /** @@ -123,18 +124,20 @@ class Converter 'warning_count' => 0, 'error_count' => 0, 'errors' => array() ); try { - $date = new DateTime($dateString); + $date = new DateTime($dateString, $this->timezone); } catch (\Exception $e) { $getErrors['error_count']++; $getErrors['errors'][] = $e->getMessage(); } } else { - $date = DateTime::createFromFormat($inputFormat, $dateString); + $date = DateTime::createFromFormat( + $inputFormat, $dateString, $this->timezone + ); $getErrors = DateTime::getLastErrors(); } if (isset($date) && $date instanceof DateTime) { - $date->setTimeZone(new \DateTimeZone($this->timezone)); + $date->setTimeZone($this->timezone); } if ($getErrors['warning_count'] == 0 diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Date/ConverterTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Date/ConverterTest.php index 9d388cce61fe885d7b6d336d4c15f13b3d0aa0c9..d1a272e3b0d998f4ee404d488bb2c23c1a02bec4 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/Date/ConverterTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Date/ConverterTest.php @@ -49,6 +49,26 @@ class ConverterTest extends \VuFindTest\Unit\TestCase * @return void */ public function testDates() + { + // Get current default time zone + $real_zone = date_default_timezone_get(); + + // Try all the tests in different time zones to ensure consistency: + foreach (array('America/New_York', 'Europe/Helsinki') as $zone) { + date_default_timezone_set($zone); + $this->runTests(); + } + + // Restore original time zone + date_default_timezone_set($real_zone); + } + + /** + * Support method for testDates() + * + * @return void + */ + protected function runTests() { // Build an object to test with (using empty configuration to ensure default // settings):