]> source.dussan.org Git - redmine.git/commitdiff
Groups are incorrect when grouping by date without user timezone set.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 20 Jun 2019 06:23:48 +0000 (06:23 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 20 Jun 2019 06:23:48 +0000 (06:23 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@18264 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/user.rb
lib/redmine/i18n.rb
test/functional/issues_controller_test.rb
test/unit/user_test.rb

index 933ac786901990cde58e84d5bfcd61c4527a29e3..096adc4a9dd6955414964a5f829474c1c1a05b00 100644 (file)
@@ -544,10 +544,14 @@ class User < Principal
 
   # Returns the day of +time+ according to user's time zone
   def time_to_date(time)
-    if time_zone.nil?
-      time.to_date
+    self.convert_time_to_user_timezone(time).to_date
+  end
+
+  def convert_time_to_user_timezone(time)
+    if self.time_zone
+      time.in_time_zone(self.time_zone)
     else
-      time.in_time_zone(time_zone).to_date
+      time.utc? ? time.localtime : time
     end
   end
 
index badf39b1a537580b688c25af07576dd3480b6c6b..7756b497519f7a3236c511ef93a6acbe8e3c3d4b 100644 (file)
@@ -79,8 +79,7 @@ module Redmine
       options = {}
       options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
       time = time.to_time if time.is_a?(String)
-      zone = user.time_zone
-      local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
+      local = user.convert_time_to_user_timezone(time)
       (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options)
     end
 
index 3b567220f8000909ff60b0d0ada5d134736c998f..746594d1f612c5c14e90d1867717080d22f89fda 100644 (file)
@@ -355,9 +355,12 @@ class IssuesControllerTest < Redmine::ControllerTest
     end
   end
 
-  def test_index_grouped_by_created_on
+  def test_index_grouped_by_created_on_if_time_zone_is_utc
     skip unless IssueQuery.new.groupable_columns.detect {|c| c.name == :created_on}
 
+    @request.session[:user_id] = 2
+    User.find(2).pref.update(time_zone: 'UTC')
+
     get :index, :params => {
         :set_filter => 1,
         :group_by => 'created_on'
@@ -369,6 +372,25 @@ class IssuesControllerTest < Redmine::ControllerTest
     end
   end
 
+  def test_index_grouped_by_created_on_if_time_zone_is_nil
+    skip unless IssueQuery.new.groupable_columns.detect {|c| c.name == :created_on}
+    current_user = User.find(2)
+    @request.session[:user_id] = current_user.id
+    current_user.pref.update(time_zone: nil)
+
+    get :index, :params => {
+        :set_filter => 1,
+        :group_by => 'created_on'
+      }
+    assert_response :success
+
+    # group_name depends on localtime
+    group_name = format_date(Issue.second.created_on.localtime)
+    assert_select 'tr.group span.name', :text => group_name do
+      assert_select '+ span.count', :text => '2'
+    end
+  end
+
   def test_index_grouped_by_created_on_as_pdf
     skip unless IssueQuery.new.groupable_columns.detect {|c| c.name == :created_on}
 
index 04e20e9c4a7c4c2e6b9b64ac9024e8e18bf7b957..e03528809f700d5641f7b4abeedb7ba1c41490f9 100644 (file)
@@ -586,7 +586,23 @@ class UserTest < ActiveSupport::TestCase
     assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
 
     preference.update_attribute :time_zone, ''
-    assert_equal '2012-05-15', User.find(1).time_to_date(time).to_s
+    assert_equal time.localtime.to_date.to_s, User.find(1).time_to_date(time).to_s
+  end
+
+  def test_convert_time_to_user_timezone_should_return_the_time_according_to_user_time_zone
+    preference = User.find(1).pref
+    time = Time.gm(2012, 05, 15, 23, 30).utc # 2012-05-15 23:30 UTC
+    time_not_utc = Time.new(2012, 05, 15, 23, 30)
+
+    preference.update_attribute :time_zone, 'Baku' # UTC+5
+    assert_equal '2012-05-16 04:30:00 +0500', User.find(1).convert_time_to_user_timezone(time).to_s
+
+    preference.update_attribute :time_zone, 'La Paz' # UTC-4
+    assert_equal '2012-05-15 19:30:00 -0400', User.find(1).convert_time_to_user_timezone(time).to_s
+
+    preference.update_attribute :time_zone, ''
+    assert_equal time.localtime.to_s, User.find(1).convert_time_to_user_timezone(time).to_s
+    assert_equal time_not_utc, User.find(1).convert_time_to_user_timezone(time_not_utc)
   end
 
   def test_fields_for_order_statement_should_return_fields_according_user_format_setting