Files
hassio-addons/birdnet-pi/rootfs/helpers/timedatectl
2025-06-29 00:35:16 +00:00

108 lines
2.2 KiB
Bash

#!/bin/bash
# Function to show the current timezone using two alternative methods
show_timezone() {
if [ -f /data/timezone ]; then
cat /data/timezone
elif [ -f /etc/timezone ]; then
cat /etc/timezone
elif [ -f /etc/localtime ]; then
readlink /etc/localtime | sed 's|/usr/share/zoneinfo/||'
else
echo "Cannot determine timezone."
fi
}
# Function to set the timezone
set_timezone() {
local new_timezone="$1"
if [ ! -f "/usr/share/zoneinfo/$new_timezone" ]; then
echo "Invalid timezone: $new_timezone"
return 1
fi
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"
}
# 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
"set-ntp")
set_ntp "$2"
;;
"show")
show_timezone
;;
"set-timezone")
set_timezone "$2"
;;
*)
show_time_details
;;
esac