Reputation: 4376
My bash script is looking at a date-time string that looks like: yyyymmddThhmmss
, for example 20160414T033407
.
I wish to convert this string into date-time format such that I can, for example, change time zone. Currently I am hard-parsing this string and do a -n on the hour to convert the timestamp to my current timezone. However, I see that I have take into account of rolling hours to next days, etc. What is a better approach to this? Thanks.
Upvotes: 0
Views: 461
Reputation: 7
Date doesn't allow "YYYYMMDDHHMMSS", but it does "YYYYMMDD HH:MM:SS", so:
D="20100101123456"
date -d "${D:0:8} ${D:8:2}:${D:10:2}:${D:12:2}"
Upvotes: 1
Reputation: 393
Could break it out with sed and repeat it back in a format other tools will understand.
$ echo '20160414T033407' | sed -r 's/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})/\1-\2-\3 \4:\5:\6/'
2016-04-14 03:34:07
And then feed it to date to get it out in a format you like, for example Unix timestamp:
$ date --date="$(echo '20160414T033407' | sed -r 's/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})/\1-\2-\3 \4:\5:\6/')" +%s
1460630047
Upvotes: 0