# Time.parse of empty string returns Time.now?

**URL:** <https://www.ruby-forum.com/t/time-parse-of-empty-string-returns-time-now/65727>\
**Category:** Ruby\
**Created:** [August 20, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/time-parse-of-empty-string-returns-time-now/65727 "2006-08-20T23:09:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterkemp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@peterkemp](https://www.ruby-forum.com/u/peterkemp)\
**Post date:** [August 20, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/time-parse-of-empty-string-returns-time-now/65727/1 "2006-08-20T23:09:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![peterkemp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@peterkemp](https://www.ruby-forum.com/u/peterkemp)\
**Post date:** [August 20, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/time-parse-of-empty-string-returns-time-now/65727/2 "2006-08-20T23:23:38Z")

</div>

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

> datetime fragment in the string or not. There is no way to say if the

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

---

<div class="post-metadata">

**Author:** ![peterkemp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@peterkemp](https://www.ruby-forum.com/u/peterkemp)\
**Post date:** [August 21, 2006, 12:55am UTC](https://www.ruby-forum.com/t/time-parse-of-empty-string-returns-time-now/65727/3 "2006-08-21T00:55:45Z")

</div>

On Mon, 21 Aug 2006, Peter K. wrote:

> 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
