mirror of
https://github.com/github/rails.git
synced 2026-01-26 14:58:11 -05:00
Introduce convenience methods past?, today? and future? for Date and Time classes to facilitate Date/Time comparisons.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
require 'tzinfo'
|
||||
module ActiveSupport
|
||||
# A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are
|
||||
# A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are
|
||||
# limited to UTC and the system's <tt>ENV['TZ']</tt> zone.
|
||||
#
|
||||
# You shouldn't ever need to create a TimeWithZone instance directly via <tt>new</tt> -- instead, Rails provides the methods
|
||||
@@ -32,12 +32,12 @@ module ActiveSupport
|
||||
class TimeWithZone
|
||||
include Comparable
|
||||
attr_reader :time_zone
|
||||
|
||||
|
||||
def initialize(utc_time, time_zone, local_time = nil, period = nil)
|
||||
@utc, @time_zone, @time = utc_time, time_zone, local_time
|
||||
@period = @utc ? period : get_period_and_ensure_valid_local_time
|
||||
end
|
||||
|
||||
|
||||
# Returns a Time or DateTime instance that represents the time in +time_zone+.
|
||||
def time
|
||||
@time ||= period.to_local(@utc)
|
||||
@@ -51,7 +51,7 @@ module ActiveSupport
|
||||
alias_method :getgm, :utc
|
||||
alias_method :getutc, :utc
|
||||
alias_method :gmtime, :utc
|
||||
|
||||
|
||||
# Returns the underlying TZInfo::TimezonePeriod.
|
||||
def period
|
||||
@period ||= time_zone.period_for_utc(@utc)
|
||||
@@ -62,38 +62,38 @@ module ActiveSupport
|
||||
return self if time_zone == new_zone
|
||||
utc.in_time_zone(new_zone)
|
||||
end
|
||||
|
||||
|
||||
# Returns a <tt>Time.local()</tt> instance of the simultaneous time in your system's <tt>ENV['TZ']</tt> zone
|
||||
def localtime
|
||||
utc.getlocal
|
||||
end
|
||||
alias_method :getlocal, :localtime
|
||||
|
||||
|
||||
def dst?
|
||||
period.dst?
|
||||
end
|
||||
alias_method :isdst, :dst?
|
||||
|
||||
|
||||
def utc?
|
||||
time_zone.name == 'UTC'
|
||||
end
|
||||
alias_method :gmt?, :utc?
|
||||
|
||||
|
||||
def utc_offset
|
||||
period.utc_total_offset
|
||||
end
|
||||
alias_method :gmt_offset, :utc_offset
|
||||
alias_method :gmtoff, :utc_offset
|
||||
|
||||
|
||||
def formatted_offset(colon = true, alternate_utc_string = nil)
|
||||
utc? && alternate_utc_string || utc_offset.to_utc_offset_s(colon)
|
||||
end
|
||||
|
||||
|
||||
# Time uses +zone+ to display the time zone abbreviation, so we're duck-typing it.
|
||||
def zone
|
||||
period.zone_identifier.to_s
|
||||
end
|
||||
|
||||
|
||||
def inspect
|
||||
"#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
|
||||
end
|
||||
@@ -122,7 +122,7 @@ module ActiveSupport
|
||||
%("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def to_yaml(options = {})
|
||||
if options.kind_of?(YAML::Emitter)
|
||||
utc.to_yaml(options)
|
||||
@@ -130,19 +130,19 @@ module ActiveSupport
|
||||
time.to_yaml(options).gsub('Z', formatted_offset(true, 'Z'))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def httpdate
|
||||
utc.httpdate
|
||||
end
|
||||
|
||||
|
||||
def rfc2822
|
||||
to_s(:rfc822)
|
||||
end
|
||||
alias_method :rfc822, :rfc2822
|
||||
|
||||
|
||||
# <tt>:db</tt> format outputs time in UTC; all others output time in local.
|
||||
# Uses TimeWithZone's +strftime+, so <tt>%Z</tt> and <tt>%z</tt> work correctly.
|
||||
def to_s(format = :default)
|
||||
def to_s(format = :default)
|
||||
return utc.to_s(format) if format == :db
|
||||
if formatter = ::Time::DATE_FORMATS[format]
|
||||
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
|
||||
@@ -150,27 +150,39 @@ module ActiveSupport
|
||||
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Replaces <tt>%Z</tt> and <tt>%z</tt> directives with +zone+ and +formatted_offset+, respectively, before passing to
|
||||
# Time#strftime, so that zone information is correct
|
||||
def strftime(format)
|
||||
format = format.gsub('%Z', zone).gsub('%z', formatted_offset(false))
|
||||
time.strftime(format)
|
||||
end
|
||||
|
||||
|
||||
# Use the time in UTC for comparisons.
|
||||
def <=>(other)
|
||||
utc <=> other
|
||||
end
|
||||
|
||||
|
||||
def between?(min, max)
|
||||
utc.between?(min, max)
|
||||
end
|
||||
|
||||
|
||||
def past?
|
||||
utc.past?
|
||||
end
|
||||
|
||||
def today?
|
||||
utc.today?
|
||||
end
|
||||
|
||||
def future?
|
||||
utc.future?
|
||||
end
|
||||
|
||||
def eql?(other)
|
||||
utc == other
|
||||
end
|
||||
|
||||
|
||||
def +(other)
|
||||
# If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time,
|
||||
# otherwise move forward from #utc, for accuracy when moving across DST boundaries
|
||||
@@ -194,7 +206,7 @@ module ActiveSupport
|
||||
result.in_time_zone(time_zone)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def since(other)
|
||||
# If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time,
|
||||
# otherwise move forward from #utc, for accuracy when moving across DST boundaries
|
||||
@@ -204,7 +216,7 @@ module ActiveSupport
|
||||
utc.since(other).in_time_zone(time_zone)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def ago(other)
|
||||
since(-other)
|
||||
end
|
||||
@@ -218,7 +230,7 @@ module ActiveSupport
|
||||
utc.advance(options).in_time_zone(time_zone)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
%w(year mon month day mday hour min sec).each do |method_name|
|
||||
class_eval <<-EOV
|
||||
def #{method_name}
|
||||
@@ -226,45 +238,45 @@ module ActiveSupport
|
||||
end
|
||||
EOV
|
||||
end
|
||||
|
||||
|
||||
def usec
|
||||
time.respond_to?(:usec) ? time.usec : 0
|
||||
end
|
||||
|
||||
|
||||
def to_a
|
||||
[time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone]
|
||||
end
|
||||
|
||||
|
||||
def to_f
|
||||
utc.to_f
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def to_i
|
||||
utc.to_i
|
||||
end
|
||||
alias_method :hash, :to_i
|
||||
alias_method :tv_sec, :to_i
|
||||
|
||||
|
||||
# A TimeWithZone acts like a Time, so just return +self+.
|
||||
def to_time
|
||||
self
|
||||
end
|
||||
|
||||
|
||||
def to_datetime
|
||||
utc.to_datetime.new_offset(Rational(utc_offset, 86_400))
|
||||
end
|
||||
|
||||
|
||||
# So that +self+ <tt>acts_like?(:time)</tt>.
|
||||
def acts_like_time?
|
||||
true
|
||||
end
|
||||
|
||||
|
||||
# Say we're a Time to thwart type checking.
|
||||
def is_a?(klass)
|
||||
klass == ::Time || super
|
||||
end
|
||||
alias_method :kind_of?, :is_a?
|
||||
|
||||
|
||||
# Neuter freeze because freezing can cause problems with lazy loading of attributes.
|
||||
def freeze
|
||||
self
|
||||
@@ -273,7 +285,7 @@ module ActiveSupport
|
||||
def marshal_dump
|
||||
[utc, time_zone.name, time]
|
||||
end
|
||||
|
||||
|
||||
def marshal_load(variables)
|
||||
initialize(variables[0].utc, ::Time.__send__(:get_zone, variables[1]), variables[2].utc)
|
||||
end
|
||||
@@ -290,10 +302,10 @@ module ActiveSupport
|
||||
result = time.__send__(sym, *args, &block)
|
||||
result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
private
|
||||
def get_period_and_ensure_valid_local_time
|
||||
# we don't want a Time.local instance enforcing its own DST rules as well,
|
||||
# we don't want a Time.local instance enforcing its own DST rules as well,
|
||||
# so transfer time values to a utc constructor if necessary
|
||||
@time = transfer_time_values_to_utc_constructor(@time) unless @time.utc?
|
||||
begin
|
||||
@@ -304,11 +316,11 @@ module ActiveSupport
|
||||
retry
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def transfer_time_values_to_utc_constructor(time)
|
||||
::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, time.respond_to?(:usec) ? time.usec : 0)
|
||||
end
|
||||
|
||||
|
||||
def duration_of_variable_length?(obj)
|
||||
ActiveSupport::Duration === obj && obj.parts.flatten.detect {|p| [:years, :months, :days].include? p }
|
||||
end
|
||||
|
||||
@@ -210,6 +210,21 @@ class DateExtCalculationsTest < Test::Unit::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
uses_mocha 'past?, today? and future?' do
|
||||
def test_today_past_future
|
||||
Date.stubs(:current).returns(Date.civil(2000, 1, 1))
|
||||
t2 = Date.civil(2000, 1, 1)
|
||||
t1, t3 = t2.yesterday, t2.tomorrow
|
||||
t4, t5 = t2 - 1.second, t2 + 1.second
|
||||
|
||||
assert t1.past?
|
||||
assert t2.today?
|
||||
assert t3.future?
|
||||
assert t4.past?
|
||||
assert t5.today?
|
||||
end
|
||||
end
|
||||
|
||||
uses_mocha 'TestDateCurrent' do
|
||||
def test_current_returns_date_today_when_zone_default_not_set
|
||||
with_env_tz 'US/Central' do
|
||||
|
||||
@@ -207,6 +207,29 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
|
||||
assert_match(/^2080-02-28T15:15:10-06:?00$/, DateTime.civil(2080, 2, 28, 15, 15, 10, -0.25).xmlschema)
|
||||
end
|
||||
|
||||
uses_mocha 'past?, today? and future?' do
|
||||
def test_past_today_future
|
||||
Date.stubs(:current).returns(Date.civil(2000, 1, 1))
|
||||
DateTime.stubs(:current).returns(DateTime.civil(2000, 1, 1, 1, 0, 1))
|
||||
t1, t2 = DateTime.civil(2000, 1, 1, 1, 0, 0), DateTime.civil(2000, 1, 1, 1, 0, 2)
|
||||
|
||||
assert t1.past?
|
||||
assert t2.future?
|
||||
assert t1.today?
|
||||
assert t2.today?
|
||||
end
|
||||
end
|
||||
|
||||
def test_current_without_time_zone
|
||||
assert DateTime.current.is_a?(DateTime)
|
||||
end
|
||||
|
||||
def test_current_with_time_zone
|
||||
with_env_tz 'US/Eastern' do
|
||||
assert DateTime.current.is_a?(DateTime)
|
||||
end
|
||||
end
|
||||
|
||||
def test_acts_like_time
|
||||
assert DateTime.new.acts_like_time?
|
||||
end
|
||||
|
||||
@@ -563,6 +563,19 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
|
||||
assert_nothing_raised { Time.now.xmlschema }
|
||||
end
|
||||
|
||||
uses_mocha 'past?, today? and future?' do
|
||||
def test_past_today_future
|
||||
Date.stubs(:current).returns(Date.civil(2000, 1, 1))
|
||||
Time.stubs(:current).returns(Time.local(2000, 1, 1, 1, 0, 1))
|
||||
t1, t2 = Time.local(2000, 1, 1, 1, 0, 0), Time.local(2000, 1, 1, 1, 0, 2)
|
||||
|
||||
assert t1.past?
|
||||
assert t2.future?
|
||||
assert t1.today?
|
||||
assert t2.today?
|
||||
end
|
||||
end
|
||||
|
||||
def test_acts_like_time
|
||||
assert Time.new.acts_like_time?
|
||||
end
|
||||
@@ -640,24 +653,24 @@ class TimeExtMarshalingTest < Test::Unit::TestCase
|
||||
assert_equal t, unmarshaled
|
||||
assert_equal t.zone, unmarshaled.zone
|
||||
end
|
||||
|
||||
def test_marshaling_with_local_instance
|
||||
|
||||
def test_marshaling_with_local_instance
|
||||
t = Time.local(2000)
|
||||
marshaled = Marshal.dump t
|
||||
unmarshaled = Marshal.load marshaled
|
||||
assert_equal t, unmarshaled
|
||||
assert_equal t.zone, unmarshaled.zone
|
||||
end
|
||||
|
||||
def test_marshaling_with_frozen_utc_instance
|
||||
|
||||
def test_marshaling_with_frozen_utc_instance
|
||||
t = Time.utc(2000).freeze
|
||||
marshaled = Marshal.dump t
|
||||
unmarshaled = Marshal.load marshaled
|
||||
assert_equal t, unmarshaled
|
||||
assert_equal t.zone, unmarshaled.zone
|
||||
end
|
||||
|
||||
def test_marshaling_with_frozen_local_instance
|
||||
|
||||
def test_marshaling_with_frozen_local_instance
|
||||
t = Time.local(2000).freeze
|
||||
marshaled = Marshal.dump t
|
||||
unmarshaled = Marshal.load marshaled
|
||||
|
||||
@@ -152,6 +152,19 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
||||
assert_equal false, @twz.between?(Time.utc(2000,1,1,0,0,1), Time.utc(2000,1,1,0,0,2))
|
||||
end
|
||||
|
||||
uses_mocha 'past?, today? and future?' do
|
||||
def test_past_today_future
|
||||
Time.stubs(:current).returns(@twz.utc)
|
||||
Date.stubs(:current).returns(@twz.utc.to_date)
|
||||
t1, t2 = @twz - 1.second, @twz + 1.second
|
||||
|
||||
assert t1.past?
|
||||
assert t2.future?
|
||||
assert !t1.today?
|
||||
assert t2.today?
|
||||
end
|
||||
end
|
||||
|
||||
def test_eql?
|
||||
assert @twz.eql?(Time.utc(2000))
|
||||
assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) )
|
||||
@@ -538,7 +551,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
||||
assert_equal "Sun, 02 Apr 2006 10:30:01 EDT -04:00", twz.since(1.days + 1.second).inspect
|
||||
assert_equal "Sun, 02 Apr 2006 10:30:01 EDT -04:00", (twz + 1.days + 1.second).inspect
|
||||
end
|
||||
|
||||
|
||||
def test_advance_1_day_across_spring_dst_transition_backwards
|
||||
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,10,30))
|
||||
# In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
|
||||
@@ -548,7 +561,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
||||
assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 1.days).inspect
|
||||
assert_equal "Sat, 01 Apr 2006 10:30:01 EST -05:00", twz.ago(1.days - 1.second).inspect
|
||||
end
|
||||
|
||||
|
||||
def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_spring_dst_transition
|
||||
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30))
|
||||
# In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
|
||||
@@ -565,7 +578,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
||||
assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(24.hours).inspect
|
||||
assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.advance(:hours => 24).inspect
|
||||
end
|
||||
|
||||
|
||||
def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_spring_dst_transition_backwards
|
||||
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,11,30))
|
||||
# In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long
|
||||
@@ -582,7 +595,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
||||
assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(24.hours).inspect
|
||||
assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:hours => -24).inspect
|
||||
end
|
||||
|
||||
|
||||
def test_advance_1_day_across_fall_dst_transition
|
||||
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
|
||||
# In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
|
||||
@@ -593,7 +606,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
||||
assert_equal "Sun, 29 Oct 2006 10:30:01 EST -05:00", twz.since(1.days + 1.second).inspect
|
||||
assert_equal "Sun, 29 Oct 2006 10:30:01 EST -05:00", (twz + 1.days + 1.second).inspect
|
||||
end
|
||||
|
||||
|
||||
def test_advance_1_day_across_fall_dst_transition_backwards
|
||||
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,10,30))
|
||||
# In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
|
||||
@@ -603,7 +616,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
||||
assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.days).inspect
|
||||
assert_equal "Sat, 28 Oct 2006 10:30:01 EDT -04:00", twz.ago(1.days - 1.second).inspect
|
||||
end
|
||||
|
||||
|
||||
def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_fall_dst_transition
|
||||
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30))
|
||||
# In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
|
||||
@@ -620,7 +633,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
||||
assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(24.hours).inspect
|
||||
assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.advance(:hours => 24).inspect
|
||||
end
|
||||
|
||||
|
||||
def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_fall_dst_transition_backwards
|
||||
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,9,30))
|
||||
# In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long
|
||||
@@ -669,7 +682,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
||||
assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(1.month).inspect
|
||||
assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.month).inspect
|
||||
end
|
||||
|
||||
|
||||
def test_advance_1_year
|
||||
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2008,2,15,10,30))
|
||||
assert_equal "Sun, 15 Feb 2009 10:30:00 EST -05:00", twz.advance(:years => 1).inspect
|
||||
@@ -679,7 +692,7 @@ class TimeWithZoneTest < Test::Unit::TestCase
|
||||
assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", twz.years_ago(1).inspect
|
||||
assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", (twz - 1.year).inspect
|
||||
end
|
||||
|
||||
|
||||
def test_advance_1_year_during_dst
|
||||
twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2008,7,15,10,30))
|
||||
assert_equal "Wed, 15 Jul 2009 10:30:00 EDT -04:00", twz.advance(:years => 1).inspect
|
||||
|
||||
Reference in New Issue
Block a user