Ruby-on-rails 以最“红宝石”的风格计算 ActiveSupport:TimeWithZone 的天数差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3049941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate difference in days ActiveSupport:TimeWithZone in the most "rubyish" style?
提问by Nick
I have a feeling someone is going to point me to another question that answers this but I've been searching with no luck over this simple issue.
我有一种感觉,有人会给我指出另一个可以回答这个问题的问题,但我一直在寻找这个简单的问题,但一直没有运气。
I have a Activerecord with a datetime property. It returns as an ActiveSupport:TimeWithZone. I know I can't compare that to DateTime.now because that doesn't include a zone so I need to use Time.zone. Makes sense.
我有一个带有日期时间属性的 Activerecord。它作为 ActiveSupport:TimeWithZone 返回。我知道我无法将它与 DateTime.now 进行比较,因为它不包含区域,所以我需要使用 Time.zone。说得通。
What I'm wondering is stylewise is there a "cleaner" way to do this than subtracting and dividing the result by 86400?
我想知道的是,有没有比将结果减去和除以 86400 更“干净”的方法?
Here's what I do:
这是我所做的:
((Time.zone.now - myActiveRecord.visit_date)/86400).to_i
Works but seems un-rubyish and I feel like I'm missing something. Should I be casting, comparing or converting some other route or is this really the typical way to do this in rails? Appreciate any tips or a link to a question that already covers this.
有效,但似乎不是红宝石,我觉得我错过了一些东西。我应该投射、比较或转换其他路线,还是这真的是在 Rails 中执行此操作的典型方法?感谢任何提示或指向已经涵盖此内容的问题的链接。
Thank you
谢谢
回答by Jakub Hampl
One thing you can do to make it more readable is:
为了使其更具可读性,您可以做的一件事是:
((Time.zone.now - myActiveRecord.visit_date) / 1.day).to_i
Edit:
编辑:
Actually you can get rid of one set of the brackets with:
实际上,您可以通过以下方式摆脱一组括号:
(Time.zone.now - myActiveRecord.visit_date).to_i / 1.day
回答by Aaron
I know this question is a bit dated but I came across it while Googling for a similar problem. In my case I needed to know the difference in whole days on a macro and micro scale.
我知道这个问题有点过时,但我在谷歌搜索类似问题时遇到了它。就我而言,我需要在宏观和微观尺度上了解整日的差异。
For example, I needed my code to be able to tell me that Dec 31, 2010 is 366 days before Jan 1, 2012 and that Dec 31, 2010 23:59 is 1 day away from Jan 1, 2011 00:00. The method above works in the former but in the case of the latter, it says they are 0 days apart.
例如,我需要我的代码能够告诉我 2010 年 12 月 31 日是 2012 年 1 月 1 日之前的 366 天,而 2010 年 12 月 31 日 23:59 是 2011 年 1 月 1 日 00:00 之前的 1 天。上述方法适用于前者,但在后者的情况下,它表示它们相隔 0 天。
What I ended up doing was using Ruby's Dateclass to do the math for me. Using the code above my method looks like this:
我最终做的是使用 Ruby 的Date类为我做数学运算。使用上面的代码,我的方法如下所示:
(Time.zone.now.to_date - myActiveRecord.visit_date.to_date).to_i
(Time.zone.now.to_date - myActiveRecord.visit_date.to_date).to_i
This will work with inputs in Timeor DateTimedue to the conversion. Another possible solution would be to first call beginning_of_dayon each of the Timesor DateTimesbut in my case, the minutes were important.
这将适用于转换中Time或DateTime由于转换而产生的输入。另一种可能的解决办法是第一次调用beginning_of_day每个的Times或DateTimes,但在我的情况下,分是重要的。
回答by jamesconant
Rails actually has a method built in for just this sort of thing.
Rails 实际上内置了一个方法来处理这种事情。
checkout #time_ago_in_words
So, for the original case...
所以,对于原案...
((Time.zone.now - myActiveRecord.visit_date)/86400).to_i
((Time.zone.now - myActiveRecord.visit_date)/86400).to_i
vs
对比
time_ago_in_words(myActiveRecord.visit_date)
time_ago_in_words(myActiveRecord.visit_date)

