Skip to content

Instantly share code, notes, and snippets.

@adscriven
Last active May 22, 2018 12:49
Show Gist options
  • Save adscriven/b77c59afae9352806182a91da0f5319d to your computer and use it in GitHub Desktop.
Save adscriven/b77c59afae9352806182a91da0f5319d to your computer and use it in GitHub Desktop.
Convert time in seconds to human-readable approximate age.
" fileage.vim
" Public domain.
" Usage: :echo Age(time_in_secs, 0)
" Useful in e.g. statusline, but don't use as is: cache
" the age of the file on bufread,bufwrite if you don't want
" to slow things down.
" secs, mins, hours, days, weeks, months, years
let ageunits = 'smhdwMy'
" Approx. after weeks (weeks in month is an average), but good
" enough for a rough indication.
let agedivis = [60.0, 60.0, 24.0, 7.0, 4.345238, 12.0, 1.0]
let ageprecis = [0, 0, 1, 1, 1, 1, 1]
let Age = {age, n ->
\ age == 0 || age / g:agedivis[n] < 1 || n + 1 == len(g:agedivis)
\ ? printf('%.*f%s', g:ageprecis[n], age, g:ageunits[n])
\ : g:Age(age / g:agedivis[n], n + 1)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment