【问题标题】:More precise distance_of_time_in_words更精确的 distance_of_time_in_words
【发布时间】:2010-11-16 12:18:54
【问题描述】:

distance_of_time_in_words 很棒,但有时不够精细。

我需要一个能够以文字形式报告确切时间距离的函数。例如,上午 7:50 到 10:10 应该是“2 小时 20 分钟”的距离,而不是“大约 2 小时”或distance_of_time_in_words 会做的任何事情。

我的用例是报告火车时刻表,特别是给定火车需要多长时间。

【问题讨论】:

标签: ruby-on-rails time


【解决方案1】:
def distance_of_time_in_hours_and_minutes(from_time, to_time)
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
  distance_in_hours   = (((to_time - from_time).abs) / 3600).floor
  distance_in_minutes = ((((to_time - from_time).abs) % 3600) / 60).round

  difference_in_words = ''

  difference_in_words << "#{distance_in_hours} #{distance_in_hours > 1 ? 'hours' : 'hour' } and " if distance_in_hours > 0
  difference_in_words << "#{distance_in_minutes} #{distance_in_minutes == 1 ? 'minute' : 'minutes' }"
end

【讨论】:

  • 我会将 distance_in_hours = (((to_time - from_time).abs) / 3600).round 更改为 distance_in_hours = (((to_time - from_time).abs) / 3600).to_i 这一轮将四舍五入,使 1.5 小时看起来像 2 小时 30 分钟
  • 我建议使用(((to_time - from_time).abs) / 3600).floor 而不是(((to_time - from_time).abs) / 3600).round,因为有时它会显示“2 小时 50 分钟”,而时间之间有 1 小时和 50 分钟的差异,
【解决方案2】:

上面的代码让我的 Rubymine 感到困惑……我不确定它是否真的正确。我重建它如下:


def distance_of_time_in_hours_and_minutes(from_time, to_time)
  from_time = from_time.to_time if from_time.respond_to?(:to_time)
  to_time = to_time.to_time if to_time.respond_to?(:to_time)
  dist = to_time - from_time
  minutes = (dist.abs / 60).round
  hours = minutes / 60
  minutes = minutes - (hours * 60)

  words = dist <= 0 ? '' : '-'

  words << "#{hours} #{hours > 1 ? 'hours' : 'hour' } and " if hours > 0
  words << "#{minutes} #{minutes == 1 ? 'minute' : 'minutes' }"
end

这是上面的一些 Rspec:


describe "Distance of Time in Hours and Minutes" do
  before do
    @time1 = Time.utc(2010,"sep",7,14,15,3)
    @time2 = @time1 + 28
    @time3 = @time1 + 30
    @time4 = @time1 + 60
    @time5 = @time1 + 60*60
    @time6 = @time1 + 60*60 + 60
    @time7 = @time1 + 60*60 + 5*60
  end

  it "calculates time differences properly" do
    distance_of_time_in_hours_and_minutes(@time1, @time1).should == "0 minutes"
    distance_of_time_in_hours_and_minutes(@time1, @time2).should == "0 minutes"
    distance_of_time_in_hours_and_minutes(@time1, @time3).should == "1 minute"
    distance_of_time_in_hours_and_minutes(@time1, @time4).should == "1 minute"
    distance_of_time_in_hours_and_minutes(@time1, @time5).should == "1 hour and 0 minutes"
    distance_of_time_in_hours_and_minutes(@time1, @time6).should == "1 hour and 1 minute"
    distance_of_time_in_hours_and_minutes(@time1, @time7).should == "1 hour and 5 minutes"
    distance_of_time_in_hours_and_minutes(@time7, @time1).should == "-1 hour and 5 minutes"
    distance_of_time_in_hours_and_minutes(@time3, @time1).should == "-1 minute"
  end
end

(请注意,在我的编码环境中,我在每次调用 distance_of_time_in_miles 之前都使用了 helper,以避免出现 NoMethodError 错误——例如,请参阅 http://old.nabble.com/How-to-spec-a-Rails-helper-method-td20660744.html 了解更多信息。

【讨论】:

  • 这行“words = dist 应该返回它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多