linux date 设置输出时间的时区

中国时区为UTC +0800,date命令前加TZ设置,仅生效一次。



root@ubuntu:~# TZ=UTC-8 date +”%Y-%m-%d %H:%M:%S”
2015-11-05 10:50:42



root@ubuntu:~# TZ=UTC-8 date +’%Z %z’
UTC +0800


The reason is that TZ=UTC-8 is interpreted as a POSIX time zone. In the POSIX timezone format, the 3 letters are the timezone abbreviation (which is arbitrary) and the number is the number of hours the timezone is behind UTC. So UTC-8 means a timezone abbreviated “UTC” that is −8 hours behind the real UTC, or UTC + 8 hours.

(It works that way because Unix was developed in the US, which is behind UTC. This format allows the US timezones to be represented as EST5, CST6, etc.)

You can see that’s what’s happening by these examples:

$ TZ=UTC-8 date +'%Z %z'
UTC +0800
$ TZ=UTC8 date +'%Z %z'
UTC -0800
$ TZ=FOO-8 date +'%Z %z'
FOO +0800

The ISO -0800 timezone format takes the opposite approach, with - indicating the zone is behind UTC, and + indicating the zone is ahead of UTC.

About 智足者富


电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>