# Scheduling in Ruby on Rails

**URL:** <https://www.ruby-forum.com/t/scheduling-in-ruby-on-rails/161775>\
**Category:** Rails\
**Created:** [March 2, 2009, 11:05am UTC](https://www.ruby-forum.com/t/scheduling-in-ruby-on-rails/161775 "2009-03-02T11:05:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nevermoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nevermoo](https://www.ruby-forum.com/u/nevermoo)\
**Post date:** [March 2, 2009, 11:05am UTC](https://www.ruby-forum.com/t/scheduling-in-ruby-on-rails/161775/1 "2009-03-02T11:05:12Z")

</div>

Hi all,

I hope someone can point me in the right direction. I am looking for  
some sample code to help with writing some code for scheduling  
maintenance for machinery. The idea is that, like a car, this  
machinery needs maintenance either at certain intervals or after a  
certain number of hours of use (whichever is sooner). I am not sure  
how best to manage the time measurements (DateTime or Time?) or how to  
build the business rules to do the measurements and comparisons  
robustly.

I wondered whether anyone here could direct me to an example.

Thanks,

## Ian.

---

<div class="post-metadata">

**Author:** ![nevermoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nevermoo](https://www.ruby-forum.com/u/nevermoo)\
**Post date:** [March 2, 2009, 11:22am UTC](https://www.ruby-forum.com/t/scheduling-in-ruby-on-rails/161775/2 "2009-03-02T11:22:47Z")

</div>

it all depends on your machine. nobody can tell you what’s right (or  
even the best) for you. you gotta decide for yourself.

to show you some alternatives:

- you could just work with cron-jobs.
- you could use backgrounDRb or workling/starling to schedule your own  
processes.

---

<div class="post-metadata">

**Author:** ![nevermoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nevermoo](https://www.ruby-forum.com/u/nevermoo)\
**Post date:** [March 2, 2009, 11:29am UTC](https://www.ruby-forum.com/t/scheduling-in-ruby-on-rails/161775/3 "2009-03-02T11:29:17Z")

</div>

On Mon, Mar 2, 2009 at 10:50 AM, Ian P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am not sure  
> how best to manage the time measurements (DateTime or Time?) or how to  
> build the business rules to do the measurements and comparisons  
> robustly.
> 
> I wondered whether anyone here could direct me to an example.

If you don’t need increments smaller than 1 day, I prefer to use the  
Date class.  
I find it easier than Time to work with:

- no ambiguity over “now” being a different date in different time zones  
etc

- easier to compare date\_1 == date\_2

A few simplistic examples:

> > date = Date.today  
> > =\> Mon, 02 Mar 2009  
> > next\_month = date + 1.month  
> > =\> Thu, 02 Apr 2009

> > date = Date.new 2009,01,31  
> > =\> Sat, 31 Jan 2009  
> > next\_month = date + 1.month  
> > =\> Sat, 28 Feb 2009 # automatic rounding down to valid date

> > date = Date.new 2008,02,29  
> > =\> Fri, 29 Feb 2008  
> > next\_year = date + 1.year  
> > =\> Sat, 28 Feb 2009 # automatic rounding down to valid date  
> > four\_years\_later = date + 4.year  
> > =\> Wed, 29 Feb 2012

Always be careful with comparing dates near the end of the months  
as months have a variable length.

Easier comparison of date\_1 == date\_2 (compared to time\_1 == time\_2)

> > date\_1 = Date.today  
> > =\> Mon, 02 Mar 2009  
> > date\_2 = Date.today  
> > =\> Mon, 02 Mar 2009  
> > date\_1 == date\_2  
> > =\> true  
> > time\_1 = Time.now  
> > =\> Mon Mar 02 11:24:59 +0100 2009  
> > time\_2 = Time.now  
> > =\> Mon Mar 02 11:25:06 +0100 2009  
> > time\_1 == time\_2  
> > =\> false

HTH,

Peter

---

<div class="post-metadata">

**Author:** ![nevermoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nevermoo](https://www.ruby-forum.com/u/nevermoo)\
**Post date:** [March 2, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/scheduling-in-ruby-on-rails/161775/4 "2009-03-02T12:06:59Z")

</div>

On 2 Mar 2009, at 10:28am, Peter V. wrote:

> If you don’t need increments smaller than 1 day, I prefer to use the  
> Date class.  
> I find it easier than Time to work with:
> 
> - no ambiguity over “now” being a different date in different time  
> zones etc
> - easier to compare date\_1 == date\_2
> 
> [http://ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html](http://ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html)

Thanks Peter,

Those are good examples. You mention using Date for increments that  
are not smaller than one day. In fact my situation is a hybrid, since  
the machinery may be running at intervals over several days (so I  
would need to track a usage event that spans days) but I need to  
record the total number of hours of use during that time. From what I  
can see the DateTime class allows for this. I suppose what I am really  
asking here is under what circumstances would I use the Time class:  
using DateTime always gives me more information doesn’t it?

## Ian.
