Uma solução alternativa que imprime em até semanas:
s2t() {
local i=${1:-0} j str w n c=0 res
((i==0))&&{ echo "0 seconds"; return; } # with input zero result zero.
# s m h d w <-- weigths
w=(60 60 24 1 7)
n=(seconds minutes hours days weeks) # names.
for j in 0 1 2; do (( str[j]=i%w[j], i/=w[j] )); done # calculate s m h.
(( str[4]=i/w[4], i-=str[4]*w[4] )); # calculate w.
str[3]=$i # days remaining.
for j in {4..0}; do
((str[j]==0)) && unset str[j] # do not print empty items.
res=$res${str[j]+"${str[j]} ${n[j]}, "} # build the resulting string.
((str[j]==1))&&res=${res//"${n[j]}"/"${n[j]%?}"} # remove trailing 's'.
c=$(( c ${str[j]++1} )) # count the number of fields.
done
res=${res# } # remove leading empty space.
res=${res%, } # remove trailing ', '
((c>1)) && res="${res%,*} and${res##*,}" # add the 'and'
echo "$res" # final string printed.
}
Teste (uma longa lista de valores):
$ a='0 1 60 3600 86400 604800 61 3601 86401 604801 3660 86460 604860 90000 608400 691200 3661 86461 604861 90001 608401 691201 90060 608460 691260 694800 90061 608461 691261 694801 694860 694861 2084583'
Resultados:
$ for d in $a; do s2t "$d"; done
0 seconds
1 second
1 minute
1 hour
1 day
1 week
1 minute and 1 second
1 hour and 1 second
1 day and 1 second
1 week and 1 second
1 hour and 1 minute
1 day and 1 minute
1 week and 1 minute
1 day and 1 hour
1 week and 1 hour
1 week and 1 day
1 hour, 1 minute and 1 second
1 day, 1 minute and 1 second
1 week, 1 minute and 1 second
1 day, 1 hour and 1 second
1 week, 1 hour and 1 second
1 week, 1 day and 1 second
1 day, 1 hour and 1 minute
1 week, 1 hour and 1 minute
1 week, 1 day and 1 minute
1 week, 1 day and 1 hour
1 day, 1 hour, 1 minute and 1 second
1 week, 1 hour, 1 minute and 1 second
1 week, 1 day, 1 minute and 1 second
1 week, 1 day, 1 hour and 1 second
1 week, 1 day, 1 hour and 1 minute
1 week, 1 day, 1 hour, 1 minute and 1 second
3 weeks, 3 days, 3 hours, 3 minutes and 3 seconds
Abaixo está uma segunda solução que se transforma em séculos que incluem meses de 30 dias e anos de 365 dias. Isso faz com que essa solução seja aproximada porque não há uma solução exata para meses (que devem estar próximos a 30,436875 em média) ou anos (que são em média de 364,2524).
Os valores do teste são muito mais longos:
a='0 1 60 3600 604800 2592000 31536000 86400 61 3601 604801 2592001 31536001 86401 3660 604860 2592060 31536060 86460 608400 2595600 31539600 90000 3196800 32140800 691200 34128000 2678400 31622400 3661 604861 2592061 31536061 86461 608401 2595601 31539601 90001 3196801 32140801 691201 34128001 2678401 31622401 608460 2595660 31539660 90060 3196860 32140860 691260 34128060 2678460 31622460 3200400 32144400 694800 34131600 2682000 31626000 34732800 3283200 32227200 34214400 608461 2595661 31539661 90061 3196861 32140861 691261 34128061 2678461 31622461 3200401 32144401 694801 34131601 2682001 31626001 34732801 3283201 32227201 34214401 3200460 32144460 694860 34131660 2682060 31626060 34732860 3283260 32227260 34214460 34736400 3286800 32230800 34218000 34819200 3200461 32144461 694861 34131661 2682061 31626061 34732861 3283261 32227261 34214461 34736401 3286801 32230801 34218001 34819201 34736460 3286860 32230860 34218060 34819260 34822800 34736461 3286861 32230861 34218061 34819261 34822801 34822860 34822861 9565268583'
O resultado também é bastante longo, mostrarei apenas alguns valores:
0 seconds
1 second
1 minute
1 hour
1 week
1 month
1 year
1 day
1 century and 1 second
1 day, 1 hour, 1 minute and 1 second
1 year, 1 week, 1 hour and 1 minute
3 centuries, 3 years, 3 months, 3 weeks, 3 days, 3 hours, 3 minutes and 3 seconds