Merge pull request #247 from femtofox/new_working

Fix bugs in time and time-zone setting utility
This commit is contained in:
noon92 2025-01-24 16:24:41 +02:00 • committed by GitHub
commit 597f947595
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 25 deletions

View file

@ -5,18 +5,18 @@
### Foxbuntu settings
## Activity LED setting. CaSe SeNsItIvE
## Options are "enable" and "disable"
#act_led="disable"
## Localization settings. CaSe SeNsItIvE
## List of timezones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
#wifi_country="US"
#timezone="UTC"
## Wifi settings / wpa_supplicant. CaSe SeNsItIvE
#wifi_ssid="wifi network name"
#wifi_psk="password"
#wifi_country="US"
## Localization settings. CaSe SeNsItIvE
## List of timezones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
#timezone="UTC"
## Activity LED setting. CaSe SeNsItIvE
## Options are "enable" and "disable"
#act_led="disable"
### Meshtastic settings

View file

@ -5,7 +5,7 @@ export TERM=xterm-256color
export LANG=C.UTF-8
if [[ $EUID -ne 0 ]]; then
echo -e "This script must be run as root. Try \`sudo femto-set-timezone\`."
echo -e "This script must be run as root. Try \`sudo femto-set-time.sh\`."
exit 1
fi
@ -14,7 +14,7 @@ arg_count=$#
help=$(cat <<EOF
If no argument is specified, a menu system will be used. Options are:
-h This message
-t "TIMEZONE" Set timezone
-t "TIMEZONE" Set time zone
-T "TIMESTAMP" Set timestamp (unix timestamp)
EOF
)
@ -29,26 +29,23 @@ log_message() {
}
set_timezone() {
for i in {1..5}; do
timedatectl set-timezone "$1" >/dev/null 2>&1 && break
sleep 1
done
[ "$i" -eq 5 ] && log_message "\nFailed to set the time zone to $1 after 5 attempts." && exit 1
echo "Setting time zone..."
ln -f -s /usr/share/zoneinfo/$1 /etc/localtime >/dev/null 2>&1
dpkg-reconfigure --frontend noninteractive tzdata >/dev/null 2>&1
}
set_timestamp() {
for i in {1..5}; do
if date -s "@$1" >/dev/null 2>&1; then
if hwclock --systohc >/dev/null 2>&1; then
echo "System time updated to:\n$(date)\n\nNew time successfully saved to RTC.\nTime & date are also set automatically from internet."
echo "Time Zone updated to:\n$(timedatectl show --property=Timezone --value) $(date +%Z) (UTC$(date +%:z))\nSystem time updated to:\n$(date)\n\nNew time successfully saved to RTC.\nTime & date are also set automatically from internet, if connected."
return 0
else
echo "System time updated to:\n$(date)\n\nUnable to communicate with RTC module. An RTC module can save system time between reboots/power outages.\nTime & date are also set automatically from internet."
echo "System time updated to:\n$(date)\n\nUnable to communicate with RTC module. An RTC module can remember system time between reboots/power outages.\nTime & date are also set automatically from internet, if connected."
return 0
fi
fi
done
echo "Failed to set system time to $(date -d @$1) after 5 attempts."
return 1
}
@ -70,7 +67,7 @@ done
if [ $arg_count -eq 0 ]; then # if the script was launched with no arguments, then load the UI.
# Fetch available time zones
echo "Loading timezones..."
echo "Loading time zones..."
timezones=$(timedatectl list-timezones)
# Build the options array
options=()
@ -80,11 +77,12 @@ if [ $arg_count -eq 0 ]; then # if the script was launched with no arguments, th
# Convert options array to string
options_str=$(printf '%s\n' "${options[@]}")
current_timezone=$(timedatectl show --property=Timezone --value)
# Show timezone selection menu with preselection of current timezone
selected_timezone=$(dialog --title "Select Timezone" \
--default-item "$(cat /etc/timezone)" \
--menu "Current timezone: $(cat /etc/timezone) (UTC$(date +%z))" 20 60 10 \
selected_timezone=$(dialog --title "Select Time Zone" \
--default-item "$current_timezone" \
--menu "Current time zone: $current_timezone (UTC$(date +%z))" 20 60 10 \
$(printf "%s " "${options[@]}") 3>&1 1>&2 2>&3)
exit_status=$?
if [[ $exit_status -eq 0 && -n "$selected_timezone" ]]; then
@ -94,12 +92,12 @@ if [ $arg_count -eq 0 ]; then # if the script was launched with no arguments, th
exit 1
fi
DATE=$(dialog --title "Select Date" --calendar "Choose a date:\nCurrent date: $(date "+%B %d, %Y")\nPress [TAB] to select." 0 0 $(date +%d) $(date +%m) $(date +%Y) 3>&1 1>&2 2>&3)
DATE=$(dialog --title "Select Date" --calendar "Current date: $(date "+%B %d, %Y")\nPress [TAB] to select." 0 0 $(date +%d) $(date +%m) $(date +%Y) 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
exit 1
fi
DATE=$(echo "$DATE" | awk -F'/' '{print $3"-"$2"-"$1}') #reformat to YYYY-MM-DD
TIME=$(dialog --title "Set Time" --timebox "Select time:\nCurrent time: $(date +%H:%M:%S)\nPress [TAB] to select." 0 0 3>&1 1>&2 2>&3) # Dialog timebox for time
TIME=$(dialog --title "Set Time" --timebox "Current time: $(date +%H:%M:%S)\nPress [TAB] to select." 0 0 3>&1 1>&2 2>&3) # Dialog timebox for time
if [ $? -eq 1 ]; then #if cancel/no
exit 1
fi