Is there a reason why AR doesn''t pass the guess year flag to parsedate when casting columns? Seems to me, in the context of a DB date field, you would always want the 4 digit year. Right now, if you shove a date like: 07/18/06 to Rails, MySQL will get 0006-07-16, but ParseDate.parsedate(''07/18/06'', true) will create the right 4-digit year. Just curious. Bob Silva http://i.nfectio.us <http://i.nfectio.us/> _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
> > Is there a reason why AR doesn''t pass the guess year flag to parsedate when > casting columns? Seems to me, in the context of a DB date field, you would > always want the 4 digit year. > > > > Right now, if you shove a date like: 07/18/06 to Rails, MySQL will get > 0006-07-16, but ParseDate.parsedate(''07/18/06'', true) will create the right > 4-digit year. > > > > Just curious…I''ve never seen any of the adapters actually return a value like this for a date field? Perhaps it''s just never come up. Have you hit this in production? -- Cheers Koz
Hi Koz, The date value coming in is from an html text field, the db column is defined as a date. Here''s a script/console session showing the behavior on 1.1.2: I use attributes_with_quotes as that''s how the value gets put into the sql insert statement. # script/console Loading development environment.>> me = Encounter.new(:encounter_date => ''07/18/06'')=> #<Encounter:0x409d19e8 @new_record=true, @attributes={"encounter_date"=>"07/18/06"}>>> me.send(:attributes_with_quotes)=> {"encounter_date"=>"''0006-07-18''"} <=== NOTE THE BAD YEAR>> >> module ActiveRecord >> module ConnectionAdapters #:nodoc: >> # An abstract definition of a column in a table.?> class Column>> def self.string_to_date(string) >> return string unless string.is_a?(String) >> date_array = ParseDate.parsedate(string, true) <=== NOTE THE TRUEPARAM>> # treat 0000-00-00 as nil?> Date.new(date_array[0], date_array[1], date_array[2]) rescue nil>> end >> end >> end >> end=> nil>> me.send(:attributes_with_quotes)=> {"encounter_date"=>"''2006-07-18''"} <=== NOTE THE GOOD YEAR>>Rails date helpers would never run into this because they define the dates via a select box, but I don''t think Rails should only accept dates via the helpers. Bob Silva http://i.nfectio.us -----Original Message----- From: rails-core-bounces@lists.rubyonrails.org [mailto:rails-core-bounces@lists.rubyonrails.org] On Behalf Of Michael Koziarski Sent: Tuesday, July 18, 2006 7:51 PM To: rails-core@lists.rubyonrails.org Subject: Re: [Rails-core] AR Dates and ParseDate> > Is there a reason why AR doesn''t pass the guess year flag to parsedatewhen> casting columns? Seems to me, in the context of a DB date field, you would > always want the 4 digit year. > > > > Right now, if you shove a date like: 07/18/06 to Rails, MySQL will get > 0006-07-16, but ParseDate.parsedate(''07/18/06'', true) will create theright> 4-digit year. > > > > Just curious.I''ve never seen any of the adapters actually return a value like this for a date field? Perhaps it''s just never come up. Have you hit this in production? -- Cheers Koz _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Any opinions on this? -----Original Message----- From: rails-core-bounces@lists.rubyonrails.org [mailto:rails-core-bounces@lists.rubyonrails.org] On Behalf Of Bob Silva Sent: Tuesday, July 18, 2006 8:37 PM To: rails-core@lists.rubyonrails.org Subject: RE: [Rails-core] AR Dates and ParseDate Hi Koz, The date value coming in is from an html text field, the db column is defined as a date. Here''s a script/console session showing the behavior on 1.1.2: I use attributes_with_quotes as that''s how the value gets put into the sql insert statement. # script/console Loading development environment.>> me = Encounter.new(:encounter_date => ''07/18/06'')=> #<Encounter:0x409d19e8 @new_record=true, @attributes={"encounter_date"=>"07/18/06"}>>> me.send(:attributes_with_quotes)=> {"encounter_date"=>"''0006-07-18''"} <=== NOTE THE BAD YEAR>> >> module ActiveRecord >> module ConnectionAdapters #:nodoc: >> # An abstract definition of a column in a table.?> class Column>> def self.string_to_date(string) >> return string unless string.is_a?(String) >> date_array = ParseDate.parsedate(string, true) <=== NOTE THE TRUEPARAM>> # treat 0000-00-00 as nil?> Date.new(date_array[0], date_array[1], date_array[2]) rescue nil>> end >> end >> end >> end=> nil>> me.send(:attributes_with_quotes)=> {"encounter_date"=>"''2006-07-18''"} <=== NOTE THE GOOD YEAR>>Rails date helpers would never run into this because they define the dates via a select box, but I don''t think Rails should only accept dates via the helpers. Bob Silva http://i.nfectio.us -----Original Message----- From: rails-core-bounces@lists.rubyonrails.org [mailto:rails-core-bounces@lists.rubyonrails.org] On Behalf Of Michael Koziarski Sent: Tuesday, July 18, 2006 7:51 PM To: rails-core@lists.rubyonrails.org Subject: Re: [Rails-core] AR Dates and ParseDate> > Is there a reason why AR doesn''t pass the guess year flag to parsedatewhen> casting columns? Seems to me, in the context of a DB date field, you would > always want the 4 digit year. > > > > Right now, if you shove a date like: 07/18/06 to Rails, MySQL will get > 0006-07-16, but ParseDate.parsedate(''07/18/06'', true) will create theright> 4-digit year. > > > > Just curious.I''ve never seen any of the adapters actually return a value like this for a date field? Perhaps it''s just never come up. Have you hit this in production? -- Cheers Koz _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
My validates_date_time plugin makes Rails'' handling of date and time strings much more sane, at least in my opinion. It would fix the problem you describe, as well as a few others. http://svn.viney.net.nz/things/rails/plugins/validates_date_time -Jonathan. On 7/27/06, Bob Silva <me@bobsilva.com> wrote:> Any opinions on this? > > -----Original Message----- > From: rails-core-bounces@lists.rubyonrails.org > [mailto:rails-core-bounces@lists.rubyonrails.org] On Behalf Of Bob Silva > Sent: Tuesday, July 18, 2006 8:37 PM > To: rails-core@lists.rubyonrails.org > Subject: RE: [Rails-core] AR Dates and ParseDate > > Hi Koz, > > The date value coming in is from an html text field, the db column is > defined as a date. Here''s a script/console session showing the behavior on > 1.1.2: > > I use attributes_with_quotes as that''s how the value gets put into the sql > insert statement. > > > # script/console > Loading development environment. > >> me = Encounter.new(:encounter_date => ''07/18/06'') > => #<Encounter:0x409d19e8 @new_record=true, > @attributes={"encounter_date"=>"07/18/06"}> > >> me.send(:attributes_with_quotes) > => {"encounter_date"=>"''0006-07-18''"} <=== NOTE THE BAD YEAR > >> > >> module ActiveRecord > >> module ConnectionAdapters #:nodoc: > >> # An abstract definition of a column in a table. > ?> class Column > >> def self.string_to_date(string) > >> return string unless string.is_a?(String) > >> date_array = ParseDate.parsedate(string, true) <=== NOTE THE TRUE > PARAM > >> # treat 0000-00-00 as nil > ?> Date.new(date_array[0], date_array[1], date_array[2]) rescue nil > >> end > >> end > >> end > >> end > => nil > >> me.send(:attributes_with_quotes) > => {"encounter_date"=>"''2006-07-18''"} <=== NOTE THE GOOD YEAR > >> > > Rails date helpers would never run into this because they define the dates > via a select box, but I don''t think Rails should only accept dates via the > helpers. > > Bob Silva > http://i.nfectio.us > > > -----Original Message----- > From: rails-core-bounces@lists.rubyonrails.org > [mailto:rails-core-bounces@lists.rubyonrails.org] On Behalf Of Michael > Koziarski > Sent: Tuesday, July 18, 2006 7:51 PM > To: rails-core@lists.rubyonrails.org > Subject: Re: [Rails-core] AR Dates and ParseDate > > > > > Is there a reason why AR doesn''t pass the guess year flag to parsedate > when > > casting columns? Seems to me, in the context of a DB date field, you would > > always want the 4 digit year. > > > > > > > > Right now, if you shove a date like: 07/18/06 to Rails, MySQL will get > > 0006-07-16, but ParseDate.parsedate(''07/18/06'', true) will create the > right > > 4-digit year. > > > > > > > > Just curious. > > I''ve never seen any of the adapters actually return a value like this > for a date field? Perhaps it''s just never come up. Have you hit this > in production? > > > -- > Cheers > > Koz > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core >