On Mon, Mar 2, 2009 at 10:50 AM, Ian Piper <[email protected]> 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

  http://ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html

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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to