Skip to content
Snippets Groups Projects
Commit 355c23ac authored by Ere Maijala's avatar Ere Maijala Committed by Demian Katz
Browse files

Fixed date converter to use the same timezone for conversion and display.

parent 3168a5fa
No related merge requests found
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
* @link http://vufind.org/wiki/vufind2:developer_manual Wiki * @link http://vufind.org/wiki/vufind2:developer_manual Wiki
*/ */
namespace VuFind\Date; namespace VuFind\Date;
use DateTime, VuFind\Exception\Date as DateException; use DateTime, DateTimeZone, VuFind\Exception\Date as DateException;
/** /**
* Date/time conversion functionality. * Date/time conversion functionality.
...@@ -58,7 +58,7 @@ class Converter ...@@ -58,7 +58,7 @@ class Converter
/** /**
* Time zone to use for conversions * Time zone to use for conversions
* *
* @var string * @var DateTimeZone
*/ */
protected $timezone; protected $timezone;
...@@ -81,8 +81,9 @@ class Converter ...@@ -81,8 +81,9 @@ class Converter
? $config->Site->displayTimeFormat : "H:i"; ? $config->Site->displayTimeFormat : "H:i";
// Set time zone // Set time zone
$this->timezone = isset($config->Site->timezone) $zone = isset($config->Site->timezone)
? $config->Site->timezone : 'America/New_York'; ? $config->Site->timezone : 'America/New_York';
$this->timezone = new DateTimeZone($zone);
} }
/** /**
...@@ -123,18 +124,20 @@ class Converter ...@@ -123,18 +124,20 @@ class Converter
'warning_count' => 0, 'error_count' => 0, 'errors' => array() 'warning_count' => 0, 'error_count' => 0, 'errors' => array()
); );
try { try {
$date = new DateTime($dateString); $date = new DateTime($dateString, $this->timezone);
} catch (\Exception $e) { } catch (\Exception $e) {
$getErrors['error_count']++; $getErrors['error_count']++;
$getErrors['errors'][] = $e->getMessage(); $getErrors['errors'][] = $e->getMessage();
} }
} else { } else {
$date = DateTime::createFromFormat($inputFormat, $dateString); $date = DateTime::createFromFormat(
$inputFormat, $dateString, $this->timezone
);
$getErrors = DateTime::getLastErrors(); $getErrors = DateTime::getLastErrors();
} }
if (isset($date) && $date instanceof DateTime) { if (isset($date) && $date instanceof DateTime) {
$date->setTimeZone(new \DateTimeZone($this->timezone)); $date->setTimeZone($this->timezone);
} }
if ($getErrors['warning_count'] == 0 if ($getErrors['warning_count'] == 0
......
...@@ -49,6 +49,26 @@ class ConverterTest extends \VuFindTest\Unit\TestCase ...@@ -49,6 +49,26 @@ class ConverterTest extends \VuFindTest\Unit\TestCase
* @return void * @return void
*/ */
public function testDates() 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 // Build an object to test with (using empty configuration to ensure default
// settings): // settings):
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment