【问题标题】:Convert GMT with offset string to MST in bash Shell script在 bash Shell 脚本中将带有偏移字符串的 GMT 转换为 MST
【发布时间】:2020-09-13 14:43:34
【问题描述】:

我有一个以下格式的字符串变量(带有 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)

我不想再次从字符串转换为日期,然后使用偏移量和负偏移量来达到 MST。有没有办法以更好的方式转换它?

【问题讨论】:

  • this thread 应该为您指明正确的方向。
  • 谢谢@vgersh99,但是这个线程没有TZ的偏移量
  • 这就是%z 的用途。这意味着作为一个“起点”——而不是一个完整的解决方案。您必须稍微“按摩”一下,但想法核心就在那里。

标签: bash shell date timezone date-conversion


【解决方案1】:

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'

输出:

2020 年 9 月 8 日:04:38:01 -0700

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2021-09-18
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多