From 633cc1283babd78971cb89d803e999a8b53e0a09 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 28 Mar 2018 00:17:40 +0200 Subject: [PATCH] Fix Date & DateTime parsings to use default timezone Date.new(...) does not take into account the current timezone, while other parts of the application do. By default always parsing any date with the default timezone and converting the resulting Time to Date would prevent this kind of issues DateTime.parse(...).in_time_zone gives an unexpected result, as the DateTime.parse(...) will create a DateTime with +0000 time zone and the `in_time_zone` will modify the DateTime to adjust to the default zone. Maybe its better explained with an example, using 'Lima' as timezone: DateTime.parse("2015-01-01") > Thu, 01 Jan 2015 00:00:00 +0000 DateTime.parse("2015-01-01").in_time_zone > Wed, 31 Dec 2014 19:00:00 -05 -05:00 And that's not the desired date but the previous day! --- app/models/officing/residence.rb | 2 +- app/models/verification/residence.rb | 2 +- lib/census_api.rb | 2 +- .../admin/api/stats_controller_spec.rb | 18 +++++++++--------- spec/factories.rb | 2 +- spec/models/ahoy/data_source_spec.rb | 6 +++--- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/models/officing/residence.rb b/app/models/officing/residence.rb index 3f5b184ba..ba930b16c 100644 --- a/app/models/officing/residence.rb +++ b/app/models/officing/residence.rb @@ -29,7 +29,7 @@ class Officing::Residence document_number: document_number, document_type: document_type, geozone: geozone, - date_of_birth: date_of_birth.to_datetime, + date_of_birth: date_of_birth.in_time_zone.to_datetime, gender: gender, residence_verified_at: Time.current, verified_at: Time.current, diff --git a/app/models/verification/residence.rb b/app/models/verification/residence.rb index 14f30254f..0b8f420ce 100644 --- a/app/models/verification/residence.rb +++ b/app/models/verification/residence.rb @@ -32,7 +32,7 @@ class Verification::Residence user.update(document_number: document_number, document_type: document_type, geozone: geozone, - date_of_birth: date_of_birth.to_datetime, + date_of_birth: date_of_birth.in_time_zone.to_datetime, gender: gender, residence_verified_at: Time.current) end diff --git a/lib/census_api.rb b/lib/census_api.rb index 4fdbd37cb..44bd69ac3 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -23,7 +23,7 @@ class CensusApi str = data[:datos_habitante][:item][:fecha_nacimiento_string] day, month, year = str.match(/(\d\d?)\D(\d\d?)\D(\d\d\d?\d?)/)[1..3] return nil unless day.present? && month.present? && year.present? - Date.new(year.to_i, month.to_i, day.to_i) + Time.zone.local(year.to_i, month.to_i, day.to_i).to_date end def postal_code diff --git a/spec/controllers/admin/api/stats_controller_spec.rb b/spec/controllers/admin/api/stats_controller_spec.rb index 97ad5d0be..29cb36724 100644 --- a/spec/controllers/admin/api/stats_controller_spec.rb +++ b/spec/controllers/admin/api/stats_controller_spec.rb @@ -17,9 +17,9 @@ describe Admin::Api::StatsController do context 'events present' do before do - time_1 = DateTime.parse("2015-01-01").in_time_zone - time_2 = DateTime.parse("2015-01-02").in_time_zone - time_3 = DateTime.parse("2015-01-03").in_time_zone + time_1 = Time.zone.local(2015, 01, 01) + time_2 = Time.zone.local(2015, 01, 02) + time_3 = Time.zone.local(2015, 01, 03) create :ahoy_event, name: 'foo', time: time_1 create :ahoy_event, name: 'foo', time: time_1 @@ -52,8 +52,8 @@ describe Admin::Api::StatsController do context 'visits present' do it 'returns visits formated for working with c3.js' do - time_1 = DateTime.parse("2015-01-01").in_time_zone - time_2 = DateTime.parse("2015-01-02").in_time_zone + time_1 = Time.zone.local(2015, 01, 01) + time_2 = Time.zone.local(2015, 01, 02) create :visit, started_at: time_1 create :visit, started_at: time_1 @@ -71,8 +71,8 @@ describe Admin::Api::StatsController do context 'visits and events present' do it 'returns combined events and visits formated for working with c3.js' do - time_1 = DateTime.parse("2015-01-01").in_time_zone - time_2 = DateTime.parse("2015-01-02").in_time_zone + time_1 = Time.zone.local(2015, 01, 01) + time_2 = Time.zone.local(2015, 01, 02) create :ahoy_event, name: 'foo', time: time_1 create :ahoy_event, name: 'foo', time: time_2 @@ -94,8 +94,8 @@ describe Admin::Api::StatsController do context 'budget investments present' do it 'returns budget investments formated for working with c3.js' do - time_1 = DateTime.parse("2017-04-01").in_time_zone - time_2 = DateTime.parse("2017-04-02").in_time_zone + time_1 = Time.zone.local(2017, 04, 01) + time_2 = Time.zone.local(2017, 04, 02) budget_investment1 = create(:budget_investment, budget: @budget, created_at: time_1) budget_investment2 = create(:budget_investment, budget: @budget, created_at: time_2) diff --git a/spec/factories.rb b/spec/factories.rb index 2f9cc4acc..4e8ff1d65 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -77,7 +77,7 @@ FactoryBot.define do user document_number document_type "1" - date_of_birth Date.new(1980, 12, 31) + date_of_birth Time.zone.local(1980, 12, 31).to_date postal_code "28013" terms_of_service '1' diff --git a/spec/models/ahoy/data_source_spec.rb b/spec/models/ahoy/data_source_spec.rb index 663a47569..5b5f636e7 100644 --- a/spec/models/ahoy/data_source_spec.rb +++ b/spec/models/ahoy/data_source_spec.rb @@ -3,9 +3,9 @@ require 'rails_helper' describe Ahoy::DataSource do describe '#build' do before do - time_1 = DateTime.parse("2015-01-01").in_time_zone - time_2 = DateTime.parse("2015-01-02").in_time_zone - time_3 = DateTime.parse("2015-01-03").in_time_zone + time_1 = Time.zone.local(2015, 01, 01) + time_2 = Time.zone.local(2015, 01, 02) + time_3 = Time.zone.local(2015, 01, 03) create :ahoy_event, name: 'foo', time: time_1 create :ahoy_event, name: 'foo', time: time_1