Time Comparisons Rails plugin
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.
This provides a more readable alternative the <, >, <=, >=, == and <=> methods for time comparison, and allows for comparing a Time with a Date/DateTime (the built-in methods return an ArgumentError in this case.)
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)} # =>
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:00COMPATIBILITY NOTE: If you're not running Rails 1.2.3 + Ruby 1.8.6 -or- Edge Rails >= r6335, uncomment the Time#to_datetime method in this plugin's init.rb file for full compatibility.