Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare('fuzzy', {})
- local time_map = {
- months = {value = 2419200},
- weeks = {value = 604800, noun = ' week'},
- days = {value = 86400, noun = ' day'},
- hours = {value = 3600, noun = ' hour'},
- minutes = {value = 60, noun = ' minute'},
- seconds = {value = 0, noun = ' second'}
- }
- local difftime = os.difftime
- local floor = math.floor
- function fuzzy.diff(b, a, fuzziness)
- --fuzziness should be called with the key name you wish for the diff to stop being fuzzy.
- --if you want to see ## days ago, then set fuzziness to 'weeks'
- fuzziness = fuzziness or 'weeks'
- local diff = difftime(b, a)
- if diff < 0 then return nil end
- local str, ftime = ''
- if (diff >= time_map.months.value or fuzziness == 'none') then
- return a
- elseif (diff >= time_map.weeks.value and diff < time_map[fuzziness].value) then
- ftime = floor(diff / time_map.weeks.value)
- str = ftime..time_map.weeks.noun
- elseif (diff >= time_map.days.value and diff < time_map[fuzziness].value) then
- ftime = floor(diff / time_map.days.value)
- str = ftime..time_map.days.noun
- elseif (diff >= time_map.hours.value and diff < time_map[fuzziness].value) then
- ftime = floor(diff / time_map.hours.value)
- str = ftime..time_map.hours.noun
- elseif (diff >= time_map.minutes.value and diff < time_map[fuzziness].value) then
- ftime = floor(diff / time_map.minutes.value)
- str = ftime..time_map.minutes.noun
- elseif (diff < time_map[fuzziness].value) then
- ftime = diff
- str = ftime..time_map.seconds.noun
- else
- return a
- end
- if (ftime > 1) then str = str..'s' end
- str = str..' ago'
- return str
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement