From 355c23acb1b62b2fbb085f4695aa97863583ac96 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Wed, 13 Aug 2014 16:05:50 +0300
Subject: [PATCH] Fixed date converter to use the same timezone for conversion
 and display.

---
 module/VuFind/src/VuFind/Date/Converter.php   | 15 ++++++++------
 .../src/VuFindTest/Date/ConverterTest.php     | 20 +++++++++++++++++++
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/module/VuFind/src/VuFind/Date/Converter.php b/module/VuFind/src/VuFind/Date/Converter.php
index 784da795f10..2d066c73d60 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 9d388cce61f..d1a272e3b0d 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):
-- 
GitLab