diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext/time')
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 30 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/conversions.rb | 22 |
2 files changed, 50 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 00fda2b370..372dd69212 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -1,5 +1,6 @@ require 'active_support/duration' require 'active_support/core_ext/time/zones' +require 'active_support/core_ext/time/conversions' class Time COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] @@ -8,7 +9,7 @@ class Time class << self # Overriding case equality method so that it returns true for ActiveSupport::TimeWithZone instances def ===(other) - other.is_a?(::Time) + super || (self == Time && other.is_a?(ActiveSupport::TimeWithZone)) end # Return the number of days in the given month. @@ -226,7 +227,7 @@ class Time end alias :at_end_of_quarter :end_of_quarter - # Returns a new Time representing the start of the year (1st of january, 0:00) + # Returns a new Time representing the start of the year (1st of january, 0:00) def beginning_of_year change(:month => 1, :day => 1, :hour => 0) end @@ -248,6 +249,31 @@ class Time advance(:days => 1) end + # Returns a Range representing the whole day of the current time. + def all_day + beginning_of_day..end_of_day + end + + # Returns a Range representing the whole week of the current time. + def all_week + beginning_of_week..end_of_week + end + + # Returns a Range representing the whole month of the current time. + def all_month + beginning_of_month..end_of_month + end + + # Returns a Range representing the whole quarter of the current time. + def all_quarter + beginning_of_quarter..end_of_quarter + end + + # Returns a Range representing the whole year of the current time. + def all_year + beginning_of_year..end_of_year + end + def plus_with_duration(other) #:nodoc: if ActiveSupport::Duration === other other.since(self) diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index d9d5e02778..49ac18d245 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -55,9 +55,31 @@ class Time utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon) end + # Converts a Time object to a Date, dropping hour, minute, and second precision. + # + # my_time = Time.now # => Mon Nov 12 22:59:51 -0500 2007 + # my_time.to_date # => Mon, 12 Nov 2007 + # + # your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009 + # your_time.to_date # => Tue, 13 Jan 2009 + def to_date + ::Date.new(year, month, day) + end unless method_defined?(:to_date) + # A method to keep Time, Date and DateTime instances interchangeable on conversions. # In this case, it simply returns +self+. def to_time self end unless method_defined?(:to_time) + + # Converts a Time instance to a Ruby DateTime instance, preserving UTC offset. + # + # my_time = Time.now # => Mon Nov 12 23:04:21 -0500 2007 + # my_time.to_datetime # => Mon, 12 Nov 2007 23:04:21 -0500 + # + # your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009 + # your_time.to_datetime # => Tue, 13 Jan 2009 13:13:03 -0500 + def to_datetime + ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400)) + end unless method_defined?(:to_datetime) end |