randtime.sh (2971B)
1 #!/bin/sh 2 3 r=0 4 c=0 5 challenge=1 6 flat=0 7 8 OPTS="hc:fr" 9 LONGOPTS="help,challenge:,flat,round" 10 11 parsed=$(getopt --options=$OPTS --longoptions=$LONGOPTS -- "$@") 12 eval set -- "${parsed[@]}" 13 14 while true; do 15 case "$1" in 16 -f|--flat) flat=1 ;; 17 -r|--round) r=1 ;; 18 -c|--challenge) 19 c=1 20 challenge=$2 21 shift;; 22 23 --) # end of arguments 24 shift 25 break 26 ;; 27 28 *) 29 printf '%sn' "Error while parsing CLI options" 1>&2 30 ;; 31 esac 32 33 shift 34 done 35 36 successes=0 37 from=0 38 til=0 39 average=0 40 41 if [ "$c" == "1" ]; then 42 clear 43 for i in {3..1}; do 44 echo "$i" 45 sleep 1 46 clear 47 done 48 fi 49 50 for i in $(seq $challenge); do 51 export LC_ALL=en_GB.UTF-8 52 from=$(date "+%s") 53 rand=$(shuf -i 1-2147483648 -n 1) 54 55 p="" 56 if [ $((rand % 2)) == 0 ]; then 57 format="+%I:%M %p" 58 p=$(date -d "@$rand" "+%p") 59 else 60 format="+%I:%M" 61 fi 62 63 date -d "@$rand" "$format" 64 h=$(date -d "@$rand" "+%I") 65 m=$(date -d "@$rand" "+%M") 66 export LC_ALL=es_MX.UTF-8 67 68 h=${h#0} 69 m=${m#0} 70 71 if [ "$r" == "1" ]; then 72 m=$((( (m + 2) / 5) * 5 )) 73 if [ $m == 60 ]; then 74 m=0 75 h=$(((h % 12) + 1)) 76 fi 77 fi 78 79 hour=$(python ~/.local/bin/inttoesp.py "$h") 80 81 if [ $m != 0 ]; then 82 if [ $m == 15 ]; then 83 minutes=" y cuarto" 84 elif [ $m -lt 30 ]; then 85 minutes=" y $(python ~/.local/bin/inttoesp.py $m)" 86 elif [ $m == 30 ]; then 87 minutes=" y media" 88 fi 89 90 if [ $m == 45 ]; then 91 minutes=" menos cuarto" 92 hour="$(python ~/.local/bin/inttoesp.py $(((h % 12) + 1)))" 93 elif [ $m -gt 30 ]; then 94 minutes=" menos $(python ~/.local/bin/inttoesp.py $((60 - $m)))" 95 hour="$(python ~/.local/bin/inttoesp.py $(((h % 12) + 1)))" 96 fi 97 else 98 minutes="" 99 fi 100 101 if [ "$p" == "am" ]; then 102 if [ $((h % 12)) -lt 6 ]; then 103 period=" de la madrugada" 104 else 105 period=" de la mañana" 106 fi 107 elif [ "$p" == "pm" ]; then 108 if [ $((h % 12)) -lt 6 ]; then 109 period=" de la tarde" 110 else 111 period=" de la noche" 112 fi 113 else 114 period="" 115 fi 116 117 if [ $hour == "uno" ]; then 118 full_date="Es la una$minutes$period" 119 else 120 full_date="Son las $hour$minutes$period" 121 fi 122 123 if [ $h == 12 ] && [ $m == 0 ]; then 124 if [ "$p" == "am" ]; then 125 full_date="Es mediodía" 126 elif [ "$p" == "pm" ]; then 127 full_date="Es medianoche" 128 fi 129 fi 130 131 read user_input 132 133 if [ "$full_date" == "$user_input" ]; then 134 echo "correcto!" 135 successes=$((successes + 1)) 136 else 137 actual="" 138 for (( j=0; j<${#full_date}; j++ )); do 139 if [ "${full_date:$j:1}" == "${user_input:$j:1}" ]; then 140 actual="$actual${full_date:$j:1}" 141 else 142 actual="$actuale[01;31m${full_date:$j:1}e[0m" 143 fi 144 done 145 echo -e $actual 146 fi 147 148 til=$(date "+%s") 149 average=$((average + (til - from))) 150 done 151 152 if [ "$c" == "1" ]; then 153 echo "average time: $((average/challenge)) s" 154 echo "total time: $average s" 155 echo "total: $successes / $challenge correct!" 156 fi