If you travel, you will probably have the problem that the time on your laptop is still the home time. On mobile phones, the time will automagically be retrieved from the mobile network time, but if you do not have a mobile connection on your laptop, this is quite hard. But there is a trick you can do, which requires the use of geoip-bin.
Just lookup your public wifi IP and compare it to the geoip database:
TZ=$(geoiplookup $(curl -s ipconfig.sh) | cut -d " " -f 4 | tr -d ',') date
With a little bit of scripting, you can put this into a nice script, which will refresh the cache of the local timezone every hour.
#!/bin/bash
# this script tries to get the local time by checking the public wifi IP
if ! [ -f ~/.cache/localtime.cache ] || [ $(($(date +%s) - $(stat --printf '%Y' ~/.cache/localtime.cache))) -gt 3600 ] ; then
geoiplookup $(curl -s ipconfig.sh) | cut -d " " -f 4 | tr -d ',' > ~/.cache/localtime.cache
if [ $? -ne 0 ]; then
exit 1
fi
fi
if [ -z "$1" ]; then
FMT="%c"
else
FMT="$1"
fi
if [ -f ~/.cache/localtime.cache ]; then
TZ=$(cat ~/.cache/localtime.cache) date +"$FMT"
else
date +"$FMT"
fi
Later I found out, that this script does only work, if the country you are in,
is listed as an own timezone in /usr/share/zoneinfo
. It is the case for GB
but for example not for AT
.
There are some conversion lists,
but you will obviously get problems when you are in the US or Russia, as those
countries span multiple timezones.
So yeah, this script kind of work in some cases only. But I hope you get the
point and can adjust it to your needs!