Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Artur Hallmann http://arturh.de

« Newer Snippets
Older Snippets »
1 total  XML / RSS feed 

Ruby Function Time Diff To String

def time_diff time1, time2
diff = time1 - time2

if diff < 1.minute
return diff.ceil.to_s + " seconds"
elsif diff < 1.hour
return (diff / 1.minute).ceil.to_s + " minutes"
elsif diff < 1.day
return (diff / 1.hour).ceil.to_s + " hours"
elsif diff < 7.day
return (diff / 1.day).ceil.to_s + " days"
elsif diff < 1.month
return (diff / 1.week).ceil.to_s + " weeks"
elsif diff < 1.year
return (diff / 1.month).ceil.to_s + " months"
else
return (diff / 1.year).ceil.to_s + " years"
end
end
« Newer Snippets
Older Snippets »
1 total  XML / RSS feed