Bash:从另一个时区获取日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26802201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: get date and time from another time zone
提问by alex_pc89
I would want to get the date and time from another time zone (UTC-4) with a bash command, but I don't want to configure it as the default TZ for the system.
Is there a simple way to do it?
E.g 1 (current):
我想使用 bash 命令从另一个时区 (UTC-4) 获取日期和时间,但我不想将其配置为系统的默认 TZ。
有没有简单的方法来做到这一点?
例如 1(当前):
$ date
fri nov 7 13:15:35 UTC 2014
E.g 2 (what I need):
例如 2(我需要的):
$ date (+ some option for UTC-4)
fri nov 7 09:15:35 UTC 2014
回答by SMA
You could use
你可以用
TZ=America/New_York date
or if you want to do date arithmetic you could use
或者如果你想做日期算术,你可以使用
date -d "+5 hours"
回答by anubhava
You can use offset value in TZ
to get the date for a different timezone:
您可以使用偏移值TZ
来获取不同时区的日期:
TZ=UTC date -R
Fri, 07 Nov 2014 13:55:07 +0000
TZ=UTC+4 date -R
Fri, 07 Nov 2014 09:54:52 -0400
回答by rubo77
If the other solutioons don't work, use
如果其他解决方案不起作用,请使用
date --date='TZ="UTC+4" 2016-08-22 10:37:44' "+%Y-%m-%d %H:%M:%S"
2016-08-22 16:37:44
2016-08-22 16:37:44
回答by mivk
I use a little script for that, so I don't have to know or remember the exact name of the timezone I'm looking for. The script takes a search string as argument, and gives the date and time for any timezone matching the search. I named it wdate
.
我为此使用了一个小脚本,因此我不必知道或记住我正在寻找的时区的确切名称。该脚本将搜索字符串作为参数,并给出与搜索匹配的任何时区的日期和时间。我给它起了名字wdate
。
#!/bin/sh
# Show date and time in other time zones
search=
zoneinfo=/usr/share/zoneinfo/posix/
format='%a %F %T'
find -L $zoneinfo -type f \
| grep -i "$search" \
| while read z
do
d=$(TZ=$z date +"$format")
printf "%-34s %23s\n" ${z#$zoneinfo} "$d"
done
Example output:
示例输出:
$ wdate fax
America/Halifax Wed 2020-01-15 07:55:43
or
或者
$ wdate canad
Canada/Pacific Wed 2020-01-15 04:03:54
Canada/Central Wed 2020-01-15 06:03:54
Canada/Mountain Wed 2020-01-15 05:03:54
Canada/Saskatchewan Wed 2020-01-15 06:03:54
Canada/Atlantic Wed 2020-01-15 08:03:54
Canada/Newfoundland Wed 2020-01-15 08:33:54
Canada/Yukon Wed 2020-01-15 04:03:54
Canada/Eastern Wed 2020-01-15 07:03:54