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 スタイルのシステムでは) ZONE="US/Eastern" として /etc/sysconfig/clock
  • または /etc/localtime を /usr/share/zoneinfo の下にあるファイルの一つにマッチさせることができます; 不愉快なことに、これはシンボリックリンクではないようですが、例えば
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 が生成する短縮形ではなく、正規のタイムゾーンを探しているかもしれません。Fedora のような timedatectl を持つシステムでは、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

タイムゾーンのために、あなたはgeolocationを使用することができます:

$ 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が指すファイル名から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 にあるタイムゾーンファイルへのシンボリックリンクになっています。この正しいタイムゾーンファイルへのパスは、地理情報も提供してくれます。

(サイドノードとして、もし古いハードコーディングされたタイムゾーンを使用している古いシステムを持っているなら、最新のタイムゾーンファイルをコピーしても問題ありません。私は古い機器のタイムゾーンの変更を解決するために何度もこれをしなければなりませんでした)。

Advertisement

関連する質問

6
10
5
37
4
Advertisement