Skip to content

Instantly share code, notes, and snippets.

@blackvoidx
Created July 29, 2025 12:46
Show Gist options
  • Save blackvoidx/a774bcbd37634f79e8e16b9c60344789 to your computer and use it in GitHub Desktop.
Save blackvoidx/a774bcbd37634f79e8e16b9c60344789 to your computer and use it in GitHub Desktop.
Change time zone based on system IP
#!/bin/bash
# Default timezone for --default option
DEFAULT_TZ="Asia/Tehran"
# Function to display help
display_help() {
echo "Usage: $0 [OPTION]"
echo
echo "Automatically detect and update timezone based on IP address location."
echo
echo "Options:"
echo " -h, --help Display this help message and exit"
echo " --default Set timezone to $DEFAULT_TZ without detection"
echo
echo "When run without options, the script will:"
echo "1. Detect your current IP address and location"
echo "2. Show your country and capital city"
echo "3. Compare with current system timezone"
echo "4. Prompt to update timezone if different"
exit 0
}
# Function to get current public IP information
get_ip_info() {
echo "Detecting your IP information..."
ip_info=$(curl -s https://ipinfo.io/json)
if [ -z "$ip_info" ]; then
echo "Error: Could not fetch IP information. Check your internet connection."
exit 1
fi
ip=$(echo "$ip_info" | jq -r '.ip')
country=$(echo "$ip_info" | jq -r '.country')
city=$(echo "$ip_info" | jq -r '.city')
timezone=$(echo "$ip_info" | jq -r '.timezone')
echo "----------------------------------------"
echo "Your current IP address: $ip"
echo "Country: $country"
echo "City: $city"
echo "Detected timezone: $timezone"
echo "----------------------------------------"
}
# Function to get the current system timezone
get_current_timezone() {
if [ -f /etc/localtime ]; then
current_tz=$(readlink /etc/localtime | sed 's|/usr/share/zoneinfo/||')
if [ -z "$current_tz" ]; then
current_tz=$(timedatectl | grep "Time zone" | awk '{print $3}')
fi
echo "$current_tz"
else
current_tz=$(timedatectl | grep "Time zone" | awk '{print $3}')
echo "$current_tz"
fi
}
# Function to change timezone
change_timezone() {
local new_tz=$1
echo "Changing timezone to $new_tz..."
# Check if the timezone file exists
if [ ! -f "/usr/share/zoneinfo/$new_tz" ]; then
echo "Error: Timezone $new_tz not found in /usr/share/zoneinfo/"
return 1
fi
# Change the timezone
sudo unlink /etc/localtime 2>/dev/null
sudo ln -s "/usr/share/zoneinfo/$new_tz" /etc/localtime
# Verify the change
current_tz=$(get_current_timezone)
if [ "$current_tz" == "$new_tz" ]; then
echo "Timezone successfully changed to $new_tz"
echo "Current time: $(date)"
else
echo "Warning: Timezone change might not have worked correctly."
echo "Current timezone is: $current_tz"
fi
}
# Check if jq is installed (for JSON parsing)
check_dependencies() {
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed. Please install it first."
echo "For Debian/Ubuntu: sudo apt-get install jq"
echo "For RHEL/CentOS: sudo yum install jq"
exit 1
fi
if ! command -v curl &> /dev/null; then
echo "Error: curl is not installed. Please install it first."
exit 1
fi
}
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help)
display_help
;;
--default)
change_timezone "$DEFAULT_TZ"
exit 0
;;
*)
echo "Unknown parameter: $1"
display_help
exit 1
;;
esac
shift
done
# Main script
check_dependencies
get_ip_info
current_tz=$(get_current_timezone)
echo "Current system timezone: $current_tz"
if [ "$timezone" == "$current_tz" ]; then
echo "Your timezone is already set correctly for your detected location."
exit 0
fi
echo "Your timezone ($current_tz) doesn't match your detected location ($timezone)."
read -p "Do you want to change your timezone to $timezone? [Y/n] " answer
answer=${answer:-Y} # Default to Y if empty
case ${answer^^} in
Y|YES|"")
change_timezone "$timezone"
;;
*)
echo "Timezone not changed."
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment