问题描述
我有以下格式的String变量(具有GMT偏移量的日期时间)。 如何在bash shell脚本中将其转换为MST?
Input GMT :- 08/Sep/2020:11:38:01 +0000
Output MST :- 08/Sep/2020:04:38:01 -0700
我们可以获得这样的偏移量
offset=$(date +%-z)
我不想再次从字符串转换为日期,然后使用offset和负offset达到MST。是否有更好的转换方法?
解决方法
将08/Sep/2020:11:38:01 +0000
和bash
转换为08 Sep 2020 11:38:01 +0000
:
gmt="08/Sep/2020:11:38:01 +0000"
gmt="${gmt//\// }" # replace all / with spaces
gmt="${gmt/:/ }" # replace first : with space
然后将其与GNU日期一起使用:
TZ="MST" date --date="$gmt" +'%d/%h/%Y:%H:%M:%S %z'
输出:
08/Sep/2020:04:38:01 -0700