在bash Shell脚本中将带有偏移量字符串的GMT转换为MST

问题描述

我有以下格式的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 +0000bash转换为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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...