File: get-timezone.sh

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (56 lines) | stat: -rwxr-xr-x 1,187 bytes parent folder | download | duplicates (2)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash

# This script provides a fallback for systems where `timedatectl show
# -p Timezone --value` does not work

set -eu

if timezone="$(timedatectl show -p Timezone --value 2>/dev/null)"; then
    echo "${timezone}"
    exit 0
fi

read_abs_symlink() {
    local target
    local fromdir

    target=$(readlink "${1}")
    case "${target}" in
        /*)
            echo "${target}"
            ;;
        *)
            fromdir=$(dirname "${1}")
            realpath "${fromdir}/${target}"
            ;;
    esac
}

read_localtime() {
    local target

    if ! [ -L "${1}" ]; then
        echo "${1} is not a symlink" 1>&2
        exit 1
    fi

    target="$(read_abs_symlink "${1}")"

    case "${target}" in
        /usr/share/zoneinfo/*)
            echo "${target#/usr/share/zoneinfo/}"
            ;;
        *)
            read_localtime "${target}"
            ;;
    esac
}

if [ -L /etc/localtime ]; then
    read_localtime /etc/localtime
elif [ -f /etc/timezone ]; then
    # Initial /etc/localtime from postinst on old Debian/Ubuntu copies
    # /etc/localtime instead of linking. Trying to recover the
    # timezone from /etc/timezone.
    cat /etc/timezone
fi