# Ruby DateTime arithmetic question

**URL:** <https://www.ruby-forum.com/t/ruby-datetime-arithmetic-question/189926>\
**Category:** Rails\
**Created:** [June 10, 2010, 1:35am UTC](https://www.ruby-forum.com/t/ruby-datetime-arithmetic-question/189926 "2010-06-10T01:35:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clay\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Clay\_H](https://www.ruby-forum.com/u/Clay_H)\
**Post date:** [June 10, 2010, 1:35am UTC](https://www.ruby-forum.com/t/ruby-datetime-arithmetic-question/189926/1 "2010-06-10T01:35:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Clay\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Clay\_H](https://www.ruby-forum.com/u/Clay_H)\
**Post date:** [June 10, 2010, 1:46am UTC](https://www.ruby-forum.com/t/ruby-datetime-arithmetic-question/189926/2 "2010-06-10T01:46:19Z")

</div>

Hi Clay,

On Wed, Jun 9, 2010 at 6:34 PM, Clay H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Clay\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@Clay\_H](https://www.ruby-forum.com/u/Clay_H)\
**Post date:** [June 11, 2010, 8:41pm UTC](https://www.ruby-forum.com/t/ruby-datetime-arithmetic-question/189926/3 "2010-06-11T20:41:21Z")

</div>

On Wed, Jun 9, 2010 at 7:34 PM, Clay H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
