aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb33
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/calculations.rb24
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb35
3 files changed, 68 insertions, 24 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb
index b5180c9592..43d70c7013 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -20,18 +20,33 @@ module ActiveSupport #:nodoc:
def yesterday
::Date.today.yesterday
end
-
+
# Returns a new Date representing the date 1 day after today (i.e. tomorrow's date).
def tomorrow
::Date.today.tomorrow
end
-
+
# Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today.
def current
::Time.zone_default ? ::Time.zone.today : ::Date.today
end
end
-
+
+ # Tells whether the Date object's date lies in the past
+ def past?
+ self < ::Date.current
+ end
+
+ # Tells whether the Date object's date is today
+ def today?
+ self.to_date == ::Date.current # we need the to_date because of DateTime
+ end
+
+ # Tells whether the Date object's date lies in the future
+ def future?
+ self > ::Date.current
+ end
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
# and then subtracts the specified number of seconds
def ago(seconds)
@@ -57,7 +72,7 @@ module ActiveSupport #:nodoc:
def end_of_day
to_time.end_of_day
end
-
+
def plus_with_duration(other) #:nodoc:
if ActiveSupport::Duration === other
other.since(self)
@@ -65,7 +80,7 @@ module ActiveSupport #:nodoc:
plus_without_duration(other)
end
end
-
+
def minus_with_duration(other) #:nodoc:
if ActiveSupport::Duration === other
plus_with_duration(-other)
@@ -73,8 +88,8 @@ module ActiveSupport #:nodoc:
minus_without_duration(other)
end
end
-
- # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
+
+ # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
# any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
def advance(options)
d = self
@@ -98,7 +113,7 @@ module ActiveSupport #:nodoc:
options[:day] || self.day
)
end
-
+
# Returns a new Date/DateTime representing the time a number of specified months ago
def months_ago(months)
advance(:months => -months)
@@ -161,7 +176,7 @@ module ActiveSupport #:nodoc:
days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
result = (self + 7).beginning_of_week + days_into_week[day]
self.acts_like?(:time) ? result.change(:hour => 0) : result
- end
+ end
# Returns a new ; DateTime objects will have time set to 0:00DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00)
def beginning_of_month
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)
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index cd234c9b89..070f72c854 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -9,13 +9,13 @@ module ActiveSupport #:nodoc:
base.class_eval do
alias_method :plus_without_duration, :+
alias_method :+, :plus_with_duration
-
+
alias_method :minus_without_duration, :-
alias_method :-, :minus_with_duration
-
+
alias_method :minus_without_coercion, :-
alias_method :-, :minus_with_coercion
-
+
alias_method :compare_without_coercion, :<=>
alias_method :<=>, :compare_with_coercion
end
@@ -28,9 +28,9 @@ module ActiveSupport #:nodoc:
def ===(other)
other.is_a?(::Time)
end
-
- # Return the number of days in the given month.
- # If no year is specified, it will use the current year.
+
+ # Return the number of days in the given month.
+ # If no year is specified, it will use the current year.
def days_in_month(month, year = now.year)
return 29 if month == 2 && ::Date.gregorian_leap?(year)
COMMON_YEAR_DAYS_IN_MONTH[month]
@@ -57,6 +57,21 @@ module ActiveSupport #:nodoc:
end
end
+ # Tells whether the Time object's time lies in the past
+ def past?
+ self < ::Time.current
+ end
+
+ # Tells whether the Time object's time is today
+ def today?
+ self.to_date == ::Date.current
+ end
+
+ # Tells whether the Time object's time lies in the future
+ def future?
+ self > ::Time.current
+ end
+
# Seconds since midnight: Time.now.seconds_since_midnight
def seconds_since_midnight
self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
@@ -106,7 +121,7 @@ module ActiveSupport #:nodoc:
(seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
end
rescue
- self.to_datetime.since(seconds)
+ self.to_datetime.since(seconds)
end
alias :in :since
@@ -199,7 +214,7 @@ module ActiveSupport #:nodoc:
change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 0)
end
alias :at_end_of_month :end_of_month
-
+
# Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00)
def beginning_of_quarter
beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
@@ -249,7 +264,7 @@ module ActiveSupport #:nodoc:
minus_without_duration(other)
end
end
-
+
# Time#- can also be used to determine the number of seconds between two Time instances.
# We're layering on additional behavior so that ActiveSupport::TimeWithZone instances
# are coerced into values that Time#- will recognize
@@ -257,7 +272,7 @@ module ActiveSupport #:nodoc:
other = other.comparable_time if other.respond_to?(:comparable_time)
minus_without_coercion(other)
end
-
+
# Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances
# can be chronologically compared with a Time
def compare_with_coercion(other)