Skip to content

Instantly share code, notes, and snippets.

@rbf
Last active December 26, 2015 21:18
Show Gist options
  • Save rbf/7214728 to your computer and use it in GitHub Desktop.
Save rbf/7214728 to your computer and use it in GitHub Desktop.
Script to set the time zone on *nix systems with informative feedback. Includes a one-liner for direct execution without feedback.
#!/bin/bash
# Tested in CentOS 6.3, CentOS 6.4
# Direct one-liner:
# sudo mv /etc/localtime /etc/localtime.bak && sudo ln -s /usr/share/zoneinfo/Europe/Zurich /etc/localtime && date
S_TIMEZONE_LOCAL="/etc/localtime"
S_TIMEZONES_DIR="/usr/share/zoneinfo"
S_TIMEZONE=${1:-"Europe/Zurich"} # as in /usr/share/zoneinfo/*
S_DATE_DISPLAY_FORMAT="%A, %B %d %Y, %H:%M:%S - Timezone %z %Z"
if [ -e ${S_TIMEZONES_DIR}/${S_TIMEZONE} ]
then
echo "Current date is: $(date +"${S_DATE_DISPLAY_FORMAT}")"
if [ "${S_TIMEZONE_LOCAL}" -ef "${S_TIMEZONES_DIR}/${S_TIMEZONE}" ]
then
echo "Timezone is already set to ${S_TIMEZONE}. Nothing to do."
else
echo -n "Setting timezone to ${S_TIMEZONE}... "
sudo mv ${S_TIMEZONE_LOCAL} ${S_TIMEZONE_LOCAL}.bak_$(date +"%Y%m%d%H%M%S%Z") && sudo ln -s ${S_TIMEZONES_DIR}/${S_TIMEZONE} ${S_TIMEZONE_LOCAL} && S_TIMEZONE_SET="OK"
if [ "${S_TIMEZONE_SET}" == "OK" ]
then
echo "OK"
echo "Date is now: $(date +"${S_DATE_DISPLAY_FORMAT}")"
else
echo "FAILED"
fi
fi
else
echo "Timezone ${S_TIMEZONE} is not found in dir ${S_TIMEZONES_DIR}. Nothing done."
if [ -n "$(find ${S_TIMEZONES_DIR}/ -ipath "*${S_TIMEZONE}*")" ]
then
echo "Did you mean one of those?"
echo "$(find ${S_TIMEZONES_DIR}/ -ipath "*${S_TIMEZONE}*")"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment