如何在 Linux 上的 shell 脚本中将 UTC 转换为本地时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6788070/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:12:19  来源:igfitidea点击:

How to convert UTC to local time in a shell script on linux

linuxshelldateutc

提问by JohnSmith

I have a string of the format

我有一个格式的字符串

20110724T080000Z

and I want to convert that to local time in a shell script on linux. I thought I simply could give it as input to date, but I don't seem to be able to tell date what format my input date has.

我想在 linux 上的 shell 脚本中将其转换为本地时间。我以为我可以简单地将其作为日期的输入,但我似乎无法告诉 date 我的输入日期具有什么格式。

this

这个

date -d "20110724T080000Z" -u

would make date complain

会让约会抱怨

date: invalid date `20110724T080000Z'

Also, what is the format of the form "20110724T080000Z" called? I have had little success trying to google for it.

另外,表格“20110724T080000Z”的格式是什么?我试图用谷歌搜索它几乎没有成功。

采纳答案by Anders Lindahl

That's ISO8601 "basic format"for a combined date and time. datedoes not seem to be able to parse 20110724T080000Z, but if you are prepared to do a few string substitutions it parses 20110724 08:00:00Zcorrectly.

这是组合日期和时间的ISO8601“基本格式”date似乎无法 parse 20110724T080000Z,但如果您准备进行一些字符串替换,它可以20110724 08:00:00Z正确解析。

回答by nemesisfixx

You might try taking advantage of perl:

您可以尝试利用 perl:

perl -e 'print scalar localtime(shift), "\n"' 20110724T080000Z

perl -e '打印标量本地时间(移位),“\n”' 20110724T080000Z

Though u might have to tweak this a little to get it to work correctly. Ok, I don't know exactly why the perl version doesn't do it well, but here is a Ruby version I've tried, though I can't pick the time out well:

虽然你可能需要稍微调整一下才能让它正常工作。好吧,我不知道为什么 perl 版本做得不好,但这是我尝试过的 Ruby 版本,虽然我不能很好地选择时间:

ruby -e "require 'date';print Date.strptime('20110724T080000Z','%Y%m%dT%H%M%SZ').ctime"

ruby -e "require 'date';print Date.strptime('20110724T080000Z','%Y%m%dT%H%M%SZ').ctime"

gives:

给出:

Sun Jul 24 00:00:00 2011

2011 年 7 月 24 日星期日 00:00:00

回答by J-e-L-L-o

its called Zulu time. Its the same as UCT, which used to be referred to as GMT. It's used with the military to specify UCT so there is no confusion on correspondance.

它被称为祖鲁时间。它与UCT相同,以前称为GMT。它与军方一起用于指定 UCT,因此不会混淆对应关系。

http://en.wikipedia.org/wiki/Date_(Unix)

http://en.wikipedia.org/wiki/Date_(Unix)

this command should work according to wikipedia:

这个命令应该根据维基百科工作:

date [-u|--utc|--universal] [mmddHHMM[[cc]yy][[.SS]] The only valid option for this form specifies Coordinated Universal Time.

日期 [-u|--utc|--universal] [mmddHHMM[[cc]yy][[.SS]] 此格式的唯一有效选项指定协调世界时。

回答by Small Boy

The dateprogram recognizes yyyy-mm-ddTHH:MM:SS(as well as yyyy-mm-dd HH:MM:SS), so:

date程序识别yyyy-mm-ddTHH:MM:SS(以及yyyy-mm-dd HH:MM:SS),因此:

a=20110724T080000Z
b=${a:0:4}-${a:4:2}-${a:6:5}:${a:11:2}:${a:13:2}
date +%F_%T -d "${b} +0"

Would print 2011-07-24_12:30:00in my locale.

2011-07-24_12:30:00在我的语言环境中打印。