-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpercentages
executable file
·61 lines (56 loc) · 1.24 KB
/
percentages
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
get_fraction() {
st=$(date --date "$1" +%s)
en=$(date --date "$2" +%s)
now=$(date +%s)
total=$(( $en - $st ))
already_done=$(( $now - $st ))
passed=$(echo "$already_done * 100 / $total" | bc -l)
onesec=$( echo "1/$total" | bc -l )
}
determine_auto_precision() {
mapfile -t full_num <<<$(echo "$onesec" | bc -l | cut -d'.' -f2 | grep -o .)
for i in "${!full_num[@]}"; do
if [[ ${full_num[i]} -ne 0 ]]; then
echo $i
break
fi
done
}
main() {
start_time=$(date +'%Y-%m-%d %H:%m:%S' -d "January 1")
end_time=$(date +'%Y-%m-%d %H:%m:%S' -d "January 1 next year")
precision="auto"
format="%\n"
for i in "$@"; do
case $i in
-s|--start)
start_time=$2
shift
shift
;;
-e|--end)
end_time=$2
shift
shift
;;
-p|--precision)
precision=$2
shift
shift
;;
-f|--format)
format=$2
shift
shift
;;
*)
;;
esac
done
get_fraction "$start_time" "$end_time"
[[ $precision == "auto" ]] && precision=$(determine_auto_precision)
string=$(printf "%.${precision}f\n" $passed)
echo -e -n "${string}${format}"
}
main "$@"