I'm porting an old rails app to Rails 4 and got stumped tonight on time
conversions.

This worked:

Loading development environment (Rails 2.3.18)
Time.now.to_s
=> "08/14/2013 07:09PM"
Time.now.to_s.to_time
=> Wed Aug 14 19:09:00 UTC 2013

Now it doesn't on Rails 4.0:

Loading development environment (Rails 4.0.0)
irb(main):001:0> Time.now
=> 2013-08-15 00:19:48 -0500
irb(main):002:0> Time.now.to_s
=> "08/15/2013 12:19AM"
irb(main):003:0> Time.now.to_s.to_time
ArgumentError: argument out of range
from
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/string/conversions.rb:23:in
`initialize'
from
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/string/conversions.rb:23:in
`new'

If I remove this from my initializer:

Time::DATE_FORMATS.merge!(:default => '%m/%d/%Y %I:%M%p')

It works again in 4.0, but the output format is wrong. I don't want my
users reading that time format:

Loading development environment (Rails 4.0.0)
irb(main):001:0> Time.now.to_s
=> "2013-08-15 00:32:07 -0500"
irb(main):002:0> Time.now.to_s.to_time
=> 2013-08-15 00:32:15 -0500

If it were a simple single string I was parsing, I could do a custom one
off parse and be done with it, but this is site wide. Passing entire hashes
to models is causing this error to manifest its self.

Any ideas to get Rails 4.0 to not just produce a custom Time string but
parse the same way as well? Seems like it should be simple, but perhaps
it's just getting late here. ;')

Thanks!


Phil

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/476db361-8fea-4b10-aa4e-0d70879eeaa7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Philip Edelbrock at Aug 21, 2013 at 6:19 am

    On Aug 20, 2013, at 7:57 AM, Hassan Schroeder wrote:


    In the source for e.g. ruby-1.9.3-p448 see ./doc/NEWS-1.9.1:

    o Time.parse and Date.parse interprets slashed numerical dates·
    as "dd/mm/yyyy".
    Well, back to basics, my question is simple:

    How do I tell Rails to use a custom date/time format without it blowing up?

    I put this in an initializer:

    Date::DATE_FORMATS.merge!(:default => '%m/%d/%Y')
    Time::DATE_FORMATS.merge!(:default => '%m/%d/%Y %I:%M%p')

    This is how I do that in Rails 2.3/Ruby 1.8.7, btw:

    ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(:default => '%m/%d/%Y')
    ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => '%m/%d/%Y %I:%M%p')

    That causes Rails 4.0/Ruby 2.0 to eat its self when dealing with forms that have dates/times. I'm not calling Date or Time directly in this case. I'm just letting Rails populate forms and then parse the params on submission to update a model. It is outputting the correct format, but it can't seem to parse what it is outputting. This wasn't a problem with Rails 2.3/Ruby 1.8.7.

    A simple way to manifest the problem is to do "Time.now.to_s.to_time" or "Date.today.to_s.to_date". This works on Rails 2.3/Ruby 1.8.7, but does not on Rails 4.0/Ruby 2.0 with the above initializers.

    It is hard to believe that they removed such basic functionality? Tamara suggests this might be a Ruby bug(?).

    Thanks!


    Phil

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/EA644F48-1A1B-4DB1-A366-466690BA60E4%40edgedesign.us.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Hassan Schroeder at Aug 21, 2013 at 2:18 pm
    On Tue, Aug 20, 2013 at 1:06 PM, Philip Edelbrock wrote:
    o Time.parse and Date.parse interprets slashed numerical dates·
    as "dd/mm/yyyy".
    Well, back to basics, my question is simple:

    How do I tell Rails to use a custom date/time format without it blowing up?

    I put this in an initializer:

    Date::DATE_FORMATS.merge!(:default => '%m/%d/%Y')

    See my previous comment; that format IS NOT VALID in Ruby 1.9.x and
    above. Your issue has nothing to do with Rails.

    A simple way to manifest the problem is to do "Time.now.to_s.to_time" or
    "Date.today.to_s.to_date". This works on Rails 2.3/Ruby 1.8.7, but does
    not on Rails 4.0/Ruby 2.0 with the above initializers.
    It works fine with VALID formats. Change your initializer to '%d/%m/%Y'
    and try `Date.today.to_s.to_date` -- no problem.

    It is hard to believe that they removed such basic functionality? Tamara
    suggests this might be a Ruby bug(?).

    No. It is documented behavior of Ruby.

    --
    Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yB%2Bfeix-JnMop8DJ6u2Yhg2iDF%3DOmdhjxE8%2B%3Dkvm-RqZw%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Phil at Aug 22, 2013 at 3:55 am

    On Wednesday, August 21, 2013 7:18:35 AM UTC-7, Hassan Schroeder wrote:
    On Tue, Aug 20, 2013 at 1:06 PM, Philip Edelbrock <ph...@edgedesign.us<javascript:>
    wrote:
    o Time.parse and Date.parse interprets slashed numerical dates·
    as "dd/mm/yyyy".
    Well, back to basics, my question is simple:

    How do I tell Rails to use a custom date/time format without it blowing
    up?

    I put this in an initializer:

    Date::DATE_FORMATS.merge!(:default => '%m/%d/%Y')

    See my previous comment; that format IS NOT VALID in Ruby 1.9.x and
    above. Your issue has nothing to do with Rails.

    A simple way to manifest the problem is to do "Time.now.to_s.to_time" or
    "Date.today.to_s.to_date". This works on Rails 2.3/Ruby 1.8.7, but does
    not on Rails 4.0/Ruby 2.0 with the above initializers.
    It works fine with VALID formats. Change your initializer to '%d/%m/%Y'
    and try `Date.today.to_s.to_date` -- no problem.

    It is hard to believe that they removed such basic functionality? Tamara
    suggests this might be a Ruby bug(?).

    No. It is documented behavior of Ruby.
    Don't get me wrong, I appreciate your help!

    It just seems that this was functionality that seemed to have been broken.
      It's not a 'valid' vs. invalid argument (where I live, m/d/y is valid and
    d/m/y is not!) I should be able to define 1/13/2 as being Feb 1st, 2013 if
    I want to be crazy about it. String parsing isn't hard, it just seems to
    be eluding me as how to hint Rails 4/Ruby 2 on how to do it the way I want
    when it worked in the past.

    As Tamara suggests, it appears the format string is being dropped by the
    time it makes it to the parsing function(?). d/m/y is fine as a default,
    but saying to change my initializer to match the already default is like
    removing it all together and doesn't solve my localization problem.


    Phil

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/383af81a-8358-4f90-8d88-9d161e6cb7bd%40googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Hassan Schroeder at Aug 22, 2013 at 3:47 pm

    On Wed, Aug 21, 2013 at 8:55 PM, Phil wrote:
    It's not a 'valid' vs. invalid argument
    Of course it is. What you consider valid or not is irrelevant, it's defined
    in the language.

    You happened to be lucky in picking a format (%m/%d/%Y) that worked in Ruby
    1.8.7;
    the opposite day/month placement %d/%m/%Y doesn't:

    1.8.7 :008 > Date.parse(Date.today.strftime('%m/%d/%Y')).to_s
      => "2013-08-22"
    1.8.7 :009 > Date.parse(Date.today.strftime('%d/%m/%Y')).to_s
    ArgumentError: invalid date

    As Tamara suggests, it appears the format string is being dropped by the
    time it makes it to the parsing function(?).
    Date.parse doesn't take a format argument. The Rails String#to_date
    helper uses Date.parse. It "used to work" because you were lucky.

    I can see an argument for replacing that with Date.strptime; why not
    try it and see if anything breaks? :-)

    Add your use case to the tests and go for it.

    --
    Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yAuTqDhe4dgFbrdET7UYzujz71DEh-pX7yDMTH%2Bs18cPw%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Colin Law at Aug 21, 2013 at 4:02 pm

    On 20 August 2013 21:06, Philip Edelbrock wrote:
    On Aug 20, 2013, at 7:57 AM, Hassan Schroeder wrote:


    In the source for e.g. ruby-1.9.3-p448 see ./doc/NEWS-1.9.1:

    o Time.parse and Date.parse interprets slashed numerical dates·
    as "dd/mm/yyyy".
    Well, back to basics, my question is simple:

    How do I tell Rails to use a custom date/time format without it blowing up?
    The american_date gem may help [1]

    Colin

    [1] https://github.com/jeremyevans/ruby-american_date

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuUpB%3DoqtTeTn6wF375Lqog8%2BLnRq-6o_5tLwpLfXQD1w%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Philip Edelbrock at Aug 21, 2013 at 6:19 am

    On Aug 18, 2013, at 11:57 AM, Tamara Temple wrote:

    On Aug 15, 2013, at 7:15 PM, Phil wrote:
    I'm porting an old rails app to Rails 4 and got stumped tonight on time conversions.

    This worked:

    Loading development environment (Rails 2.3.18)
    Time.now.to_s
    => "08/14/2013 07:09PM"
    Time.now.to_s.to_time
    => Wed Aug 14 19:09:00 UTC 2013

    Now it doesn't on Rails 4.0:

    Loading development environment (Rails 4.0.0)
    irb(main):001:0> Time.now
    => 2013-08-15 00:19:48 -0500
    irb(main):002:0> Time.now.to_s
    => "08/15/2013 12:19AM"
    irb(main):003:0> Time.now.to_s.to_time
    ArgumentError: argument out of range
    from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/string/conversions.rb:23:in `initialize'
    from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/string/conversions.rb:23:in `new'

    If I remove this from my initializer:

    Time::DATE_FORMATS.merge!(:default => '%m/%d/%Y %I:%M%p')

    It works again in 4.0, but the output format is wrong. I don't want my users reading that time format:

    Loading development environment (Rails 4.0.0)
    irb(main):001:0> Time.now.to_s
    => "2013-08-15 00:32:07 -0500"
    irb(main):002:0> Time.now.to_s.to_time
    => 2013-08-15 00:32:15 -0500

    If it were a simple single string I was parsing, I could do a custom one off parse and be done with it, but this is site wide. Passing entire hashes to models is causing this error to manifest its self.

    Any ideas to get Rails 4.0 to not just produce a custom Time string but parse the same way as well? Seems like it should be simple, but perhaps it's just getting late here. ;')

    Thanks!
    This isn't a Rails problem, it's something that happens in Ruby. I think it's a bug:

    $ irb -r date
    irb(main):001:0> format = "%m/%d/%Y"
    => "%m/%d/%Y"
    irb(main):002:0> Date.parse(Date.today.strftime(format),format)
    ArgumentError: invalid date
    from (irb):2:in `parse'
    from (irb):2
    from /Users/tamara/.rubies/ruby-2.0.0-p427/bin/irb:12:in `<main>'
    irb(main):003:0> Date.today.strftime(format)
    => "08/18/2013"
    irb(main):004:0> Date.parse(Date.today.to_s)
    => #<Date: 2013-08-18 ((2456523j,0s,0n),+0s,2299161j)>


    $ ruby -v
    ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]


    I don't have anything older to check this on, however, reading the system documentation for strptime(), it seems to state that formats should be compatible with strftime().

    A quick test in C[1] shows that the format from strftime to strptime is compatible. So sounds like a ruby bug…


    [1] https://gist.github.com/tamouse/6263292


    Thanks! Yes, I did update to Ruby 2.0.0p247, but I also updated to Rails-4 and, well, entirely new hardware and OS as well, so I'm not sure where the bug is exactly.

    I worked around it for now:

          params[:form][:time_field] = Time.strptime(params[:form][:time_field],"%m/%d/%Y %I:%M%p")
          @myobj = MyObj.new(params[:form].permit!)


    Phil

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/AEF6C20D-A73A-40E7-A34E-055214ECF11F%40edgedesign.us.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tamara Temple at Aug 21, 2013 at 3:37 pm

    On Aug 20, 2013, at 9:57 AM, Hassan Schroeder wrote:

    On Thu, Aug 15, 2013 at 5:15 PM, Phil wrote:

    I'm porting an old rails app to Rails 4 and got stumped tonight on time conversions.

    This worked:

    Loading development environment (Rails 2.3.18)
    Time.now.to_s
    => "08/14/2013 07:09PM"

    Now it doesn't on Rails 4.0:

    Loading development environment (Rails 4.0.0)

    irb(main):002:0> Time.now.to_s
    => "08/15/2013 12:19AM"

    1.9.3 (main):0 > Date.parse("08/20/2013").to_s
    ArgumentError: invalid date
    from (pry):22:in `parse'
    1.9.3 (main):0 > Date.parse("20/08/2013").to_s
    => "2013-08-20"
    1.9.3 (main):0 >

    In the source for e.g. ruby-1.9.3-p448 see ./doc/NEWS-1.9.1:

    o Time.parse and Date.parse interprets slashed numerical dates·
    as "dd/mm/yyyy".

    HTH!
    --
    Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yAQEDNjSQ6KtaYyn-SrxzAWecv2GZj3y%2BGC3T8ZO1_vrw%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    The documentation, however, points to the libc functions, strftime(3) and strptime(3), which state quite plainly that these formats are reflexive. Hardly seems sporting, does it? Without including a format field, it's probably quite reasonable to assume the parse methods will interpret in that dd/mm/yyyy order, BUT, *with* a format field?? They should not ignore the format field in that case. *That* is the bug.



    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9607CE16-D087-41D5-A318-93ACE17F2476%40gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedAug 18, '13 at 9:19a
activeAug 22, '13 at 3:47p
posts8
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase