首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ActiveSupport TimeWithZone如何保留值?

ActiveSupport TimeWithZone如何保留值?
EN

Stack Overflow用户
提问于 2019-06-30 16:23:53
回答 1查看 302关注 0票数 1

我试过这样的方法:

代码语言:javascript
运行
复制
a = ActiveSupport::TimeWithZone.new(Time.now,Time.zone)
b = a
a - b  # it gives 0.0 (float)

当我试着:

代码语言:javascript
运行
复制
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"]

那么这个浮点数在哪里?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-30 16:57:12

Ruby的- (减法)方法来自Time类,您可以看到Time类中的TimeWithZone的Rails源代码

代码语言:javascript
运行
复制
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之间的浮点数,或者从时间中减去给定的秒数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56826384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档