Forum: Ruby on Rails Time.parse seems to fail

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2ec870b7b79f5a96049c50797ce8a208?d=identicon&s=25 Claus-christian Ude (ccude)
on 2009-05-28 21:22
Hi,

it's seems to me the function Time.parse has an error:

ok    - Time.parse("12/01/2008") => 2008-12-01
Error - Time.parse("01.12.2008") => 2008-01-12

Or is there an option/timezone I have to chnage, so it will work
correctly?


Greetings Claus-Christian Ude
2ec870b7b79f5a96049c50797ce8a208?d=identicon&s=25 Claus-christian Ude (ccude)
on 2009-05-28 21:26
Ok, some more details:
> it's seems to me the function Time.parse has an error:
>
> ok    - Time.parse("12/01/2008") => 2008-12-01
> Error - Time.parse("01.12.2008") => 2008-01-12
>

Time.parse("01.12.2008") => 2008-12-01 whould be correct. I found
already the function _parse in lib/ruby/1.8/date/format.rb. But before I
change in this file, must be really sure, there is no other solution.

mfg
2ec870b7b79f5a96049c50797ce8a208?d=identicon&s=25 Claus-christian Ude (ccude)
on 2009-05-29 20:26
Ok, I have the solution. Here I correct the wrong function of
Date.parse:

class Date
  class << self
    alias :oldparse :_parse

    def _parse(str, comp=false)
      if str =~ /(\d{1,2})\.(\d{1,2})\.(\d\d\d\d)\ (\d{1,2})\:(\d\d)/
        e       = Format::Bag.new
        e.mday  = $1.to_i
        e.mon   = $2.to_i
        e.year  = $3.to_i
        e.hour  = $4.to_i
        e.min   = $5.to_i
        return e.to_hash
      end
      if str =~ /(\d{1,2})\.(\d{1,2})\.(\d\d\d\d)/
        e       = Format::Bag.new
        e.mday  = $1.to_i
        e.mon   = $2.to_i
        e.year  = $3.to_i
        return e.to_hash
      end

      oldparse(str, comp)
    end
  end
end
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-30 09:49
(Received via mailing list)
2009/5/28 Claus-christian Ude <rails-mailing-list@andreas-s.net>

>
> Hi,
>
> it's seems to me the function Time.parse has an error:
>
> ok    - Time.parse("12/01/2008") => 2008-12-01
> Error - Time.parse("01.12.2008") => 2008-01-12
>

My brain does not seem to be working well today, I cannot actually see
what
you are getting at.  What would you expect the second one to give and
why?

Colin
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-30 09:53
(Received via mailing list)
On May 30, 8:48 am, Colin Law <clan...@googlemail.com> wrote:
>
> My brain does not seem to be working well today, I cannot actually see what
> you are getting at.  What would you expect the second one to give and why?
>

These things are ofter locale dependant. My european brain expects day
month year, but apparently in the US month ,day, year is more common.
If you are expecting dates in a particular format strptime is useful,
if not some sort of calendary widget is often a better idea.

Fred
2ec870b7b79f5a96049c50797ce8a208?d=identicon&s=25 Claus-christian Ude (ccude)
on 2009-06-01 10:29
Frederick Cheung wrote:
> On May 30, 8:48�am, Colin Law <clan...@googlemail.com> wrote:
> If you are expecting dates in a particular format strptime is useful,
> if not some sort of calendary widget is often a better idea.
>
> Fred

Well yes, I already do so (calendar_select_date), but I get in the
controller-function as params the dd.mm.yyyy format. So
update_attributes convert the date automaticly, but it do it wrong
(mm.dd.yyyy), therefor I "repair" the Date.parse-Function.

mfg C-C
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-06-01 11:20
(Received via mailing list)
On Jun 1, 9:29 am, Claus-christian Ude <rails-mailing-l...@andreas-
s.net> wrote:
> (mm.dd.yyyy), therefor I "repair" the Date.parse-Function.
>

if you're guarenteed that the format you get in the controller is
going to be dd.mm.yyyy then strptime is probably the easiest way
(although just tearing the string up with a regexp wouldn't be hard
either)

Fred
This topic is locked and can not be replied to.