2009-02-03 4 views

ответ

1

чек из this library, его называют человеческое время, и это позволяет синтаксический анализ и визуализацию времени, который является более перевариваемым людьми.

0

Я не знаю о Groovy, но это очень просто для порта the one from rails. Вы можете увидеть исходный код Ruby, нажав «показать источник».

2

Вот быстрый порт. Вы можете попробовать:

def distance_of_time_in_words = { fromTime, toTime, includeSeconds, options -> 
    if(toTime==null) toTime = 0 
    if(includeSeconds==null) includeSeconds = false 
    if(options==null) options = [] 

    def distance_in_mins = (int)Math.round(Math.abs(toTime - fromTime)/60.0) 
    def distance_in_seconds = (int)Math.round(Math.abs(toTime - fromTime)) 

    switch (distance_in_mins) { 
     case 0..1: 
      if(distance_in_mins == 0) { 
       return "less than 1 minute" 
      } else { 
       if (includeSeconds == false) return "${distance_in_mins} minute" 
      } 

      switch (distance_in_seconds) { 
       case 0..4: return "less than 5 seconds" 
       case 5..9: return "less than 10 seconds" 
       case 10..19: return "less than 20 seconds" 
       case 20..39: return "half a minute" 
       case 40..59: return "less than 1 minute" 
       default: return "1 minute" 
      } 

     case 2..44: return "${distance_in_mins} minutes" 
     case 45..89: return "about an hour" 
     case 90..1439: return "about ${Math.round(distance_in_mins/60.0)} hours" 
     case 1440..2879: return "a day" 
     case 2880..43199: return "${Math.round(distance_in_mins/1440)} days" 
     case 43200..86399: return "a month" 
     case 86400..525599: return "${Math.round(distance_in_mins/43200)} months" 
     case 525600..1051199: return "a year" 
     default: return "over ${Math.round(distance_in_mins/525600)} years" 
    } 

} 

def d = new Date().time 
println distance_of_time_in_words(d, d + 10, true, []) 
println distance_of_time_in_words(d, d + 60, true, []) 
println distance_of_time_in_words(d, d + 35, true, []) 
println distance_of_time_in_words(d, d + 123, true, []) 
println distance_of_time_in_words(d, d + 400, true, []) 
println distance_of_time_in_words(d, d + 1300*60, true, []) 
println distance_of_time_in_words(d, d + 2000*60, true, []) 
println distance_of_time_in_words(d, d + 3.5*60*60*24, true, []) 
println distance_of_time_in_words(d, d + 32*60*60*24, true, []) 
println distance_of_time_in_words(d, d + 65*60*60*24, true, []) 
println distance_of_time_in_words(d, d + 1.5*12*30*60*60*24, true, []) 
println distance_of_time_in_words(d, d + 7*12*30*60*60*24, true, []) 
0

Вы могли бы также проверить с помощью timeago, что делается на яваскрипте стороны. Достаточно легко написать taglib, который вы можете использовать для создания даты с соответствующим классом в вашей разметке.

Смежные вопросы