Time.parse of empty string returns Time.now?

P

Peter Krantz

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
 
L

Logan Capaldo

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:

=> 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
 
A

ara.t.howard

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:

=> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top