#!/bin/sh
# change this value to suit you, see below for further guidance
DESIRED_TIME_ZONE="US/Central"
# Note, if you were to get this value from an argument etc, keep
# in mind it would be vulnerable to command injection
# get timezone values from `ls /usr/share/zoneinfo` if you need them
# for example, you can see there's a /usr/share/zoneinfo/US/Central and
# /usr/share/zoneinfo/America/Chicago etc
# you shouldn't have to change anything below here
# actually set the timezone (overwrite the symbolic link)
echo "Setting timezone ($DESIRED_TIME_ZONE)"
sudo ln -sf /usr/share/zoneinfo/$DESIRED_TIME_ZONE /etc/localtime
sudo sh -c "echo $DESIRED_TIME_ZONE > /etc/timezone"
# get the current time from the Internet so we can automate this
# ignore "too far in the future" err if you get it
echo "Getting current time from timeapi.org" 1>&2
new_time=`wget -q -O - http://www.timeapi.org/utc/now.json | cut -d '"' -f 4`
echo "Got ($new_time)" 1>&2
echo "Setting date and time" 1>&2
sudo date -s "$new_time"