I would like a method/closure which works like this
println TimeDifference.format( System.currentMilliSeconds()-1000*60*60*24*7*6 )
and prints
1 month, 3 weeks, 2 days, 45 hours and 3 minutes ago.
Is there an off the shelf solution for this?
I would like a method/closure which works like this
println TimeDifference.format( System.currentMilliSeconds()-1000*60*60*24*7*6 )
and prints
1 month, 3 weeks, 2 days, 45 hours and 3 minutes ago.
Is there an off the shelf solution for this?
Here's a quick port. you may try:
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, [])
check out this library , its called human time, and it allows parsing and rendering of time that is more digestable by humans.
You can use this PrettyDate plugin -- http://www.grails.org/plugin/pretty-date
or this PrettyTime plugin -- http://www.grails.org/plugin/pretty-time
I don't know about Groovy, but it is very straightforward to port the one from rails. You can see the Ruby source code by clicking on 'show source'.
I'd use joda-time plugin, Period and PeriodFormatter, like here.