Added the ,largest)interval method to Duration. Minor cleanup.

This commit is contained in:
Peter Camilleri 2015-07-09 23:26:11 -04:00
parent 712fef0bbc
commit 9d69257e96
3 changed files with 51 additions and 10 deletions

View file

@ -29,14 +29,14 @@ class TimeLibraryTester < Minitest::Test
foorth_equal('local_offset', [Time.now.utc_offset])
foorth_equal('Duration .intervals',
[[31556952.0, 2629746.0, 86400, 3600, 60, 1]])
[[31_556_952, 2_629_746, 86_400, 3_600, 60, 1]])
foorth_equal('Duration .sec_per_year', [31556952.0])
foorth_equal('Duration .sec_per_month', [2629746.0])
foorth_equal('Duration .sec_per_day', [86400])
foorth_equal('Duration .sec_per_hour', [3600])
foorth_equal('Duration .sec_per_min', [60])
foorth_equal('Duration .sec_per_sec', [1])
foorth_equal('Duration .sec_per_year', [31_556_952])
foorth_equal('Duration .sec_per_month', [ 2_629_746])
foorth_equal('Duration .sec_per_day', [ 86_400])
foorth_equal('Duration .sec_per_hour', [ 3_600])
foorth_equal('Duration .sec_per_min', [ 60])
foorth_equal('Duration .sec_per_sec', [ 1])
end
def test_converting_to_time
@ -342,4 +342,29 @@ class TimeLibraryTester < Minitest::Test
foorth_raises('"Someday June 14 at 06:50 PM" Time "%A %B %d at %I:%M %p" parse!')
end
def test_some_duration_formatting_and_support
foorth_equal('0 .to_d .largest_interval', [5])
foorth_equal('0.1 .to_d .largest_interval', [5])
foorth_equal('1 .to_d .largest_interval', [5])
foorth_equal('59 .to_d .largest_interval', [5])
foorth_equal('60 .to_d .largest_interval', [4])
foorth_equal('60.02 .to_d .largest_interval', [4])
foorth_equal('120.02 .to_d .largest_interval', [4])
foorth_equal('3120.02 .to_d .largest_interval', [4])
foorth_equal('3599.99 .to_d .largest_interval', [4])
foorth_equal('3600 .to_d .largest_interval', [3])
foorth_equal('86399 .to_d .largest_interval', [3])
foorth_equal('86400 .to_d .largest_interval', [2])
foorth_equal('2629746 .to_d .largest_interval', [1])
foorth_equal('31556952 .to_d .largest_interval', [0])
end
end

View file

@ -37,4 +37,4 @@ module XfOOrth
# [a_time] f"string" [a_string]
Duration.create_shared_method('f"', NosSpec, [], &format_action)
end
end

View file

@ -10,13 +10,21 @@ module XfOOrth
A_Minute = 60 * A_Second
An_Hour = 60 * A_Minute
A_Day = 24 * An_Hour
A_Month = (365.2425/12.0) * A_Day
A_Year = 365.2425 * A_Day
A_Month = Rational(365_2425, 120000) * A_Day
A_Year = Rational(365_2425, 10000) * A_Day
Intervals = [A_Year, A_Month, A_Day, An_Hour, A_Minute, A_Second]
#Find the largest interval for this duration
def largest_interval
(0..5).detect {|idx| Intervals[idx] <= period} || 5
end
end
#Methods to retrieve interval values from the Duration class.
Duration.create_exclusive_method('.intervals', TosSpec, [], &lambda {|vm|
vm.push(Duration::Intervals)
})
@ -45,4 +53,12 @@ module XfOOrth
vm.push(Duration::A_Second)
})
#Methods to deal with intervals.
#[a_duration] .largest_interval [an_index(0..5)]
Duration.create_shared_method('.largest_interval', TosSpec, [], &lambda {|vm|
vm.push(self.largest_interval)
})
end