mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-06-01 05:14:04 +02:00
Align timedatectl with logic
https://github.com/alexbelgium/hassio-addons/issues/1595#issuecomment-2393969024
This commit is contained in:
@@ -1,47 +1,105 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Function to show the current timezone, with two alternative methods
|
# Check if the script is being run as root
|
||||||
|
if [ "$(id -u)" -ne 0 ]; then
|
||||||
|
echo "This script must be run as root or with sudo."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Function to show the current timezone using two alternative methods
|
||||||
show_timezone() {
|
show_timezone() {
|
||||||
# Check if the /etc/timezone file exists
|
if [ -f /data/timezone ]; then
|
||||||
if [ -f /etc/timezone ]; then
|
cat /data/timezone
|
||||||
timezone=$(cat /etc/timezone)
|
elif [ -f /etc/timezone ]; then
|
||||||
|
cat /etc/timezone
|
||||||
elif [ -f /etc/localtime ]; then
|
elif [ -f /etc/localtime ]; then
|
||||||
timezone=$(readlink /etc/localtime)
|
readlink /etc/localtime | sed 's|/usr/share/zoneinfo/||'
|
||||||
timezone=${timezone/\/usr\/share\/zoneinfo\//}
|
|
||||||
else
|
else
|
||||||
timezone="Cannot determine timezone."
|
echo "Cannot determine timezone."
|
||||||
fi
|
fi
|
||||||
echo "$timezone"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to set the timezone
|
# Function to set the timezone
|
||||||
set_timezone() {
|
set_timezone() {
|
||||||
new_timezone="$1"
|
local new_timezone="$1"
|
||||||
echo "$new_timezone" | sudo tee /etc/timezone >/dev/null
|
if [ ! -f "/usr/share/zoneinfo/$new_timezone" ]; then
|
||||||
sudo ln -sf /usr/share/zoneinfo/"$new_timezone" /etc/localtime
|
echo "Invalid timezone: $new_timezone"
|
||||||
if [ -f /etc/environment ]; then sudo sed -i "/TZ/c\TZ=$new_timezone" /etc/environment; fi
|
return 1
|
||||||
if [ -d /var/run/s6/container_environment ]; then echo "$new_timezone" | sudo tee /var/run/s6/container_environment/TZ > /dev/null; fi
|
fi
|
||||||
echo "$new_timezone"
|
|
||||||
|
echo "$new_timezone" > /data/timezone
|
||||||
|
echo "$new_timezone" > /etc/timezone
|
||||||
|
ln -sf "/usr/share/zoneinfo/$new_timezone" /etc/localtime
|
||||||
|
|
||||||
|
# Update /etc/environment if it exists
|
||||||
|
if [ -f /etc/environment ]; then
|
||||||
|
sed -i "/^TZ=/c\TZ=$new_timezone" /etc/environment
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Update s6 container environment if it exists
|
||||||
|
if [ -d /var/run/s6/container_environment ]; then
|
||||||
|
echo "$new_timezone" > /var/run/s6/container_environment/TZ
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Timezone set to: $new_timezone"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Main script
|
# Function to enable or disable NTP
|
||||||
|
set_ntp() {
|
||||||
|
case "$1" in
|
||||||
|
"false")
|
||||||
|
systemctl stop systemd-timesyncd
|
||||||
|
systemctl disable systemd-timesyncd
|
||||||
|
echo "NTP disabled"
|
||||||
|
;;
|
||||||
|
"true")
|
||||||
|
systemctl start systemd-timesyncd
|
||||||
|
systemctl enable systemd-timesyncd
|
||||||
|
|
||||||
|
# Remove the /data/timezone file when NTP is enabled
|
||||||
|
if [ -f /data/timezone ]; then
|
||||||
|
rm -f /data/timezone
|
||||||
|
echo "Timezone configuration file /data/timezone deleted."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "NTP enabled"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Invalid argument for set-ntp. Use 'false' or 'true'."
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to show detailed time settings
|
||||||
|
show_time_details() {
|
||||||
|
local local_time
|
||||||
|
local utc_time
|
||||||
|
local time_zone
|
||||||
|
local ntp_status="no"
|
||||||
|
local ntp_service="inactive"
|
||||||
|
|
||||||
|
local_time="$(date)"
|
||||||
|
utc_time="$(date -u)"
|
||||||
|
time_zone="$(show_timezone)"
|
||||||
|
|
||||||
|
# Check if NTP is used
|
||||||
|
if systemctl is-active --quiet systemd-timesyncd; then
|
||||||
|
ntp_status="yes"
|
||||||
|
ntp_service="active"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Print the information
|
||||||
|
echo "Local time: $local_time"
|
||||||
|
echo "Universal time: $utc_time"
|
||||||
|
echo "Time zone: $time_zone"
|
||||||
|
echo "Network time on: $ntp_status"
|
||||||
|
echo "NTP service: $ntp_service"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Main script logic
|
||||||
case "$1" in
|
case "$1" in
|
||||||
"set-ntp")
|
"set-ntp")
|
||||||
case "$2" in
|
set_ntp "$2"
|
||||||
"false")
|
|
||||||
sudo systemctl stop systemd-timesyncd
|
|
||||||
sudo systemctl disable systemd-timesyncd
|
|
||||||
echo "NTP disabled"
|
|
||||||
;;
|
|
||||||
"true")
|
|
||||||
sudo systemctl start systemd-timesyncd
|
|
||||||
sudo systemctl enable systemd-timesyncd
|
|
||||||
echo "NTP enabled"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Invalid argument for set-ntp. Use 'false' or 'true'."
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
;;
|
;;
|
||||||
"show")
|
"show")
|
||||||
show_timezone
|
show_timezone
|
||||||
@@ -50,23 +108,6 @@ case "$1" in
|
|||||||
set_timezone "$2"
|
set_timezone "$2"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
# Get values
|
show_time_details
|
||||||
local_time="$(date)"
|
|
||||||
utc_time="$(date -u)"
|
|
||||||
time_zone="$(show_timezone)"
|
|
||||||
# Check if NTP is used
|
|
||||||
if sudo systemctl status systemd-timesyncd | grep -q " active"; then
|
|
||||||
ntp_status="yes"
|
|
||||||
ntp_service="active"
|
|
||||||
else
|
|
||||||
ntp_status="no"
|
|
||||||
ntp_service="inactive"
|
|
||||||
fi
|
|
||||||
# Print the information
|
|
||||||
echo "Local time: $local_time"
|
|
||||||
echo "Universal time: $utc_time"
|
|
||||||
echo "Time zone: $time_zone"
|
|
||||||
echo "Network time on: $ntp_status"
|
|
||||||
echo "NTP service: $ntp_service"
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|||||||
Reference in New Issue
Block a user