1

How can I display how long ago a given date was. For example:

echo "File was modified `date -r file.txt`"

This would echo:

File was modified Tue Jan  7 00:52:40 GMT 2014

However I want it to resemble something like the following:

File was modified 3 seconds ago
File was modified 1 hour ago
File was modified 2 days ago
File was modified 3 weeks ago

Or something like that. The point is that it should be easy to understand when included in a sentence.

(This is not a duplicate of this question which only specified whole days.)

Community
  • 1
  • 1
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • For date arithmetic your best bet is a higher-level programming language, such as Python. – dg99 Jan 07 '14 at 23:54
  • This [question](http://stackoverflow.com/q/11/1671639) could gives you the idea for framing the algorithm. – Praveen Jun 13 '14 at 21:43

2 Answers2

1

I had a similar issue today and ended up coding a function for that:

 time_ago() {
  if (( $# < 1 )); then
    local u=$(tput smul) # underline 
    local n=$(tput rmul) # no underline 
    echo "usage: time_ago ${u}timestamp_ago${n} [${u}timestamp_now${n}]"
    exit 1
  fi
  local minutes=60
  local hours=$(( 60 * minutes ))
  local days=$(( 24 * hours ))
  local diff=$(( ${2:-$(date +%s)} - $1 ))
  local human
  if (( diff < 30 )); then
    human="${diff} seconds"
  elif (( diff < 90 )); then
    human="1 minute"
  elif (( diff < 1 * hours )); then
    human="$(( (diff + minutes / 2) / minutes )) minutes"
  elif (( diff < 1 * hours + 30 * minutes )); then
    human="about 1 hour"
  elif (( diff < 1 * days )); then
    human="about $(( (diff + hours / 2) / hours )) hours"
  elif (( diff < 2 * days)); then
    human="1 day"
  else
    human="$(( (diff + days / 2) / days)) days"
  fi

  echo "$human ago"
}

if [[ "$0" = "./time_ago" ]];then
  time_ago 1556324045 1556540404 # 3 days ago 
  time_ago 1556424045 1556540404 # 1 day ago 
  time_ago 1556534045 1556540404 # about 2 hours ago
  time_ago 1556536045 1556540404 # about 1 hour ago
  time_ago 1556539145 1556539875 # 12 minutes ago 
  time_ago 1556539900 1556539921 # 21 seconds ago
  time_ago
fi

It is available as a gist if you want to comment/edit. The intervals are inspired by rails' distance_of_time_in_word.

Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
0

You can use do some modular arithmetic to accomplish this. For example:

DATE=$(date -r file.txt +%s)
NOW=$(date +%s)
DIFF=$(expr $NOW - $DATE)

SEC=$((DIFF % 60))

DIFF=$((DIFF / 60))
MIN=$((DIFF % 60))

DIFF=$((DIFF / 60))
HOUR=$((DIFF % 24))

DIFF=$((DIFF / 24))
DAY=$DIFF

echo $DAY days, $HOUR hours, $MIN mins, $SEC seconds ago.
TheMasster12
  • 570
  • 5
  • 9
  • This is starting to get close to what I'm after, however it displays redundant usings (eg. `0 days, 0 hours, 0 mins, 15 seconds ago`) and makes grammatical errors around singular values (eg. `1 days, 1 hours, 1 mins, 1 seconds ago`). This seems like a good candidate for a small command line tool or even bash script that would accept two dates and produce nicer, more succinct output as shown in the original question. – Drew Noakes Jan 08 '14 at 01:06
  • @dg99 It seems I misinterpreted your question. Are you looking for something like [this](http://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site?rq=1) but in bash? – TheMasster12 Jan 08 '14 at 02:08
  • Yes, that's right. I'm optimising for human readability. The value will be in a sentence, not in a table, so it should read nicely. – Drew Noakes Jan 08 '14 at 03:06