Guest User

Untitled

a guest
Dec 11th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. # Some handy informal duration functions in Bourne shell (bash is fine, too).
  2.  
  3. # duration(): given seconds, returns commonsense equivalent duration.
  4.  
  5. # ago(): given seconds from epoch, compares to current time.
  6.  
  7. # howlongago(): show how long ago a file or subdir was last modified.
  8. # given a file, shows how long ago that file was updated.
  9. # given a directory, recursively searches for most
  10. # recently changed FILE (not directory)
  11. # given no argument, searches the current working directory.
  12.  
  13.  
  14. # Examples:
  15. #
  16. # $ howlongago /etc/passwd
  17. # Last updated 3 months, 15 weeks ago
  18. #
  19. # $ howlongago /var/www # searches recursively for most recent file
  20. # Last updated 1 day, 0 hours ago
  21. #
  22. # $ howlongago # no argument checks current working directory
  23. # Last updated 5 minutes, 57 seconds ago
  24. #
  25. # $ duration $((10**9)) # how long is a billion seconds?
  26. # 31 years
  27. #
  28. # $ ago 0 # How long ago was the Unix Epoch?
  29. # 60 years ago
  30. #
  31. # $ date +%s
  32. # 1912817946 # Current number of seconds since the Epoch
  33. #
  34. # $ ago $(date +%s) # Of course, now is always now.
  35. # just now
  36. #
  37. # # "ago" isn't just for the past
  38. # $ ago $(date --date='next tuesday' +%s)
  39. # 2 days, 20 hours from now
  40. #
  41. # # how long until R's birthday?
  42. # $ ago $(date -d 'feb 11 next year' +%s)
  43. # 2 months, 9 weeks from now
  44.  
  45.  
  46. duration() {
  47. # Input number of seconds, print informal duration in English.
  48. # Shows two units of precision. (E.g., hours and minutes).
  49.  
  50. # Inaccuracies that probably don't matter:
  51. # 1. Always rounds *down*.
  52. # E.g. 1 hour, 59 minutes, 59 seconds => 1 hour, 59 minutes
  53. # 2. A month is precisely 30 days. This is, of course, not quite true.
  54. # 3. Similarly, a year is exactly 364 days.
  55.  
  56. local s=$1; local m=$((s / 60)); local h=$((m / 60))
  57. local d=$((h / 24)); local w=$((d / 7)); local M=$((d / 30))
  58. local y=$((d / 364))
  59. if [ $s -lt 60 ]; then
  60. span="$s second`s $s`"
  61. elif [ $s -lt $((60*60)) ]; then
  62. s=$((s%60))
  63. span="$m minute`s $m`, $s second`s $s`"
  64. elif [ $s -lt $((60*60*24)) ]; then
  65. m=$((m%60))
  66. span="$h hour`s $h`, $m minute`s $m`"
  67. elif [ $s -lt $((60*60*24*7)) ]; then
  68. h=$((h%24))
  69. span="$d day`s $d`, $h hour`s $h`"
  70. elif [ $s -lt $((60*60*24*30)) ]; then
  71. d=$((d%7))
  72. span="$w week`s $w`, $d day`s $d`"
  73. elif [ $s -lt $((60*60*24*364)) ]; then
  74. w=$((w%30))
  75. span="$M month`s $M`, $w week`s $w`"
  76. elif [ $s -lt $((60*60*24*364*10)) ]; then
  77. M=$((M%12))
  78. span="$y year`s $y`, $M month`s $M`"
  79. else
  80. span="$y year`s $y`" # Stopping counting months after a decade
  81. fi
  82. echo "$span"
  83. }
  84.  
  85. ago() {
  86. # Given a date (in seconds since The Epoch), print how long ago
  87. # that was from now, in informal English.
  88. local now=$(date +%s)
  89. local s=$((now - $1))
  90. if [ $s -eq 0 ]; then
  91. echo "just now"
  92. elif [ $s -gt 0 ]; then
  93. echo "$(duration $s) ago"
  94. else
  95. s=$((-s))
  96. echo "$(duration $s) from now"
  97. fi
  98. }
  99.  
  100. howlongago() {
  101. # Given a directory or filename (or nothing for cwd), print how
  102. # recently it was last modified in informal English.
  103.  
  104. # Note: Recursively searches directories for all files and reports
  105. # on the one most recently updated. Directories themselves are
  106. # *not* considered for comparison since they get modified by simple
  107. # things like renaming or copying a file.
  108.  
  109. if [ -z "$1" ]; then
  110. dir="."
  111. else
  112. dir="$1"
  113. fi
  114. local mostrecent=$(find "$dir" -type f -printf '%T@\n' | cut -d. -f1 | sort -n | tail -n1)
  115. if [ -z "$mostrecent" ]; then
  116. echo "No files found"
  117. return
  118. fi
  119.  
  120. echo "Last updated $(ago $mostrecent)"
  121. }
  122.  
  123. s() {
  124. if [ $1 -ne 1 ]; then echo -n "s"; fi
  125. }
  126.  
  127.  
  128. # TODO
  129. # Maybe consider adding support for something larger than "years".
  130. #
  131. # Decades don't work since nobody talks about "4 decades and 7 years".
  132. # We could use "score", for twenty years, but that's just weird.
  133.  
  134. # In English, what happens is people start rounding to the nearest
  135. # decade at some point, as in: "over 130 years ago". Perhaps, I
  136. # should start after a century?
Add Comment
Please, Sign In to add comment