我试过这样的方法:
a = ActiveSupport::TimeWithZone.new(Time.now,Time.zone)
b = a
a - b # it gives 0.0 (float)当我试着:
a.to_s # it gives "2019-06-30 11:11:42 -0700"
a.to_a # it gives [42, 11, 11, 30, 6, 2019, 0, 181, true, "PDT"]那么这个浮点数在哪里?
发布于 2019-06-30 16:57:12
Ruby的- (减法)方法来自Time类,您可以看到Time类中的TimeWithZone的Rails源代码。
def -(other)
if other.acts_like?(:time)
to_time - other.to_time
elsif duration_of_variable_length?(other)
method_missing(:-, other)
else
result = utc.acts_like?(:date) ? utc.ago(other) : utc - other rescue utc.ago(other)
result.in_time_zone(time_zone)
end
end根据Ruby文档,它返回一个浮点数。
差异-以秒为单位返回时间和other_time之间的浮点数,或者从时间中减去给定的秒数。
https://stackoverflow.com/questions/56826384
复制相似问题