Time Comparisons plugin

Plugin details

Provides for chronological comparisons between Time, Date and DateTime objects using before?, after? same_time?, same_time_or_before?, same_time_or_after?, within?, outside? and compare_time methods.

Websitehttp://code.google.com/p/time-comparisons/ Repositoryhttp://time-comparisons.googlecode.com/svn/trunk/ Author Geoff Buesing Tags Time, comparison LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install http://time-comparisons.googlecode.com/svn/trunk/

Examples:

  past      = Time.utc(2007, 6, 7, 4, 30, 29)
  present   = Time.utc(2007, 6, 7, 4, 30, 30)
  future    = Time.utc(2007, 6, 7, 4, 30, 31)

  present.same_time? DateTime.civil(2007, 6, 7, 4, 30, 30)  # => true
  past.before? present  # => true
  past.same_time_or_before? present  # => true
  past.after? present  # => false
  past.after? Date.new(2012,1,1)  # => true
  past.same_time_or_after? present  # => false
  present.within? past, future  # => true
  present.outside? past, future  # => false

  times = [present, future, past, Date.new(1990), DateTime.civil(2012)]
  puts times.sort {|a,b| a.compare_time(b)}  # =>
=begin
  1990-01-01
  Fri Jun 08 16:30:29 UTC 2007
  Fri Jun 08 16:30:30 UTC 2007
  Fri Jun 08 16:30:31 UTC 2007
  2012-01-01T00:00:00+00:00
=end


Note that Date objects are considered as a time point at the beginning of the day. Therefore the following will be true:

  Time.now.after? Date.today # => true 

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | Back in time (1 older version) | Last edited by: hardway, 8 months ago