Ruby DateTime arithmetic question

I have ~ 200,000 rows in a csv file that I need to parse. They are
time stamped and are supposed to all be 10 minutes apart from each
other. The format of the file is:

1,05/31/2006 18:08,101
1,05/31/2006 18:18,102
1,05/31/2006 18:28,103
1,05/31/2006 18:38,101
1,05/31/2006 18:48,101
1,05/31/2006 18:58,100

I need to iterate through all of these and mark the ones that have
more than 10 minutes between them.

I’m thinking to loop through the file and convert each time stamp to a
DateTime with something like:

require ‘date’

previous = DateTime.strptime(thefile[i-1][1], “%m/%d/%Y %H:%M”)
current = DateTime.strptime(thefile[i][1], “%m/%d/%Y %H:%M”)

I then could do something like:
diff = current - previous

and check to see whether diff is greater than 10 minutes – if so,
then do the processing that I need to do.

It appears that diff returns as something like Rational(1, 48). I’m
not sure what to do with that.

Any suggestions on how to move forward with this and check for the 10
minute gap?

Hi Clay,

On Wed, Jun 9, 2010 at 6:34 PM, Clay H. [email protected] wrote:

diff = current - previous

and check to see whether diff is greater than 10 minutes – if so,
then do the processing that I need to do.

There are probably more clever solutions but, bein’ me, I’d try
something simple like…

previous = DateTime.strptime(thefile[i-1][1], “%m/%d/%Y %H:%M”)
current = DateTime.strptime(thefile[i][1], “%m/%d/%Y %H:%M”)

limit = previous.advance(:minutes => 10)

if current > limit …

HTH,
Bill

On Wed, Jun 9, 2010 at 7:34 PM, Clay H. [email protected] wrote:

It appears that diff returns as something like Rational(1, 48). I’m
not sure what to do with that.

It’s the proportion of a 24 hour day, so 1/48 is a half hour.

But since you’re using rails and have active support you could do

if (current - 10.minutes) > previous

HTH


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale