Advertisement
draugath

Untitled

May 20th, 2012
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1. declare('fuzzy', {})
  2. local time_map = {
  3.     months  = {value = 2419200},
  4.     weeks   = {value =  604800, noun = ' week'},
  5.     days    = {value =   86400, noun = ' day'},
  6.     hours   = {value =    3600, noun = ' hour'},
  7.     minutes = {value =      60, noun = ' minute'},
  8.     seconds = {value =       0, noun = ' second'}
  9. }
  10.  
  11. local difftime = os.difftime
  12. local floor = math.floor
  13.  
  14. function fuzzy.diff(b, a, fuzziness)
  15.     --fuzziness should be called with the key name you wish for the diff to stop being fuzzy.
  16.     --if you want to see ## days ago, then set fuzziness to 'weeks'
  17.     fuzziness = fuzziness or 'weeks'
  18.     local diff = difftime(b, a)
  19.     if diff < 0 then return nil end
  20.    
  21.     local str, ftime = ''
  22.     if (diff >= time_map.months.value or fuzziness == 'none') then
  23.         return a
  24.  
  25.     elseif (diff >= time_map.weeks.value and diff < time_map[fuzziness].value) then
  26.         ftime = floor(diff / time_map.weeks.value)
  27.         str = ftime..time_map.weeks.noun
  28.  
  29.     elseif (diff >= time_map.days.value and diff < time_map[fuzziness].value) then
  30.         ftime = floor(diff / time_map.days.value)
  31.         str = ftime..time_map.days.noun
  32.  
  33.     elseif (diff >= time_map.hours.value and diff < time_map[fuzziness].value) then
  34.         ftime = floor(diff / time_map.hours.value)
  35.         str = ftime..time_map.hours.noun
  36.  
  37.     elseif (diff >= time_map.minutes.value and diff < time_map[fuzziness].value) then
  38.         ftime = floor(diff / time_map.minutes.value)
  39.         str = ftime..time_map.minutes.noun
  40.  
  41.     elseif (diff < time_map[fuzziness].value) then
  42.         ftime = diff
  43.         str = ftime..time_map.seconds.noun
  44.  
  45.     else
  46.         return a
  47.     end
  48.  
  49.     if (ftime > 1) then str = str..'s' end
  50.     str = str..' ago'
  51.  
  52.     return str
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement