很簡單的想法
別人也早就寫好了
安裝
pip install -U tzupdate
使用
sudo tzupdate
因為 IP geolocation 有時候不是很準,但是,實際上不需要十分精準,所以還是可以用。如果真的不確定,也可以先用 tzupdate -p 去抓到的timezone 是否正確。
tzupdate -p
source code:
https://github.com/cdown/tzupdate
再配合 network-manager 讓連網後自動調整時間
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #!/bin/sh
IF=$1
STATUS=$2
wait_for_process() {
PNAME=$1
PID=`/usr/bin/pgrep $PNAME`
while [ -z "$PID" ]; do
sleep 3;
PID=`/usr/bin/pgrep $PNAME`
done
}
start_update_timezone() {
wait_for_process nm-applet
current_TZ=$(tzupdate -p | sed "s/Detected timezone is //g"|sed s/\\.//g)
system_TZ=$(ls -l /etc/localtime | sed s@.*zoneinfo/@@g)
if [ "$current_TZ" != "$system_TZ" ]; then
tzupdate
else
fi
}
if [ "$STATUS" = "up" ]; then
start_update_timezone
exit $?
fi
|
把檔案 copy 到 /etc/NetworkManager/dispatcher.d/99_auto_update_tz
記得變更屬性為可執行檔!
以後出國,就不怕忘記調整摟
Comments