Project

General

Profile

Patch #22320 » 0002-Fixes-Query-date_clause-timezone-handling.patch

Jens Krämer, 2016-03-26 09:22

View differences:

app/models/query.rb
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
......
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
test/unit/query_test.rb
c = QueryColumn.new('foo', :caption => lambda {'Foo'})
assert_equal 'Foo', c.caption
end
def test_date_clause_should_respect_user_time_zone_with_local_default
@query = IssueQuery.new(:name => '_')
# user is in Hawaii (-10)
User.current = users(:users_001)
User.current.pref.update_attribute :time_zone, 'Hawaii'
# assume timestamps are stored in server local time
local_zone = Time.zone
from = Date.parse '2016-03-20'
to = Date.parse '2016-03-22'
assert c = @query.send(:date_clause, 'table', 'field', from, to, false)
# the dates should have been interpreted in the user's time zone and
# converted to local time
# what we get exactly in the sql depends on the local time zone, therefore
# it's computed here.
f = User.current.time_zone.local(from.year, from.month, from.day).yesterday.end_of_day.in_time_zone(local_zone)
t = User.current.time_zone.local(to.year, to.month, to.day).end_of_day.in_time_zone(local_zone)
assert_equal "table.field > '#{Query.connection.quoted_date f}' AND table.field <= '#{Query.connection.quoted_date t}'", c
end
def test_date_clause_should_respect_user_time_zone_with_utc_default
@query = IssueQuery.new(:name => '_')
# user is in Hawaii (-10)
User.current = users(:users_001)
User.current.pref.update_attribute :time_zone, 'Hawaii'
# assume timestamps are stored as utc
ActiveRecord::Base.default_timezone = :utc
from = Date.parse '2016-03-20'
to = Date.parse '2016-03-22'
assert c = @query.send(:date_clause, 'table', 'field', from, to, false)
# the dates should have been interpreted in the user's time zone and
# converted to utc. March 20 in Hawaii begins at 10am UTC.
assert_equal "table.field > '2016-03-20 09:59:59.999999' AND table.field <= '2016-03-23 09:59:59.999999'", c
ensure
ActiveRecord::Base.default_timezone = :local # restore Redmine default
end
end
(2-2/3)