Preserve fractional seconds in DateTime#to_time
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
This commit is contained in:
parent
c1c6f29214
commit
bf0395837f
@ -66,7 +66,7 @@ def to_date
|
||||
# Attempts to convert self to a Ruby Time object; returns self if out of range of Ruby Time class
|
||||
# If self has an offset other than 0, self will just be returned unaltered, since there's no clean way to map it to a Time
|
||||
def to_time
|
||||
self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec) : self
|
||||
self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec, sec_fraction * (RUBY_VERSION < '1.9' ? 86400000000 : 1000000)) : self
|
||||
end
|
||||
|
||||
# To be able to keep Times, Dates and DateTimes interchangeable on conversions
|
||||
|
@ -38,6 +38,8 @@ def test_to_time
|
||||
assert_equal Time.utc_time(2039, 2, 21, 10, 11, 12), DateTime.new(2039, 2, 21, 10, 11, 12, 0, 0).to_time
|
||||
# DateTimes with offsets other than 0 are returned unaltered
|
||||
assert_equal DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24)), DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).to_time
|
||||
# Fractional seconds are preserved
|
||||
assert_equal Time.utc(2005, 2, 21, 10, 11, 12, 256), DateTime.new(2005, 2, 21, 10, 11, 12 + Rational(256, 1000000), 0).to_time
|
||||
end
|
||||
|
||||
def test_civil_from_format
|
||||
|
Loading…
Reference in New Issue
Block a user