[fix #2905] fix DATE_TRUNC usage

DATE_TRUNC is played on the db where the timestamp are stored in utc.
So if a user search for 12/12/2012 in Paris timezone and that we truncate it should do something similar to :

SELECT DATE_TRUNC('day', foo.timezone) from (SELECT timestamp '2012-12-12 00:00:00' AT TIME ZONE 'Europe/Paris') as foo;
=> 2012-12-11

To avoid such pb, I suggest avoiding DATE_TRUNC when exact results are needed
This commit is contained in:
simon lehericey 2018-10-26 09:02:14 +00:00
parent 3dad743d56
commit 3685c65fb7
3 changed files with 16 additions and 7 deletions

View file

@ -111,8 +111,12 @@ class ProcedurePresentation < ApplicationRecord
column = sanitized_column(filter)
case table
when 'self'
date = filter['value'].to_date rescue nil
dossiers.where("DATE_TRUNC('day', #{column}) = ?", date)
date = Time.zone.parse(filter['value'])
if date.present?
dossiers.where("#{column} BETWEEN ? AND ?", date, date + 1.day)
else
[]
end
when 'type_de_champ', 'type_de_champ_private'
relation = table == 'type_de_champ' ? :champs : :champs_private
dossiers

View file

@ -72,9 +72,9 @@ describe StatsController, type: :controller do
it do
expect(subject).to eq({
2.months.ago.beginning_of_month => 2,
1.month.ago.beginning_of_month => 4,
1.hour.ago.beginning_of_month => 5
Time.utc(2016, 8, 1) => 2,
Time.utc(2016, 9, 1) => 4,
Time.utc(2016, 10, 1) => 5
})
end
end
@ -86,8 +86,8 @@ describe StatsController, type: :controller do
it do
expect(subject).to eq({
2.months.ago.beginning_of_month => 2,
1.month.ago.beginning_of_month => 4
Time.utc(2016, 8, 1) => 2,
Time.utc(2016, 9, 1) => 4
})
end
end

View file

@ -404,6 +404,11 @@ describe ProcedurePresentation do
it { is_expected.to contain_exactly(kept_dossier.id) }
end
context 'for a malformed date' do
let(:filter) { [{ 'table' => 'self', 'column' => 'updated_at', 'value' => 'malformed date' }] }
it { is_expected.to match([]) }
end
end
context 'for type_de_champ table' do