linux shell utc时间,如何在Linux上的shell腳本中將UTC轉換為本地時間

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腳本中將其轉換為本地時間。我認為我可以直接將其作為輸入,但我似乎無法告訴日期輸入日期的格式。

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”的格式是什么?我試圖谷歌它沒有成功。

3 个解决方案

#1

3

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

這是組合日期和時間的ISO8601“基本格式”。 date似乎無法解析20110724T080000Z,但是如果你准備做幾個字符串替換它正確解析20110724 08:00:00Z。

#2

1

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)

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.

date [-u | --utc | --universal] [mmddHHMM [[cc] yy] [[。SS]]此表單唯一有效的選項指定協調世界時。

#3

0

You might try taking advantage of perl:

您可以嘗試利用perl:

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

perl -e'print scalar localtime(shift),“\ 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值