Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7524 (closed enhancement: incomplete)

Opened 3 years ago

Last modified 3 years ago

[PATCH] Added upto method to core_ext/time/calculations so that you could step through time

Reported by: jakev Assigned to: core
Priority: normal Milestone: 1.2.4
Component: ActiveSupport Version: edge
Severity: normal Keywords:
Cc:

Description

Added upto method to core_ext/time/calculations so thatyou could step through time with different step sizes.

The sizes are any time methods that works on Fixnum, i.e. :days, :years, :minutes, :seconds, :hours, :months.

This is my first patch and I dont see anywhere to attach the diff file, so i am going to just paste it below.

PATCH:


Index: activesupport/test/core_ext/time_ext_test.rb =================================================================== --- activesupport/test/core_ext/time_ext_test.rb (revision 6146) +++ activesupport/test/core_ext/time_ext_test.rb (working copy) @@ -293,7 +293,87 @@

midnight = Time.local(2005, 2, 21, 0, 0, 0) assert_equal midnight.midnight, (midnight + 1.hour + 0.000001).midnight

end

+ + + def test_upto_by_days + time = Time.local(2005, 2, 21, 17, 44, 30) + end_date = time + 10.days + counter = 0 + time.upto(end_date) do |date| + assert_equal date, (time + counter.days) + counter += 1 + end + + end + + def test_upto_by_months + time = Time.local(2005, 2, 21, 17, 44, 30) + end_date = time + 10.months + counter = 0 + time.upto(end_date, :month) do |date| + assert_equal date, (time + counter.months) + counter += 1 + end + + end + + def test_upto_by_years + time = Time.local(2005, 2, 21, 17, 44, 30) + end_date = time + 10.years + counter = 0 + time.upto(end_date, :year) do |date| + assert_equal date, (time + counter.years) + counter += 1 + end + + end + + def test_upto_by_seconds + time = Time.local(2005, 2, 21, 17, 44, 30) + end_date = time + 10.seconds + counter = 0 + time.upto(end_date, :seconds) do |date| + assert_equal date, (time + counter.seconds) + counter += 1 + end + + end + + def test_upto_by_hours + time = Time.local(2005, 2, 21, 17, 44, 30) + end_date = time + 10.hours + counter = 0 + time.upto(end_date, :hours) do |date| + assert_equal date, (time + counter.hours) + counter += 1 + end + + end + + def test_upto_by_minutes + time = Time.local(2005, 2, 21, 17, 44, 30) + end_date = time + 10.minutes + counter = 0 + time.upto(end_date, :minutes) do |date| + assert_equal date, (time + counter.minutes) + counter += 1 + end + + end + + def test_upto_with_bad_step_size + time = Time.local(2005, 2, 21, 17, 44, 30) + end_date = time + 10.minutes + assert_raise NoMethodError do + time.upto(end_date, :non_step_size) do |date| + puts date + end + end + + + end +

def test_days_in_month

assert_equal 31, Time.days_in_month(1, 2005)

Index: activesupport/lib/active_support/core_ext/time/calculations.rb =================================================================== --- activesupport/lib/active_support/core_ext/time/calculations.rb (revision 6146) +++ activesupport/lib/active_support/core_ext/time/calculations.rb (working copy) @@ -196,6 +196,23 @@

self.since(1.day)

end

+ + + # Convenience method to step though time in different increments + # step_sizes = :days, :months, :years, :seconds, :minutes, :hours + def upto(end_date, step_size = :days) + start_date = self + stepper = 1.send(step_size.to_s.pluralize.to_sym) + + while start_date < end_date + yield start_date + start_date += stepper + end + rescue NoMethodError => e + raise NoMethodError("Invalid Step Size") + + end +

def plus_with_duration(other) #:nodoc:

if ActiveSupport::Duration === other

other.since(self)

Attachments

added_upto_to_time_calculations.diff (3.3 kB) - added by jakev on 02/10/07 23:50:32.
diff file for patch

Change History

02/10/07 23:50:32 changed by jakev

  • attachment added_upto_to_time_calculations.diff added.

diff file for patch

02/24/07 18:50:15 changed by josh

  • keywords set to verified.

07/19/07 23:50:19 changed by mpalmer

  • keywords deleted.
  • status changed from new to closed.
  • resolution set to incomplete.

The patch doesn't apply against current edge. Needs rebasing.