计算两次之间的距离

时间:2009-08-06 04:35:37

标签: ruby-on-rails

我正在努力为“铁路”制作一个“更好”的distance_of_time_in_words,到目前为止,我有这个:http://github.com/radar/dotiw

然而,正如规格所示,目前它在几个月+几年内窒息。我完全失败了!帮助

Pax添加的代码,所以没有人必须搜索github:

module ActionView
  module Helpers
    module DateHelper
      def distance_of_time_in_words_hash(from_time, to_time)
        output = HashWithIndifferentAccess.new
        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 = from_time - to_time

        # Get a positive number.
        distance *= -1 if distance < 0

        while distance > 0
          if distance > 100000000000000
          elsif distance >= 31449600
            output[I18n.t(:years, :default => "years")], distance = 
              distance.divmod(31449600)
          elsif distance >= 2419200
            output[I18n.t(:months, :default => "months")], distance = 
              distance.divmod(2419200)
          elsif distance >= 604800
            output[I18n.t(:weeks, :default => "weeks")], distance = 
              distance.divmod(604800)
          elsif distance >= 86400
            output[I18n.t(:days, :default => "days")], distance =
              distance.divmod(86400)
          elsif distance >= 3600
            output[I18n.t(:hours, :default => "hours")], distance = 
              distance.divmod(3600)
          elsif distance >= 60
            output[I18n.t(:minutes, :default => "minutes")], distance = 
              distance.divmod(60)
          else
            output[I18n.t(:seconds, :default => "seconds")] = distance.to_i
            distance = 0
          end
        end
        output
      end

      def distance_of_time_in_words(from_time, to_time, include_seconds = false, 
          options = {}, output_options = {})

        hash = distance_of_time_in_words_hash(from_time, to_time)
        hash.delete(:seconds) if !include_seconds

        # Remove all the values that are nil.
        time_measurements = [
          I18n.t(:years, :default => "years"),
          I18n.t(:months, :default => "months"),
          I18n.t(:weeks, :default => "weeks"),
          I18n.t(:days, :default => "days"),
          I18n.t(:hours, :default => "hours"),
          I18n.t(:minutes, :default => "minutes"),
          I18n.t(:seconds, :default => "seconds")].delete_if do |key|
            hash[key].nil?
        end

        output = []
        time_measurements.each do |key|
          name = hash[key] > 1 ? key : key.singularize
          output += ["#{hash[key]} #{name}"]
        end

        output.to_sentence(output_options)
      end
    end
  end
end

2 个答案:

答案 0 :(得分:2)

我们也完全失败了。也许你可以通过以下方式让我们的更容易生活:

  • (1)发布代码(不是那么大)[忘了这个,我已经为你做了]。
  • (2)告诉我们完全正确的行为应该是什么(所需的输出)。
  • (3)告诉我们你得到的行为(样本实际输出)。

那么也许我们能够做得更好。

我直接注意到的一件事:除了二月之外的任何一个月都有 2,419,200秒(然后仅在非闰年)。同样,没有一年有364天。

我认为您需要重新考虑如何计算间隔。

假设您的“单词”版本的持续时间可以处理一点不准确,至少应该使用这些数字的平均值(考虑到一年是365.2425天,应该足够接近您的目的):

  • 一年是31,556,952秒。
  • 一个月是2,629,746秒(简单除以12)。

答案 1 :(得分:2)

看起来你正试图在所有适用的单位(即“2年,4个月,7天”而不是“2年”Rails'distance_of_time_in_words给出的)中获得时差。 ?

如果是这样,请查看In Rails, display time between two dates in English,另一个关于显示时差的问题。我在那里的答案应该为你似乎想要做的事情提供一个良好的起点,并且将方法扩展到包括小时,分钟和秒都不难。

相关问题