2009-08-06 183 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

相關問題