[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

setting date format to validate date in ruby

Jay Pangmi

8/24/2008 4:35:00 AM

Hi, I'm just learning ruby. I was searching for date validation in ruby
and found this:
================================================================================
for date in dateArray
if %r{(\d\d)/(\d\d)/(\d\d\d\d)} =~ date
mday, month, year = $1.to_i, $2.to_i, $3.to_i
puts date if Date.valid_civil?(year, month, mday)
else
puts "Invalid Format"
end
end
================================================================================

This seemed to do the job but found out not. The problem is it only
validates the 01/12/2008 but 1/2/2008 fails. Also the date with
different separater fails as well. For 1/2/2008 such dates I tried the
following:
================================================================================
for date in dateArray
if %r{(\d{1,2})/(\d{1,2})/(\d{4})} =~ date
mday, month, year = $1.to_i, $2.to_i, $3.to_i
puts date if Date.valid_civil?(year,month,mday)
else
puts "Invalid Format"
end
end
================================================================================
For some strange reason, it only iterates thru 5 dates though I have
seven dates in dateArray. But for the five it does it right. So, I'm
wondering why? also what can I do to give different options for
separators like "/,:,-," or space. Though the original code is not my
invention I am wondering what does this line do in particular:
================================================================================
......
mday, month, year = $1.to_i, $2.to_i, $3.to_i
......
================================================================================

Thanks.
--
Posted via http://www.ruby-....

4 Answers

TPReal

8/24/2008 8:07:00 AM

0

Jay Pangmi wrote:
> Hi, I'm just learning ruby. I was searching for date validation in ruby

You can have a look at this post:
http://al2o3-cr.blogspot.com/2008/08/downloading... . There's
date validation in it, in the very general form, working with any
separator and any number of digits. It uses scanning for digit groups.

TPR.
--
Posted via http://www.ruby-....

Jay Pangmi

8/24/2008 8:50:00 AM

0

Thomas B. wrote:
> You can have a look at this post:
> http://al2o3-cr.blogspot.com/2008/08/downloading... . There's
> date validation in it, in the very general form, working with any
> separator and any number of digits. It uses scanning for digit groups.
>
> TPR.

wow.. man that was cool, specially for the beginner like me. Thanks a
tonn. Anyway, I figured one solution out for me here's the code:
================================================================================
for mydate in dateArray
if %r{(\d{1,2})/(\d{1,2})/(\d{4})} =~ mydate or
%r{(\d{1,2}):(\d{1,2}):(\d{4})} =~ mydate or
%r{(\d{1,2}) (\d{1,2}) (\d{4})} =~ mydate
mday, month, year = $1.to_i, $2.to_i, $3.to_i
if Date.valid_civil?(year,month,mday)
#virtualArray << mydate
puts "Valid Date: " << mydate
else
puts "Invalid Date: " << mydate
end
end
end
================================================================================
One really cool thing about Date.valid_civil? is that it return false
even if the date doesn't exist in the calender no matter how right the
format is.
Now what is bothering me is the logic to sort out the dates in the
array. I tried the simplest way "Array.sort" but it only sorted the
array according to the first value it sees not the whole date. How can I
sort it out. Thanks

--
Posted via http://www.ruby-....

TPReal

8/24/2008 9:09:00 AM

0

Jay Pangmi wrote:
> Thomas B. wrote:
>> You can have a look at this post:
>> http://al2o3-cr.blogspot.com/2008/08/downloading... . There's
>> date validation in it, in the very general form, working with any
>> separator and any number of digits. It uses scanning for digit groups.
>
> Now what is bothering me is the logic to sort out the dates in the
> array. I tried the simplest way "Array.sort" but it only sorted the
> array according to the first value it sees not the whole date. How can I
> sort it out. Thanks

Use the same approach as is used in the post I gave you link to:
- create Date objects for each date (once you parse it and see that it
is correct), and put the dates in an array,
- sort the array of dates, they will get sorted OK,
- convert dates back to strings if you need to, using Date#strftime.

TPR.
--
Posted via http://www.ruby-....

Suraj Kurapati

8/25/2008 3:58:00 PM

0

Thomas B. wrote:
> Jay Pangmi wrote:
>> Thomas B. wrote:
>>> You can have a look at this post:
>>> http://al2o3-cr.blogspot.com/2008/08/downloading... . There's
>>> date validation in it, in the very general form, working with any
>>> separator and any number of digits. It uses scanning for digit groups.
>>
>> Now what is bothering me is the logic to sort out the dates in the
>> array. I tried the simplest way "Array.sort" but it only sorted the
>> array according to the first value it sees not the whole date. How can I
>> sort it out. Thanks
>
> Use the same approach as is used in the post I gave you link to:
> - create Date objects for each date (once you parse it and see that it
> is correct), and put the dates in an array,
> - sort the array of dates, they will get sorted OK,
> - convert dates back to strings if you need to, using Date#strftime.

This can all be done *temporarily* if you use Array#sort_by:

string_dates = [ ... ]
result = string_dates.sort_by {|s| Date.parse s }

Assuming that Date.parse is how you convert your string dates into Date
objects.
--
Posted via http://www.ruby-....