From c91be8e0168b19e99ebe02790a37e5e02ef57f5f Mon Sep 17 00:00:00 2001 From: Holger Just Date: Fri, 10 Jul 2015 04:37:16 +0200 Subject: [PATCH] Use dates according to the user's timezone in queries --- app/models/query.rb | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/models/query.rb b/app/models/query.rb index a232258..d62da55 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -751,32 +751,32 @@ class Query < ActiveRecord::Base when "w" # = this week first_day_of_week = l(:general_first_day_of_week).to_i - day_of_week = Date.today.cwday + day_of_week = User.current.today.cwday days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6, is_custom_filter) when "lw" # = last week first_day_of_week = l(:general_first_day_of_week).to_i - day_of_week = Date.today.cwday + day_of_week = User.current.today.cwday days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) sql = relative_date_clause(db_table, db_field, - days_ago - 7, - days_ago - 1, is_custom_filter) when "l2w" # = last 2 weeks first_day_of_week = l(:general_first_day_of_week).to_i - day_of_week = Date.today.cwday + day_of_week = User.current.today.cwday days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) sql = relative_date_clause(db_table, db_field, - days_ago - 14, - days_ago - 1, is_custom_filter) when "m" # = this month - date = Date.today + date = User.current.today sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month, is_custom_filter) when "lm" # = last month - date = Date.today.prev_month + date = User.current.today.prev_month sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month, is_custom_filter) when "y" # = this year - date = Date.today + date = User.current.today sql = date_clause(db_table, db_field, date.beginning_of_year, date.end_of_year, is_custom_filter) when "~" sql = sql_contains("#{db_table}.#{db_field}", value.first) @@ -848,12 +848,20 @@ class Query < ActiveRecord::Base end end + def date_for_user_time_zone(y, m, d) + if tz = User.current.time_zone + tz.local y, m, d + else + Time.local y, m, d + end + end + # Returns a SQL clause for a date or datetime field. def date_clause(table, field, from, to, is_custom_filter) s = [] if from if from.is_a?(Date) - from = Time.local(from.year, from.month, from.day).yesterday.end_of_day + from = date_for_user_time_zone(from.year, from.month, from.day).yesterday.end_of_day else from = from - 1 # second end @@ -864,7 +872,7 @@ class Query < ActiveRecord::Base end if to if to.is_a?(Date) - to = Time.local(to.year, to.month, to.day).end_of_day + to = date_for_user_time_zone(to.year, to.month, to.day).end_of_day end if self.class.default_timezone == :utc to = to.utc @@ -876,7 +884,7 @@ class Query < ActiveRecord::Base # Returns a SQL clause for a date or datetime field using relative dates. def relative_date_clause(table, field, days_from, days_to, is_custom_filter) - date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil), is_custom_filter) + date_clause(table, field, (days_from ? User.current.today + days_from : nil), (days_to ? User.current.today + days_to : nil), is_custom_filter) end # Returns a Date or Time from the given filter value -- 2.4.4