mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Change Time#sec_fraction to use subsec
Time instances can have fractional parts smaller than a nanosecond.
This commit is contained in:
parent
dbc7a7cb50
commit
88d844b278
2 changed files with 16 additions and 2 deletions
|
@ -75,9 +75,9 @@ class Time
|
|||
|
||||
# Returns the fraction of a second as a +Rational+
|
||||
#
|
||||
# Time.new(2012, 8, 29, 0, 0, 0.5).sec_fraction # => (1/2)
|
||||
# Time.new(2012, 8, 29, 0, 0, 0.5).sec_fraction # => (1/2)
|
||||
def sec_fraction
|
||||
Rational(nsec, 1000000000)
|
||||
subsec
|
||||
end
|
||||
|
||||
# Returns a new Time where one or more of the elements have been changed according
|
||||
|
|
|
@ -107,6 +107,20 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_sec_fraction
|
||||
time = Time.utc(2016, 4, 23, 0, 0, Rational(1,10000000000))
|
||||
assert_equal Rational(1,10000000000), time.sec_fraction
|
||||
|
||||
time = Time.utc(2016, 4, 23, 0, 0, 0.0000000001)
|
||||
assert_equal 0.0000000001.to_r, time.sec_fraction
|
||||
|
||||
time = Time.utc(2016, 4, 23, 0, 0, 0, Rational(1,10000))
|
||||
assert_equal Rational(1,10000000000), time.sec_fraction
|
||||
|
||||
time = Time.utc(2016, 4, 23, 0, 0, 0, 0.0001)
|
||||
assert_equal 0.0001.to_r / 1000000, time.sec_fraction
|
||||
end
|
||||
|
||||
def test_beginning_of_day
|
||||
assert_equal Time.local(2005,2,4,0,0,0), Time.local(2005,2,4,10,10,10).beginning_of_day
|
||||
with_env_tz 'US/Eastern' do
|
||||
|
|
Loading…
Reference in a new issue