c++ - 如何通过/etc/localtime的时区信息逆向查找linux当前设定的城市信息
ringa_lee
ringa_lee 2017-04-17 11:24:50
0
1
771

已知linux系统大多通过例如:

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

来设定当前时区。

那么如何通过这个localtime文件逆向查找到本系统设置的城市信息呢?

ringa_lee
ringa_lee

ringa_lee

reply all(1)
大家讲道理

Regarding your question, if it is a soft link file, you can obtain the source file address through the readlink command:

# readlink /etc/localtime
/usr/share/zoneinfo/Asia/Shanghai

If it is not a link file, it can be identified by comparing md5, because in most environments it should exist in the form of a soft link, so I actually copied the file here to test:

# cp /usr/share/zoneinfo/Asia/Shanghai abc
# m=`md5sum abc | awk '{print }'`
# find /usr/share/zoneinfo -type f | xargs md5sum | grep "$m"
c103f379c73f61b9eaf39a9a8e0c2cb1 /usr/share/zoneinfo/Asia/Shanghai
c103f379c73f61b9eaf39a9a8e0c2cb1 /usr/share/zoneinfo/PRC

If there is further need, for example, use a script to implement:

if [ -L “/etc/localtime” ]; then  # is symbolic link
    ...
else # normal file
    ...
fi
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template