通过命令行转换时区

hen*_*dry 29 linux time date command-line conversion

我知道http://www.timeanddate.com/worldclock/converter.html

我不知道如何以“太平洋标准时间下午 5 点 BST”等理智自然的格式查询http://www.google.com

还是我必须编写这样的应用程序?

hen*_*dry 51

台北是下午 6 点,这里是几点?

date --date='TZ="Asia/Taipei" 18:00'
Fri Jul 16 11:00:00 BST 2010
Run Code Online (Sandbox Code Playgroud)

伦敦上午 11 点,台北几点?

TZ=Asia/Taipei date -d "11:00 BST"
Fri Jul 16 18:00:00 CST 2010
Run Code Online (Sandbox Code Playgroud)

  • 要查找时区标识符,请使用以下内容:`timedatectl list-timezones|grep -i taipei`(打印`Asia/Taipei`)、`timedatectl list-timezones|grep -i berlin`(打印`Europe/Berlin`)、` timedatectl list-timezones|grep -i angeles`(打印 `America/Los_Angeles`) (3认同)

Dav*_*idG 9

我认为这更接近 OP 所问的(因为他不一定知道 BST 是台北?并且答案没有解释如何从“BST”到达“亚洲/台北”)。

首先我的当前日期:

$ date
Mon Apr 21 13:07:21 MDT 2014
Run Code Online (Sandbox Code Playgroud)

然后我想知道的日期:

$ date -d '5pm BST'
Mon Apr 21 15:00:00 MDT 2014
Run Code Online (Sandbox Code Playgroud)

所以我知道那5pm BST是2个小时的路程。

我通常会忘记是否必须在 EDT 时间中添加或删除两个小时,因此我有一个包含常用时区的小脚本:

$ cat tz
#!/bin/bash
TZ='America/Edmonton' date
TZ='America/Chicago' date
TZ='America/New_York' date
Run Code Online (Sandbox Code Playgroud)

和输出:

$ tz
Mon Apr 21 13:12:32 MDT 2014
Mon Apr 21 14:12:32 CDT 2014
Mon Apr 21 15:12:32 EDT 2014
Run Code Online (Sandbox Code Playgroud)

tz可以在此处找到脚本的有效位置/usr/share/zoneinfo

但同样,在未来的时间里,我只使用date -d '<time> <timezone>'.

  • 由于某种原因,这不适用于 macOS 附带的“date”版本。我以前也遇到过类似的问题,并且使用过 GNU coreutils。如果您使用 Homebrew,请执行“brew install coreutils”,然后使用“gdate”(或“gdd”等),那么这个答案就有效。 (2认同)

pix*_*eat 8

这个例子来自http://www.pixelbeat.org/cmdline.html#dates

它给出了对应于美国西海岸上午 9 点的当地时间,考虑了不同的夏令时转换。

date --date='TZ="America/Los_Angeles" 09:00 next Fri'
Run Code Online (Sandbox Code Playgroud)

使用 tzselect 获取 TZ。PST 格式不明确。例如,IST = 印度标准时间和爱尔兰夏令时。

  • 不知道`tzselect`,谢谢。如果您输入错误的“TZ”输入,您可能会得到误导性结果,例如 TZ=London date Fri Jul 16 10:28:52 London 2010 (3认同)

小智 5

我知道这是一个旧线程,但我需要相同用例的代码,并根据此处的想法开发了这个小 bash 脚本:

#!/bin/bash
# ig20180122 - displays meeting options in other time zones
# set the following variable to the start and end of your working day
myday="8 20" # start and end time, with one space
# set the local TZ
myplace='America/Sao_Paulo'
# set the most common places
place[1]='America/Toronto'
place[2]='America/Chicago' # Houston as well
place[3]='Europe/Amsterdam'
place[4]='Europe/Dublin'
# add cities using place[5], etc.
# set the date format for search
dfmt="%m-%d" # date format for meeting date
hfmt="+%B %e, %Y" # date format for the header
# no need to change onwards
format1="%-10s " # Increase if your cities are large
format2="%02d "
mdate=$1
if [[ "$1" == "" ]]; then mdate=`date "+$dfmt"`; fi
date -j -f "$dfmt" "$hfmt" "$mdate"
here=`TZ=$myplace date -j -f "$dfmt" +%z  "$mdate"`
here=$((`printf "%g" $here` / 100))
printf "$format1" "Here" 
printf "$format2" `seq $myday` 
printf "\n"
for i in `seq 1 "${#place[*]}"`
do
    there=`TZ=${place[$i]} date -j -f "$dfmt" +%z  "$mdate"`
    there=$((`printf "%g" $there` / 100))
    city[$i]=${place[$i]/*\//}
    tdiff[$i]=$(($there - $here))
    printf "$format1" ${city[$i]}
    for j in `seq $myday`
    do
        printf "$format2" $(($j+${tdiff[$i]}))
    done
    printf "(%+d)\n" ${tdiff[$i]}
done
Run Code Online (Sandbox Code Playgroud)

您可以使用检查今天或未来日期的时差:

16:08 $ meet
January 22, 2019
Here       08 09 10 11 12 13 14 15 16 17 18 19 20 
Toronto    05 06 07 08 09 10 11 12 13 14 15 16 17 (-3)
Chicago    04 05 06 07 08 09 10 11 12 13 14 15 16 (-4)
Amsterdam  11 12 13 14 15 16 17 18 19 20 21 22 23 (+3)
Dublin     10 11 12 13 14 15 16 17 18 19 20 21 22 (+2)
16:13 $ meet 05-24
May 24, 2019
Here       08 09 10 11 12 13 14 15 16 17 18 19 20 
Toronto    07 08 09 10 11 12 13 14 15 16 17 18 19 (-1)
Chicago    06 07 08 09 10 11 12 13 14 15 16 17 18 (-2)
Amsterdam  13 14 15 16 17 18 19 20 21 22 23 24 25 (+5)
Dublin     12 13 14 15 16 17 18 19 20 21 22 23 24 (+4)
16:13 $ 
Run Code Online (Sandbox Code Playgroud)

华泰