Ruby 1.9 Time parse question

Hi there!

I have a ruby program that works as expected with 1.8.6 and 1.8.7. I
just discovered some failures with some functions when i ran it in
1.9.2.

The line that is breaking is:

data_array.sort! { |a,b| Time.parse( b[1]+’ ‘+b[2] ) <=> Time.parse( a[1]+’
'+a[2] ) }

the data is an AoA and looks like:
data_array = [ [‘1’, ‘04/14/2011’, ‘10:00 am’, ‘foo’],
[‘2’, ‘04/18/2011’, ‘03:30 pm’, ‘bar’],
[‘3’, ‘04/18/2011’, ‘11:15 am’, ‘baz’]]

=> so I expect the elements to resort as: 1, 3, 2.

I want to use code that will work in both 1.8 and 1.9 using the
standard Ruby libraries for maximum portability.

In 1.8, the Time.parse method works with the ‘mm/dd/yyyy’ format,
while in 1.9 it is looking for ‘dd/mm/yyyy’.

Is there a nice clean way to sort this array by date and time that
will work in both 1.8 and 1.9?

Paul.

On Tue, Jun 7, 2011 at 3:35 PM, Paul [email protected] wrote:

data_array = [ [‘1’, ‘04/14/2011’, ‘10:00 am’, ‘foo’],

Is there a nice clean way to sort this array by date and time that
will work in both 1.8 and 1.9?

You should give the format explicitly:

16:22:52 ~$ allruby -r date -e ‘puts Date.strptime(“04/14/2011”,
“%m/%d/%Y”)’
CYGWIN_NT-5.1 padrklemme2 1.7.9(0.237/5/3) 2011-03-29 10:10 i686 Cygwin

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
2011-04-14

ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-cygwin]
2011-04-14

jruby 1.6.1 (ruby-1.8.7-p330) (2011-04-12 85838f6) (Java HotSpot™
Client VM 1.6.0_25) [Windows XP-x86-java]
2011-04-14

jruby 1.6.1 (ruby-1.9.2-p136) (2011-04-12 85838f6) (Java HotSpot™
Client VM 1.6.0_25) [Windows XP-x86-java]
2011-04-14

You can as well do the parsing yourself:

16:25:00 ~$ allruby -r date -e ‘m,d,y=“04/14/2011”.scan(/\d+/).map
{|x|x.to_i};puts Date.new(y,m,d)’
CYGWIN_NT-5.1 padrklemme2 1.7.9(0.237/5/3) 2011-03-29 10:10 i686 Cygwin

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
2011-04-14

ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-cygwin]
2011-04-14

jruby 1.6.1 (ruby-1.8.7-p330) (2011-04-12 85838f6) (Java HotSpot™
Client VM 1.6.0_25) [Windows XP-x86-java]
2011-04-14

jruby 1.6.1 (ruby-1.9.2-p136) (2011-04-12 85838f6) (Java HotSpot™
Client VM 1.6.0_25) [Windows XP-x86-java]
2011-04-14

Kind regards

robert

Hello Robert, thank you for the reply.

I played with Date.strptime but it seems to ignore the time stamps.

I need to use the time stamps to sort the records correctly. My
current code joins the date and time and parses them together using
the Time library so that it can sort it based on date+time stamp.

Is there a way I can use the Date parser so that it factors in the date
+time?
(if there is, I haven’t figured it out yet)

Thanks. Paul.

(I wish I could specify format for the Time parsing like I can with
Date parsing)

On Jun 7, 10:58am, Robert K. wrote:

Btw, I’d rather convert strings on insertion into the Array. It’s
usually best to convert input into specific types when one sees it.
Then you can use all the specific Date / Time / DateTime functionality
in your application and only convert back to string when you output
results. Also, often specific data needs less memory than data left
in strings. Also, converting only once is more efficient than doing
it over and over again. In your case you could as well use
Array#sort_by to convert each timestamp only once.

Hello Robert, I was thinking the same thing. The biggest difference is
between re-writing two sort routines and re-writing a third of the
entire program.

Looks like I have some refactoring to do this week…

Cheers! Paul.

Please do not top post.

On Tue, Jun 7, 2011 at 4:48 PM, Paul [email protected] wrote:

Hello Robert, thank you for the reply.

I played with Date.strptime but it seems to ignore the time stamps.

Clearly, as Date is about dates. :slight_smile:

I need to use the time stamps to sort the records correctly. My
current code joins the date and time and parses them together using
the Time library so that it can sort it based on date+time stamp.

Is there a way I can use the Date parser so that it factors in the date
+time?
(if there is, I haven’t figured it out yet)

My bad. I overlooked that you want time as well. Then use class Time
or DateTime instead. They all have method strptime. Legal format
patterns are in the docs.

(I wish I could specify format for the Time parsing like I can with
Date parsing)

You can.

Btw, I’d rather convert strings on insertion into the Array. It’s
usually best to convert input into specific types when one sees it.
Then you can use all the specific Date / Time / DateTime functionality
in your application and only convert back to string when you output
results. Also, often specific data needs less memory than data left
in strings. Also, converting only once is more efficient than doing
it over and over again. In your case you could as well use
Array#sort_by to convert each timestamp only once.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs