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