【问题标题】:Calculating the distance between two times计算两次之间的距离
【发布时间】:2009-08-06 04:35:36
【问题描述】:

我正在尝试为 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

【问题讨论】:

  • 这不应该是“差异”而不是“距离”吗?也许这是我不明白的一些时空问题:)

标签: ruby-on-rails


【解决方案1】:

看起来您正在尝试获取所有适用单位的时差(即“2 年、4 个月、7 天”而不是 Rails 的distance_of_time_in_words 给出的“2 年”)?

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

【讨论】:

  • 与我已经拥有的非常相似,但仍然无法产生正确的结果,但我更喜欢您使用数字助手而不是“幻数”的方法
【解决方案2】:

我们也完全不知所措。也许您可以通过以下方式让我们的生活更轻松:

  • (1) 贴出代码(没那么大)[忘了这个吧,我给你弄好了]。
  • (2)准确地告诉我们正确的行为应该是什么(期望的输出)。
  • (3) 告诉我们您得到的行为(示例实际输出)。

那么也许我们可以做得更好。

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

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

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

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

【讨论】:

  • 规范文件中描述了正确的行为。可以通过运行规范文件来获取错误。
  • 我认为您应该在here上发布所有内容。当这些网站倒闭或决定将其投资货币化时,引用 tinyurl 或 github 共享的无数问题和答案会发生什么?老实说,即使是维基百科也是如此,尽管可能性较小。重新评论我应该只运行你的规范文件,你会发现你得到的帮助越多,投入其中。如果您在此处发布所有内容并实际描述问题所在,而不是仅仅说“运行规范文件”,那就容易多了。
猜你喜欢
  • 2010-10-30
  • 2011-04-23
  • 2014-11-14
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多