将 BigDecimal 小时数添加到日期时间错误 1 秒



这是随着ActiveSupport 6的更新而发生的

start_time = DateTime.now.beginning_of_day
start_time + BigDecimal(2).hours #=>  Wed, 11 Sep 2019 01:59:59 +0000

奇怪的是,这与时间一起工作得很好

start_time = Time.now.beginning_of_day
start_time + BigDecimal(2).hours #=>  2019-09-11 02:00:00 +0000

谁能解释为什么?

最终,它归结为ActiveSupport在内部执行的一些数学中的浮点错误。

请注意,使用 Rational 而不是 BigDecimal 可以:

DateTime.now.beginning_of_day + Rational(2, 1).hours
# => Mon, 02 Dec 2019 02:00:00 -0800
Time.now.beginning_of_day + Rational(2, 1).hours
# => 2019-12-02 02:00:00 -0800

以下是Time/DateTime/ActiveSupport的相关代码:

class DateTime
def since(seconds)
self + Rational(seconds, 86400)
end
def plus_with_duration(other) #:nodoc:
if ActiveSupport::Duration === other
other.since(self)
else
plus_without_duration(other)
end
end
end
class Time
def since(seconds)
self + seconds
rescue
to_datetime.since(seconds)
end
def plus_with_duration(other) #:nodoc:
if ActiveSupport::Duration === other
other.since(self)
else
plus_without_duration(other)
end
end
def advance(options)
unless options[:weeks].nil?
options[:weeks], partial_weeks = options[:weeks].divmod(1)
options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
end
unless options[:days].nil?
options[:days], partial_days = options[:days].divmod(1)
options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
end
d = to_date.gregorian.advance(options)
time_advanced_by_date = change(year: d.year, month: d.month, day: d.day)
seconds_to_advance = 
options.fetch(:seconds, 0) +
options.fetch(:minutes, 0) * 60 +
options.fetch(:hours, 0) * 3600
if seconds_to_advance.zero?
time_advanced_by_date
else
time_advanced_by_date.since(seconds_to_advance)
end
end
end
class ActiveSupport::Duration
def since(time = ::Time.current)
sum(1, time)
end
def sum(sign, time = ::Time.current)
parts.inject(time) do |t, (type, number)|
if t.acts_like?(:time) || t.acts_like?(:date)
if type == :seconds
t.since(sign * number)
elsif type == :minutes
t.since(sign * number * 60)
elsif type == :hours
t.since(sign * number * 3600)
else
t.advance(type => sign * number)
end
else
raise ::ArgumentError, "expected a time or date, got #{time.inspect}"
end
end
end
end

在您的案例中发生的事情在t.since(sign * number * 3600)行上,numberBigDecimal(2),而日期时间Rational(seconds, 86400)。因此,使用日期时间时的整个表达式是Rational(1 * BigDecimal(2) * 3600, 86400).

由于 BigDecimal 用作 Rational 的参数,因此结果根本不是理性的:

Rational(1 * BigDecimal(2) * 3600, 86400)
# => 0.83333333333333333e-1 # Since there's no obvious way to coerce a BigDecimal into a Rational, this returns a BigDecimal
Rational(1 * 2 * 3600, 86400)
# => (1/12)                 # A rational, as expected

此值使其返回到时间#前进。以下是它所做的计算结果:

options[:days], partial_days = options[:days].divmod(1)
# => [0.0, 0.83333333333333333e-1] # 0 days, 2 hours
options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
# => 0.1999999999999999992e1 # juuuust under 2 hours.

最后,0.199999999999999992e1 * 3600 = 7199.9999999999999712,当它最终转换回时间/日期时间时,它会被打底。

时间不会发生这种情况,因为时间永远不需要将持续时间的值传递给 Rational。

我认为这不应该被视为一个错误,因为如果您要传递 BigDecimal ,那么您应该期望代码如何处理您的数据:作为带有小数部分的数字,而不是比率。也就是说,当您使用 BigDecimal 时,您会面临浮点错误。

它偏离了一秒,而不是毫秒。为什么不使用2.hours而不是BigDecimal(2).hours

最新更新