much more accurate age

This commit is contained in:
2022-08-25 12:15:43 +03:00
parent 890216e348
commit 4759c3554d

View File

@@ -1,5 +1,5 @@
#!/bin/bash
set -e
function _help() {
echo '
Usage: file-age {file} [units]
@@ -7,27 +7,67 @@ Usage: file-age {file} [units]
Checks file modification date, and the current time. Prints the age of
the file in [units].
[units] is one of S, M, H, d, w, m, y, human
[units] is one of S, M, H, d, w, m, y, full, human
seconds is the default. All values floored.
month = 30 days
year = 365 days
human = YY-MM-DD hh:mm:ss
'
exit
year = 365 days
full = YY-MM-DD hh:mm:ss
human = 3 months 2 days
'
exit
}
for (( i=1; i<=$#; i++ ))
do [[ "${!i}" = "-h" ]] && _help
done
function _human {
local T=$1
local Y=$((T/60/60/24/365))
local m=$((T/60/60/24/30%12))
local D=$((T/60/60/24%30))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
printf '%02d-%02d-%02d %02d:%02d:%02d\n' $Y $m $D $H $M $S
file_date=( $( date -r "$STATFILE" +"%Y %m %d %H %M %S" ) )
current_date=( $( date +"%Y %m %d %H %M %S" ) )
local values=()
for (( i=0; $i<${#file_date[@]}; i++ )); do
values+=( $(( ${current_date[$i]#0} - ${file_date[$i]#0})) )
done
if [[ ${values[5]} -lt 0 ]]; then
values[5]=$(( 60 + ${values[5]} )); values[4]=$(( ${values[4]} -1 ))
fi
if [[ ${values[4]} -lt 0 ]]; then
values[4]=$(( 60 + ${values[4]} )); values[3]=$(( ${values[3]} -1 ))
fi
if [[ ${values[3]} -lt 0 ]]; then
values[3]=$(( 24 + ${values[3]} )); values[2]=$(( ${values[2]} -1 ))
fi
if [[ ${values[2]} -lt 0 ]]; then
case ${file_date[1]#0} in
1|3|5|7|8|10|12) mlen=31;;
2) mlen=28;;
*) mlen=30;;
esac
values[2]=$(( $mlen + ${values[2]} ));
values[1]=$(( ${values[1]} -1 ))
fi
if [[ ${values[1]} -lt 0 ]]; then
values[1]=$(( 12 + ${values[1]} )); values[0]=$(( ${values[0]} -1 ))
fi
if [[ "$1" = "full" ]]; then
printf '%02d-%02d-%02d %02d:%02d:%02d\n' ${values[@]}
return
fi
local units=( yr mo day hr min sec )
if [[ "${file_date[@]}" = "${current_date[@]}" ]]; then
printf " 0 sec\n"; return
fi
for (( i=0; $i<${#values[@]}; i++ )); do
if [[ ${values[$i]} -gt 0 ]]; then
j=$(( i + 1 ))
printf "%2s %-3s" ${values[$i]} ${units[$i]}
if [[ $j -lt ${#values[@]} ]]; then
printf " %2s %-3s" ${values[$j]} ${units[$j]}
break
fi
fi
done
printf "\n"
}
STATFILE="$1"
@@ -44,22 +84,24 @@ age=$(( $now - $created ))
case $UNITS in
S)
echo $age;;
echo $age ;;
M)
echo $(( $age / 60 ));;
echo $(( $age / 60 )) ;;
H)
echo $(( $age / 3600 ));;
echo $(( $age / 3600 )) ;;
d)
echo $(( $age / 86400 ));;
echo $(( $age / 86400 )) ;;
w)
echo $(( $age / 604800 ));;
echo $(( $age / 604800 )) ;;
m)
echo $(( $age / 2592000 ));;
echo $(( $age / 2592000 )) ;;
y)
echo $(( $age / 31536000 ));;
echo $(( $age / 31536000 )) ;;
full)
_human full;;
human)
_human "$age";;
_human;;
*)
echo Unit $UNITS not recognized.
_help;;
_help ;;
esac