Skip to content

Commit

Permalink
Merge [5388] from trunk.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-pre-release@5389 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Nov 2, 2006
1 parent 229e197 commit ce0653b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion activesupport/CHANGELOG
Expand Up @@ -2,7 +2,7 @@


* Update dependencies to allow constants to be defined alongside their siblings. A common case for this is AR model classes with STI; user.rb might define User, Administrator and Guest for example. [Nicholas Seckar] * Update dependencies to allow constants to be defined alongside their siblings. A common case for this is AR model classes with STI; user.rb might define User, Administrator and Guest for example. [Nicholas Seckar]


* next_week respects DST changes. #6483 [marclove] * next_week respects DST changes. #6483, #5617, #2353, #2509, #4551 [marclove, rabiedenharn, rails@roetzel.de, jsolson@damogran.org, drbrain@segment7.net]


* Expose methods added to Enumerable in the documentation, such as group_by. Closes #6170. [sergeykojin@gmail.com, Marcel Molina Jr.] * Expose methods added to Enumerable in the documentation, such as group_by. Closes #6170. [sergeykojin@gmail.com, Marcel Molina Jr.]


Expand Down
13 changes: 7 additions & 6 deletions activesupport/lib/active_support/core_ext/time/calculations.rb
Expand Up @@ -25,7 +25,7 @@ def days_in_month(month, year=nil)


# Seconds since midnight: Time.now.seconds_since_midnight # Seconds since midnight: Time.now.seconds_since_midnight
def seconds_since_midnight def seconds_since_midnight
self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6) self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
end end


# Returns a new Time where one or more of the elements have been changed according to the +options+ parameter. The time options # Returns a new Time where one or more of the elements have been changed according to the +options+ parameter. The time options
Expand Down Expand Up @@ -56,13 +56,16 @@ def advance(options)
# Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
# Do not use this method in combination with x.months, use months_ago instead! # Do not use this method in combination with x.months, use months_ago instead!
def ago(seconds) def ago(seconds)
seconds.until(self) self.since(-seconds)
end end


# Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around # Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around
#the Numeric extension. Do not use this method in combination with x.months, use months_since instead! #the Numeric extension. Do not use this method in combination with x.months, use months_since instead!
def since(seconds) def since(seconds)
seconds.since(self) initial_dst = self.dst? ? 1 : 0
f = seconds.since(self)
final_dst = f.dst? ? 1 : 0
(seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
end end
alias :in :since alias :in :since


Expand Down Expand Up @@ -135,9 +138,7 @@ def beginning_of_week
# Returns a new Time representing the start of the given day in next week (default is Monday). # Returns a new Time representing the start of the given day in next week (default is Monday).
def next_week(day = :monday) def next_week(day = :monday)
days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
# Adjust in case of switches to or from daylight savings time since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
week_from_today = self.since(1.week) + (self.since(1.week) <=> self).hour
week_from_today.beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
end end


# Returns a new Time representing the start of the day (0:00) # Returns a new Time representing the start of the day (0:00)
Expand Down
50 changes: 50 additions & 0 deletions activesupport/test/core_ext/time_ext_test.rb
Expand Up @@ -9,6 +9,24 @@ def test_seconds_since_midnight
assert_equal 60.00001,Time.local(2005,1,1,0,1,0,10).seconds_since_midnight assert_equal 60.00001,Time.local(2005,1,1,0,1,0,10).seconds_since_midnight
end end


def test_seconds_since_midnight_at_daylight_savings_time_start
# dt: US: 2005 April 3rd 2:00am ST => April 3rd 3:00am DT
assert_equal 3600+59*60+59, Time.local(2005,4,3,1,59,59).seconds_since_midnight, 'just before DST start'
assert_equal 3600+59*60+59+2,Time.local(2005,4,3,3, 0, 1).seconds_since_midnight, 'just after DST start'
end

def test_seconds_since_midnight_at_daylight_savings_time_end
# st: US: 2005 October 30th 2:00am DT => October 30th 1:00am ST
# avoid setting a time between 1:00 and 2:00 since that requires specifying whether DST is active
assert_equal 3599, Time.local(2005,10,30,0,59,59).seconds_since_midnight, 'just before DST end'
assert_equal 3*3600+1, Time.local(2005,10,30,2, 0, 1).seconds_since_midnight, 'just after DST end'

# now set a time between 1:00 and 2:00 by specifying whether DST is active
# uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
assert_equal 1*3600+30*60, Time.local(0,30,1,30,10,2005,0,0,true,'EST5EDT').seconds_since_midnight, 'before DST end'
assert_equal 2*3600+30*60, Time.local(0,30,1,30,10,2005,0,0,false,'EST5EDT').seconds_since_midnight, 'after DST end'
end

def test_begining_of_week def test_begining_of_week
assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week
assert_equal Time.local(2005,11,28), Time.local(2005,11,28,0,0,0).beginning_of_week #monday assert_equal Time.local(2005,11,28), Time.local(2005,11,28,0,0,0).beginning_of_week #monday
Expand Down Expand Up @@ -88,13 +106,37 @@ def test_ago
assert_equal Time.local(2005,2,20,9,9,45), Time.local(2005,2,22,10,10,10).ago(86400*2 + 3600 + 25) assert_equal Time.local(2005,2,20,9,9,45), Time.local(2005,2,22,10,10,10).ago(86400*2 + 3600 + 25)
end end


def test_daylight_savings_time_crossings_backward_start
# dt: US: 2005 April 3rd 4:18am
assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-1.day=>st'
assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-1.day=>st'
end

def test_daylight_savings_time_crossings_backward_end
# st: US: 2005 October 30th 4:03am
assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-1.day=>dt'
assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-1.day=>dt'
end

def test_since def test_since
assert_equal Time.local(2005,2,22,10,10,11), Time.local(2005,2,22,10,10,10).since(1) assert_equal Time.local(2005,2,22,10,10,11), Time.local(2005,2,22,10,10,10).since(1)
assert_equal Time.local(2005,2,22,11,10,10), Time.local(2005,2,22,10,10,10).since(3600) assert_equal Time.local(2005,2,22,11,10,10), Time.local(2005,2,22,10,10,10).since(3600)
assert_equal Time.local(2005,2,24,10,10,10), Time.local(2005,2,22,10,10,10).since(86400*2) assert_equal Time.local(2005,2,24,10,10,10), Time.local(2005,2,22,10,10,10).since(86400*2)
assert_equal Time.local(2005,2,24,11,10,35), Time.local(2005,2,22,10,10,10).since(86400*2 + 3600 + 25) assert_equal Time.local(2005,2,24,11,10,35), Time.local(2005,2,22,10,10,10).since(86400*2 + 3600 + 25)
end end


def test_daylight_savings_time_crossings_forward_start
# st: US: 2005 April 2nd 7:27pm
assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+1.day=>dt'
assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+1.day=>dt'
end

def test_daylight_savings_time_crossings_forward_end
# dt: US: 2005 October 30th 12:45am
assert_equal Time.local(2005,10,31,1,45,0), Time.local(2005,10,30,1,45,0).since(86400), 'dt+1.day=>st'
assert_equal Time.local(2005,11, 1,1,45,0), Time.local(2005,10,31,1,45,0).since(86400), 'st+1.day=>st'
end

def test_yesterday def test_yesterday
assert_equal Time.local(2005,2,21,10,10,10), Time.local(2005,2,22,10,10,10).yesterday assert_equal Time.local(2005,2,21,10,10,10), Time.local(2005,2,22,10,10,10).yesterday
assert_equal Time.local(2005,2,28,10,10,10), Time.local(2005,3,2,10,10,10).yesterday.yesterday assert_equal Time.local(2005,2,28,10,10,10), Time.local(2005,3,2,10,10,10).yesterday.yesterday
Expand Down Expand Up @@ -150,6 +192,14 @@ def test_next_week
assert_equal Time.local(2006,11,1), Time.local(2006,10,23,0,0,0).next_week(:wednesday) assert_equal Time.local(2006,11,1), Time.local(2006,10,23,0,0,0).next_week(:wednesday)
end end


def test_next_week_near_daylight_start
assert_equal Time.local(2006,4,3), Time.local(2006,4,2,23,1,0).next_week, 'just crossed standard => daylight'
end

def test_next_week_near_daylight_end
assert_equal Time.local(2006,10,30), Time.local(2006,10,29,23,1,0).next_week, 'just crossed daylight => standard'
end

def test_to_s def test_to_s
time = Time.local(2005, 2, 21, 17, 44, 30) time = Time.local(2005, 2, 21, 17, 44, 30)
assert_equal "2005-02-21 17:44:30", time.to_s(:db) assert_equal "2005-02-21 17:44:30", time.to_s(:db)
Expand Down

0 comments on commit ce0653b

Please sign in to comment.