ChatGPT解决这个技术问题 Extra ChatGPT

你如何在 Rails 中处理相对时间?

我正在编写一个 Rails 应用程序,但似乎找不到如何计算相对时间,即如果给定某个 Time 类,它可以计算“30 秒前”或“2 天前”或者如果它超过一个月“2008 年 9 月 1 日”等

“计算”似乎是错误的词,您的意思是“输出”吗?

A
Andrew Marshall

听起来您正在寻找来自 ActiveSupport 的 time_ago_in_words 方法(或 distance_of_time_in_words)。像这样称呼它:

<%= time_ago_in_words(timestamp) %>

给红宝石新手如何在 irb 中运行它的提示?我运行: require 'active_support' 然后尝试 'time_ago_in_words(Time.now)' 但找不到该功能。
@cboettig 它不是 ActiveSupport 的一部分,它在 ActionView::Helpers::DateHelper 中定义。
如果您在 Rails 控制台中,则不需要“active_support”。有一个名为 helper 的控制台方法,您可以调用和访问这些方法。 helper.time_ago_in_words(timestamp)
仅供参考,如果您想在 irb 或普通 Ruby 中使用它,只需执行以下操作:require 'active_support/core_ext' 首先,然后您就可以使用 7.days.ago 和其他类似的结构了。
伟大的!我刚来这里期待一些示例代码并找到了这个。我喜欢 ruby 和 rails
m
maček

我已经写了这个,但是必须检查提到的现有方法,看看它们是否更好。

module PrettyDate
  def to_pretty
    a = (Time.now-self).to_i

    case a
      when 0 then 'just now'
      when 1 then 'a second ago'
      when 2..59 then a.to_s+' seconds ago' 
      when 60..119 then 'a minute ago' #120 = 2 minutes
      when 120..3540 then (a/60).to_i.to_s+' minutes ago'
      when 3541..7100 then 'an hour ago' # 3600 = 1 hour
      when 7101..82800 then ((a+99)/3600).to_i.to_s+' hours ago' 
      when 82801..172000 then 'a day ago' # 86400 = 1 day
      when 172001..518400 then ((a+800)/(60*60*24)).to_i.to_s+' days ago'
      when 518400..1036800 then 'a week ago'
      else ((a+180000)/(60*60*24*7)).to_i.to_s+' weeks ago'
    end
  end
end

Time.send :include, PrettyDate

嗯,我读了第一个 ruby,并且 ruby 没有开箱即用的 ActiveSupport。所以我认为这篇文章更红宝石。然而,在实际问题中提到了导轨。
Rails 是一个 web 框架,底层语言是 ruby。因此,在 rails 问题上获得一些 ruby 代码是很正常的。即使标准的 ruby 不包含 ActiveRecord,也只需要一个“gem install activerecord”命令就可以支持它。
您可以将 case 语句中的最后 return 行放在 else 下,并删除所有 return
有人能说出为什么有 +99 +800 +180000 吗?
+99 因为“一小时前”的结尾在 7100 处结束,比两小时还短 100 秒。同样,+800,因为 172000 比两天短 800 秒(172800 秒),依此类推。
s
seo

只是为了澄清 Andrew Marshall 使用 time_ago_in_words 的解决方案(对于 Rails 3.0 和 Rails 4.0)

如果你在视野中

<%= time_ago_in_words(Date.today - 1) %>

如果您在控制器中

include ActionView::Helpers::DateHelper
def index
  @sexy_date = time_ago_in_words(Date.today - 1)
end

默认情况下,控制器没有导入模块 ActionView::Helpers::DateHelper

注意将助手导入控制器不是“轨道方式”。助手用于帮助视图。 time_ago_in_words 方法被决定为 MVC 三元组中的视图实体。 (我不同意,但在罗马时......)


H
Honza

关于什么

30.seconds.ago
2.days.ago

还是你正在拍摄的其他东西?


这与问题所说的相反。
T
TonyLa

您可以使用算术运算符来计算相对时间。

Time.now - 2.days 

2天前给你。


你知道 2.days.ago 吗?
`Time.now + 2.days' 有速记方法吗?
@NixNinja:2.days.since2.days.from_now
这与问题所问的相反。 time_ago_in_words 是问题的答案。
F
Femaref

像这样的东西会起作用。

def relative_time(start_time)
  diff_seconds = Time.now - start_time
  case diff_seconds
    when 0 .. 59
      puts "#{diff_seconds} seconds ago"
    when 60 .. (3600-1)
      puts "#{diff_seconds/60} minutes ago"
    when 3600 .. (3600*24-1)
      puts "#{diff_seconds/3600} hours ago"
    when (3600*24) .. (3600*24*30) 
      puts "#{diff_seconds/(3600*24)} days ago"
    else
      puts start_time.strftime("%m/%d/%Y")
  end
end

这本质上是 distance_of_time_in_words 助手的重新实现:api.rubyonrails.com/classes/ActionView/Helpers/…
这意味着你不应该使用它,你应该使用内置的
这对我来说似乎很好。它允许您自定义其余的字符串。即“3 秒前”与“3 秒前”
#{diff_seconds/360} 应该是 #{diff_seconds/3600}。我已经编辑过了。
L
Lee Dykes

由于这里的大多数答案建议 time_ago_in_words。

而不是使用:

<%= time_ago_in_words(comment.created_at) %>

在 Rails 中,更喜欢:

<abbr class="timeago" title="<%= comment.created_at.getutc.iso8601 %>">
  <%= comment.created_at.to_s %>
</abbr>

连同一个 jQuery 库 http://timeago.yarp.com/,带有代码:

$("abbr.timeago").timeago();

主要优势:缓存

http://rails-bestpractices.com/posts/2012/02/10/not-use-time_ago_in_words/


实际上,现在是 time_ago_in_words 而不是时间S
d
davogones

看看这里的实例方法:

http://apidock.com/rails/Time

这有一些有用的方法,例如昨天、明天、开始的一周、前等。

例子:

Time.now.yesterday
Time.now.ago(2.days).end_of_day
Time.now.next_month.beginning_of_month

Z
Zack Xu

如果你正在构建一个 Rails 应用程序,你应该使用

Time.zone.now
Time.zone.today
Time.zone.yesterday

这会在您配置 Rails 应用程序的时区中为您提供时间或日期。

例如,如果您将应用程序配置为使用 UTC,则 Time.zone.now 将始终采用 UTC 时间(例如不受英国夏令时变化的影响)。

计算相对时间很容易,例如

Time.zone.now - 10.minute
Time.zone.today.days_ago(5)

B
Brett Shollenberger

我已经为 Rails ActiveRecord 对象编写了一个 gem。该示例使用 created_at,但它也适用于 updated_at 或具有 ActiveSupport::TimeWithZone 类的任何内容。

只需 gem install 并在您的 TimeWithZone 实例上调用“漂亮”方法。

https://github.com/brettshollenberger/hublot


T
The Whiz of Oz

另一种方法是从后端卸载一些逻辑,并使用 Javascript 插件让浏览器完成这项工作,例如:

jQuery time ago 或其 Rails Gem adaptation