3

I'm trying to properly format "and" when it comes to reading time. I'd like to try to figure out how to only echo "and" before the last value (whether it be Hours, Minutes, or Seconds) and only if there's more than one value (need two values, ie: 2 Days, and 5 minutes, or 2 Days, 12 hours, and 5 minutes)

I don't know the proper syntax to say, if ONLY one of the values, H, M, and S is greater than 0, then $DAnd="and ". This would be repeated for hours as well, so if ONLY of the values, M and S is greater than 0, then "$HAnd="and ". For minutes, it's easy, as it's only checking to see if S is greater than 0.

How does one achieve this without making a large mess of code?

My current script:

#!/bin/bash
TIME1="08/15/2018 10:30:41"
TIME2="08/30/2018 8:34:40"
SEC1=`date +%s -d "${TIME1}"`
SEC2=`date +%s -d "${TIME2}"`
DIFF=`expr ${SEC2} - ${SEC1}`
CONVERTTIME()
{
  local T=$1
  local D=$((T/60/60/24))
  local H=$((T/60/60%24))
  local M=$((T/60%60))
  local S=$((T%60))
  if [[ $D > 0 ]]; then
    [[ ($H = 0 || $M = 0 || $S = 0) && ($H = 0 && $M = 0 && $S = 0) ]] && DComma="" || DComma=","
    #The problem
    [[ ($H > 0 || $M > 0 || $S > 0) && ($H = 0 || $M = 0 || $S = 0) ]] && DAnd="" || DAnd="and "
    [[ $D = 1 ]] && echo -n "$D day$DComma " || echo -n "$D days$DComma $DAnd"

  fi
  if [[ $H > 0 ]]; then
    [[ ($M = 0 || $S = 0) && ($M = 0 && $S = 0) ]] && HComma="" || HComma=","
    #The problem
    [[ ($M > 0 || $S > 0) && ($M = 0 || $S = 0) ]] && HAnd="" || HAnd="and "
    [[ $H = 1 ]] && echo -n "$H hour$HComma $HAnd" || echo -n "$H hours$HComma $HAnd"
  fi
  if [[ $M > 0 ]]; then
    [[ $S = 0 ]] && MComma="" || MComma=", and "
    [[ $M = 1 ]] && echo -n "$M minute$MComma" || echo -n "$M minutes$MComma"
  fi
  if [[ $S > 0 ]]; then
    [[ $S = 0 ]] && echo -n "$S second" || echo -n "$S seconds"
  fi
  # If no difference in time:
  [[ $D = 0 && $H = 0 && $M = 0 && $S = 0 ]] && echo -n "0 seconds"
  echo
}
echo
echo "TIME DIFFERENCE: $(CONVERTTIME $DIFF)"
echo
3
  • 1
    I recommend you install the dateutils package so you don't have to reinvent the wheel.
    – Wildcard
    Aug 22, 2018 at 16:14
  • @Wildcard, how would you do that with dateutils? Off the top of the man page, I can't see how to get that output format.
    – ilkkachu
    Aug 22, 2018 at 16:44
  • @ilkkachu, it was a general comment; I don't think dateutils can handle specifically the use case of including the "and" string before the final unit, and leaving out the units that are 0. But it can easily compute the diff between two dates (provided in any format you like) and print the output according to any static format string you like. So not an answer to this precise use case, but hopefully useful for future readers anyway.
    – Wildcard
    Aug 22, 2018 at 17:46

3 Answers 3

2

I'd write that as

seconds2text() {     
    local diff=$1
    local words=()
    if (( diff == 0 )); then
        words=("0 seconds")
    else
        local s=$((diff % 60))
        local m=$((diff / 60 % 60))
        local h=$((diff / 60 / 60 % 24))
        local d=$((diff / 60 / 60 / 24))

        (( d > 0 )) && { unit=day;    (( d > 1 )) && unit+=s; words+=("$d $unit"); };
        (( h > 0 )) && { unit=hour;   (( h > 1 )) && unit+=s; words+=("$h $unit"); }
        (( m > 0 )) && { unit=minute; (( m > 1 )) && unit+=s; words+=("$m $unit"); }
        (( s > 0 )) && { unit=second; (( s > 1 )) && unit+=s; words+=("$s $unit"); }
        (( ${#words[@]} > 1 )) && words[-1]="and ${words[-1]}"
    fi
    local IFS=,
    local text="${words[*]}"
    text=${text/,and/ and}
    echo "${text//,/, }"
}

and then

$ for d in 0 1 61 3600 3601 3660 3661 86400 86401 86460 86461 90000 90001 90060 90061 180122; do seconds2text $d; done
0 seconds
1 second
1 minute and 1 second
1 hour
1 hour and 1 second
1 hour and 1 minute
1 hour, 1 minute and 1 second
1 day
1 day and 1 second
1 day and 1 minute
1 day, 1 minute and 1 second
1 day and 1 hour
1 day, 1 hour and 1 second
1 day, 1 hour and 1 minute
1 day, 1 hour, 1 minute and 1 second
2 days, 2 hours, 2 minutes and 2 seconds

Notes:

  • [[ $x > 0 ]] and [[ $x = 0 ]] are doing string comparison, not numeric comparison. Use ((x > 0)) and ((x == 0)) instead (and note that the $ is not required there).
  • get out of the habit of using ALLCAPS variables. Leave those for the shell. One day you'll write PATH=something and then wonder why your script is broken.
1
  • in zsh, you could write path=something and still wonder why the script broke.
    – ilkkachu
    Aug 22, 2018 at 16:38
1

With zsh:

#! /bin/zsh -
t1=${1?first date please}
t2=${2?second date please}

zmodload zsh/datetime
strftime -rst1 '%m/%d/%Y %H:%M:%S' "$t1" || exit
strftime -rst2 '%m/%d/%Y %H:%M:%S' "$t2" || exit

t=$((t2 - t1))
if ((t)) {
  and=' and' out= plural=s
  for unit duration (
    second 60
    minute 60
    hour   24
    day    7
    week   t+1
  ) {
    ((n = t % duration))
    ((t /= duration))
    ((n > 0)) && out="$and $n $unit${plural: n<2}$out" and=,
    ((t)) || break
  }
  out=${out#?* }
} else {
  out=now
}
echo "$out"

Then:

./that-script "08/15/2018 10:30:41" "08/30/2018 8:34:40"
2 weeks, 22 hours, 3 minutes and 59 seconds
0

How about combining the date command with a sed script )for a date / time difference less than a year)? Try

date +"%j d, %H h, %M m, %S s" -d@$(((d-90000))) | sed -r 's/, 0*00 .//g; s/365 d,* *//; s/ ([0-9]{2} [dhms])$/ and \1/; s/(^| )0+/\1/g; s/^$/0 s/; s/([02-9] [dhms])/\1s/g; s/ d(s|,|$)/ day\1/; s/ h/ hour/; s/ m/ minute/; s/ s/ second/'

Stealing the test loop from glenn jackman's post:

for DIFF in 0 1 61 3600 3601 3660 3661 86400 86401 86460 86461 90000 90001 90060 90061 180122
  do date +"%j d, %H h, %M m, %S s" -d@$(((DIFF-90000))) |
sed -r '
s/, 0*00 .//g
s/365 d,* *//
s/ ([0-9]{2} [dhms])$/ and \1/
s/(^| )0+/\1/g
s/^$/0 s/
s/([02-9] [dhms])/\1s/g
s/ d(s|,|$)/ day\1/
s/ h/ hour/
s/ m/ minute/
s/ s/ second/
'; done
0 seconds
1 second
1 minute, and 1 second
1 hour
1 hour, and 1 second
1 hour, and 1 minute
1 hour, 1 minute, and 1 second
1 day
1 day, and 1 second
1 day, and 1 minute
1 day, 1 minute, and 1 second
1 day, and 1 hour
1 day, 1 hour, and 1 second
1 day, 1 hour, and 1 minute
1 day, 1 hour, 1 minute, and 1 second
2 days, 2 hours, 2 minutes, and 2 seconds

(I know it will fail to add the plural s at any number ending in 1, e.g. 11, 21 etc...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.