search

Home  >  Q&A  >  body text

c++ - 如何通过/etc/localtime的时区信息逆向查找linux当前设定的城市信息

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

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

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

来设定当前时区。

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

ringa_leeringa_lee2786 days ago751

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 11:26:50

    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
    

    reply
    0
  • Cancelreply