Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 905
JohnSmith Avatar asked Jul 22 '11 09:07

JohnSmith


2 Answers

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.

like image 163
Anders Lindahl Avatar answered Sep 16 '22 19:09

Anders Lindahl


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

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:00 in my locale.

like image 39
Small Boy Avatar answered Sep 18 '22 19:09

Small Boy