2011-07-11 04:57:39 +0000 2011-07-11 04:57:39 +0000
135
135
Advertisement

如何查看Linux中的哪个时区?

Advertisement

有什么方法可以查看我目前在Linux中的哪个时区?

Advertisement
Advertisement

答案 (12)

252
252
252
2011-07-11 05:01:26 +0000

如果你是指从控制台,只需输入:

date +%Z
```。
58
58
58
2015-02-13 16:22:27 +0000

如果你想要数字时区:

date +'%:z %Z'

样本输出:

-05:00 EST
26
Advertisement
26
26
2013-08-31 09:58:18 +0000
Advertisement

我想以 “美国/东部 "或 "欧洲/伦敦 "的形式找到时区。你可以在以下目录中找到:

  • /etc/timezone (在Ubuntu和Red Hat系统中存在,但不包括Amazon Linux)
  • (在Red Hat风格的系统中),在/etc/sysconfig/clock 中的ZONE="US/Eastern" - 或者你可以尝试将/etc/localtime匹配到/usr/share/zoneinfo下的一个文件;令人讨厌的是,这似乎不是一个symlink,但你可以例如
21
21
21
2011-07-11 04:59:58 +0000

通常情况下,TZ环境变量会告诉你一些有用的信息。但是,最好使用像mktime()localtime()这样的函数在time_t和本地时区表示之间进行转换。也就是说,不要尝试自己做转换

13
Advertisement
13
13
2016-05-10 10:50:27 +0000
Advertisement

对于ubuntu,可以试试这个:

$ cat /etc/timezone

输出示例:

Asia/Kolkata

其他发行版参考 : https://unix.stackexchange.com/questions/110522/timezone-setting-in-linux

7
7
7
2016-09-13 21:16:56 +0000

有时,你可能要找的是典型的时区,而不是date %Z产生的简略形式,例如US/Eastern。在带timedatectl的系统中,例如Fedora,timedatectl会输出很多有用的信息,包括当前区域:

# timedatectl
      Local time: Tue 2016-09-13 17:10:26 EDT
  Universal time: Tue 2016-09-13 21:10:26 UTC
        RTC time: Tue 2016-09-13 21:10:26
       Time zone: US/Eastern (EDT, -0400)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

不幸的是,timedatectlset-timezone作为命令,但没有相应的get-timezone。解析如下:

# timedatectl status | grep "zone" | sed -e 's/^[]*Time zone: \(.*\) (.*)$//g'`
US/Eastern
4
Advertisement
4
4
2015-07-22 04:58:52 +0000
Advertisement
$ strings /etc/localtime | tail -n 1
MST7MDT,M3.2.0,M11.1.0

所以我是在山地时间。虽然上面关于使用环境变量或仅仅是日期命令输出的建议有时会更好,但这取决于你想如何使用。

3
3
3
2018-07-01 14:02:51 +0000

对于时区,你可以使用地理位置:

$ curl https://ipapi.co/timezone
America/Chicago

或者:

$ curl http://ip-api.com/line?fields=timezone
America/Chicago

http://wiki.archlinux.org/index.php/time#Time /zone

1
Advertisement
1
1
2018-04-16 15:36:04 +0000
Advertisement

几个解决方案:

date +"%Z %z"
timedatectl | grep "Time zone"
cat /etc/timezone

给我的(分别是):

UTC +0000
Time zone: Etc/UTC (UTC, +0000)
Etc/UTC

我的电脑在UTC上。

0
0
0
2018-08-05 07:51:54 +0000

您可以同时显示日期和时区:

date +'%d/%m/%Y %H:%M:%S [%:z %Z]'
0
0
0
2018-06-26 06:54:56 +0000

有时候timedatectl set-timezone不更新/etc/timezone,所以最好从/etc/timezone的symlink指向的文件名中获取tiemzone:

#!/bin/bash
set -euo pipefail

if filename=$(readlink /etc/localtime); then
    # /etc/localtime is a symlink as expected
    timezone=${filename#*zoneinfo/}
    if [[$timezone = "$filename" || ! $timezone =~ ^[^/]+/[^/]+$ ]]; then
        # not pointing to expected location or not Region/City
        >&2 echo "$filename points to an unexpected location"
        exit 1
    fi
    echo "$timezone"
else # compare files by contents
    # https://stackoverflow.com/questions/12521114/getting-the-canonical-time-zone-name-in-shell-script#comment88637393_12523283
    find /usr/share/zoneinfo -type f ! -regex ".*/Etc/.*" -exec \
        cmp -s {} /etc/localtime \; -print | sed -e 's@.*/zoneinfo/@@' | head -n1
fi

参考文献:在本答案中。

0
0
0
2017-11-15 00:31:57 +0000

使用 TZ 或 date 是不可靠的,因为它告诉你的是用户的时区,而不是默认的系统时区。如果你没有 /etc/timezone,请查看 /etc/localtime。一般来说,这就是 “服务器的 "时区。/etc/localtime通常是一个指向/usr/share/zoneinfo中的时区文件的符号链接。

新的linux系统有 "timedatectl",当命令运行时,它可以提供大量的信息。我曾多次这样做来解决旧设备上的时区变化问题)。)

Advertisement

相关问题

6
10
5
37
7
Advertisement
Advertisement