[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Time.parse of empty string returns Time.now?

Peter Krantz

8/20/2006 9:08:00 PM

Hi!

I am Ruby newbie and have a question regarding Time.parse(). I am
using Time.parse to scan arbitrary strings for a datetime value. When
I feed strings that have no datetime value in them Time.parse seems to
return Time.now.

How can I check if my string has a datetime value in it? I would
rather have Time.parse return nil if there wasn't any value in the
string. Right now it seems like I am unable to tell if there was a
datetime fragment in the string or not. There is no way to say if the
string contained a datetime value == now or if it was empty.

Example:

> reactor_shutdown = Time.parse("Never")
=> Sun Aug 20 23:05:33 CEST 2006

Regards,

Peter

2 Answers

Logan Capaldo

8/20/2006 9:23:00 PM

0


On Aug 20, 2006, at 5:07 PM, Peter Krantz wrote:

> Hi!
>
> I am Ruby newbie and have a question regarding Time.parse(). I am
> using Time.parse to scan arbitrary strings for a datetime value. When
> I feed strings that have no datetime value in them Time.parse seems to
> return Time.now.
>
> How can I check if my string has a datetime value in it? I would
> rather have Time.parse return nil if there wasn't any value in the
> string. Right now it seems like I am unable to tell if there was a
> datetime fragment in the string or not. There is no way to say if the
> string contained a datetime value == now or if it was empty.
>
> Example:
>
>> reactor_shutdown = Time.parse("Never")
> => Sun Aug 20 23:05:33 CEST 2006
>
> Regards,
>
> Peter
>

This is a bit of a hack but:

def parse_time(s)
if Date._parse(s).empty?
nil
else
Time.parse(s)
end
end

irb(main):020:0> parse_time("Never")
=> nil
irb(main):021:0> parse_time("2:00 pm")
=> Sun Aug 20 14:00:00 EDT 2006



Ara.T.Howard

8/20/2006 10:54:00 PM

0