Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Time.parse of empty string returns Time.now?

Reply
Thread Tools

Time.parse of empty string returns Time.now?

 
 
Peter Krantz
Guest
Posts: n/a
 
      08-20-2006
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

 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      08-20-2006

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



 
Reply With Quote
 
 
 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      08-20-2006
On Mon, 21 Aug 2006, 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


irb(main):001:0> require 'date'
=> true

irb(main):002:0 reactor_shutdown = DateTime.parse "Never"
ArgumentError: 3 elements of civil date are necessary
from /home/ahoward//lib/ruby/1.8/date.rb:1214:in `new_with_hash'
from /home/ahoward//lib/ruby/1.8/date.rb:1258:in `parse'
from (irb):3

irb(main):003:0> reactor_shutdown = DateTime.parse "2006-00-24"
ArgumentError: invalid date
from /home/ahoward//lib/ruby/1.8/date.rb:1173:in `civil'
from /home/ahoward//lib/ruby/1.8/date.rb:1216:in `new_with_hash'
from /home/ahoward//lib/ruby/1.8/date.rb:1258:in `parse'
from (irb):4


-a
--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dali lama

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GetControlValidationValue returns empty string kurt sune ASP .Net 0 09-01-2006 12:40 PM
HTTPResponse.read() returns an empty string? Christoph Söllner Python 2 10-18-2005 10:10 PM
FindControl Method always returns empty string!!!!! steroche ASP .Net Building Controls 2 08-25-2005 03:15 AM
[bug] String#split returns extra empty string Simon Strandgaard Ruby 8 06-01-2004 01:52 PM
java String split returns an additional first empty string Hanif Java 6 10-17-2003 06:21 AM



Advertisments