# Time.parse seems to fail

**URL:** <https://www.ruby-forum.com/t/time-parse-seems-to-fail/169058>\
**Category:** Rails\
**Created:** [May 28, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/time-parse-seems-to-fail/169058 "2009-05-28T21:22:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ccude](https://www.ruby-forum.com/u/ccude)\
**Post date:** [May 28, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/time-parse-seems-to-fail/169058/1 "2009-05-28T21:22:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ccude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ccude](https://www.ruby-forum.com/u/ccude)\
**Post date:** [May 28, 2009, 9:26pm UTC](https://www.ruby-forum.com/t/time-parse-seems-to-fail/169058/2 "2009-05-28T21:26:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ccude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ccude](https://www.ruby-forum.com/u/ccude)\
**Post date:** [May 29, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/time-parse-seems-to-fail/169058/3 "2009-05-29T20:26:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ccude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ccude](https://www.ruby-forum.com/u/ccude)\
**Post date:** [May 30, 2009, 9:49am UTC](https://www.ruby-forum.com/t/time-parse-seems-to-fail/169058/4 "2009-05-30T09:49:10Z")

</div>

2009/5/28 Claus-christian Ude [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 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

---

<div class="post-metadata">

**Author:** ![ccude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ccude](https://www.ruby-forum.com/u/ccude)\
**Post date:** [May 30, 2009, 9:53am UTC](https://www.ruby-forum.com/t/time-parse-seems-to-fail/169058/5 "2009-05-30T09:53:16Z")

</div>

On May 30, 8:48 am, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ccude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ccude](https://www.ruby-forum.com/u/ccude)\
**Post date:** [June 1, 2009, 10:29am UTC](https://www.ruby-forum.com/t/time-parse-seems-to-fail/169058/6 "2009-06-01T10:29:57Z")

</div>

Frederick C. wrote:

> On May 30, 8:48ï¿½am, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ccude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ccude](https://www.ruby-forum.com/u/ccude)\
**Post date:** [June 1, 2009, 11:20am UTC](https://www.ruby-forum.com/t/time-parse-seems-to-fail/169058/7 "2009-06-01T11:20:51Z")

</div>

On Jun 1, 9:29 am, Claus-christian Ude \<rails-mailing-l…@andreas-  
[s.net](http://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
