2009-08-06 179 views
1

我试图为导轨制作一个“更好”的distance_of_time_in_words,到目前为止我拥有这个:http://github.com/radar/dotiw计算两次之间的距离

然而,目前它扼杀了几个月+年,如规格所示。我完全丧失了!帮帮我?由大同添加

代码,以便没有其他人有过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

这不应该是“差异”而不是“距离”? 也许这是一些时间空间的东西,我不明白:) – 2009-08-06 05:11:39

回答

2

它看起来像你试图让所有适用的单位而言的时间差(即“2年,3月,7天”,而不是“2年”这Rails的distance_of_time_in_words给出)?

如果是这样,请查看In Rails, display time between two dates in English,另一个关于显示时间差异的SO问题。我的回答应该给你一个好的起点,让你看起来好像在努力做什么,并且将方法扩展到包括小时,分钟和秒钟并不难。

+0

与我已有的非常相似,仍然不会产生正确的结果,但我更喜欢使用数字助手与“幻数”的方法 – 2009-08-06 22:17:17

2

我们也正处在一个完全丧失。也许你可以让我们的生活更轻松:

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

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

我注意到的一件事:在2月以外的任何月份(并且仅在非闰年),有而不是 2,419,200秒。同样,没有一年有364天。

我想你需要重新思考你如何计算间隔。

假设你的“单词”版本的持续时间可以处理一点点不准确,在最低限度,你应该使用这些数字的平均值(考虑到一年365.2425天,应该足够接近你的用途):

  • 一年是31556952秒。
  • 一个月是2,629,746秒(简单除以12)。
+0

正确的行为描述在spec文件中。错误可以通过运行spec文件来获得。 – 2009-08-06 06:20:30

+2

我认为你应该在SO上发布你所有的东西*。当这些网站折叠或决定将其投资货币化时,参考tinyurl或github共享的无数问题和答案会发生什么情况?说实话,即使是维基百科,情况也是如此,尽管不太可能。重新评论我应该运行你的规格文件,你会发现你获得更多的帮助。如果你在这里发布了所有东西,并且实际描述了问题所在,而不是仅仅说“运行spec文件”,那将会容易得多。 – paxdiablo 2009-08-06 07:48:33

相关问题