change-time: Infer value digits automatically

Infer digits from the length of the maximum value.

Signed-off-by: Jonathon Hall <jonathon.hall@puri.sm>
This commit is contained in:
Jonathon Hall 2024-08-06 09:30:38 -04:00
parent b908e0b8bd
commit 234595df4a
No known key found for this signature in database
GPG Key ID: 1E9C3CA91AE25114

View File

@ -8,11 +8,10 @@ echo -e -n "You need to set the time according to the UTC/GMT timezone please ch
get_date () {
local field_name min max digits
local field_name min max
field_name="$1"
min="$2"
max="$3"
digits="$4"
echo "Please insert $field_name (between $min-$max)"
read -r value
echo
@ -25,21 +24,21 @@ get_date () {
echo
done
# Pad with zeroes to digits
value="$(printf "%0${digits}u" "$value")"
# Pad with zeroes to length of maximum value
value="$(printf "%0${#max}u" "$value")"
}
get_date "year" "2024" "2200" "4"
get_date "year" "2024" "2200"
year=$value
get_date "month" "01" "12" "2"
get_date "month" "01" "12"
month=$value
get_date "day" "01" "31" "2"
get_date "day" "01" "31"
day=$value
get_date "hour" "00" "23" "2"
get_date "hour" "00" "23"
hour=$value
get_date "minute" "00" "59" "2"
get_date "minute" "00" "59"
min=$value
get_date "second" "00" "59" "2"
get_date "second" "00" "59"
sec=$value
if ! date -s "$year-$month-$day $hour:$min:$sec" &>/dev/null; then