如何在 Linux 上的 shell 脚本中将 UTC 转换为本地时间
我有一个格式的字符串
20110724T080000Z
,我想在 Linux 上的 shell 脚本中将其转换为本地时间。我以为我可以将其作为迄今为止的输入,但我似乎无法告诉日期我输入日期的格式。
这
date -d "20110724T080000Z" -u
会让日期抱怨
date: invalid date `20110724T080000Z'
另外,“20110724T080000Z”表格的格式是什么?我尝试用谷歌搜索它但没有成功。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是组合日期和时间的ISO8601“基本格式”。
date
似乎无法解析20110724T080000Z
,但如果您准备进行一些字符串替换,它会解析20110724 08:00:00Z
> 正确。That's ISO8601 "basic format" for a combined date and time.
date
does not seem to be able to parse20110724T080000Z
, but if you are prepared to do a few string substitutions it parses20110724 08:00:00Z
correctly.date
程序识别这 2 个:yyyy-mm-ddTHH:MM:SS
yyyy-mm-dd HH:MM:SS
所以:
会在我的语言环境中打印
2011-07-24_12:30:00
。The
date
program recognizes these 2:yyyy-mm-ddTHH:MM:SS
yyyy-mm-dd HH:MM:SS
So:
Would print
2011-07-24_12:30:00
in my locale.它被称为祖鲁时间。它与 UCT 相同,过去被称为 GMT。它与军队一起使用来指定 UCT,因此不会出现通信方面的混乱。
http://en.wikipedia.org/wiki/Date_(Unix)
此命令应该根据维基百科工作:
date [-u|--utc|--universal] [mmddHHMM[[cc]yy][[.SS]] 此形式的唯一有效选项指定协调世界时。
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.
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.
您可以尝试利用 perl:
不过你可能需要稍微调整一下才能使其正常工作。好吧,我不知道为什么 Perl 版本做得不好,但这是我尝试过的 Ruby 版本,尽管我不能很好地选择时间:
给出:
You might try taking advantage of perl:
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:
gives: