From bfa12d7a02ce0e84fcd2b83f2ce6fee1386757e3 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Thu, 11 Sep 2008 12:51:16 +0200 Subject: Introduce convenience methods past?, today? and future? for Date and Time classes to facilitate Date/Time comparisons. --- .../core_ext/date_time/calculations.rb | 24 +++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/date_time') diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index 155c961a91..0099431e9d 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -7,7 +7,7 @@ module ActiveSupport #:nodoc: module Calculations def self.included(base) #:nodoc: base.extend ClassMethods - + base.class_eval do alias_method :compare_without_coercion, :<=> alias_method :<=>, :compare_with_coercion @@ -19,6 +19,20 @@ module ActiveSupport #:nodoc: def local_offset ::Time.local(2007).utc_offset.to_r / 86400 end + + def current + ::Time.zone_default ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime + end + end + + # Tells whether the DateTime object's datetime lies in the past + def past? + self < ::DateTime.current + end + + # Tells whether the DateTime object's datetime lies in the future + def future? + self > ::DateTime.current end # Seconds since midnight: DateTime.now.seconds_since_midnight @@ -78,7 +92,7 @@ module ActiveSupport #:nodoc: def end_of_day change(:hour => 23, :min => 59, :sec => 59) end - + # Adjusts DateTime to UTC by adding its offset value; offset is set to 0 # # Example: @@ -89,17 +103,17 @@ module ActiveSupport #:nodoc: new_offset(0) end alias_method :getutc, :utc - + # Returns true if offset == 0 def utc? offset == 0 end - + # Returns the offset value in seconds def utc_offset (offset * 86400).to_i end - + # Layers additional behavior on DateTime#<=> so that Time and ActiveSupport::TimeWithZone instances can be compared with a DateTime def compare_with_coercion(other) other = other.comparable_time if other.respond_to?(:comparable_time) -- cgit v1.2.3