3

I'm building a list of EBS Volumes in AWS.

I need to put the create time into a more user-friendly format. Management complains about unreadable times.

If I run this command it gives me the time the volume was created in this format:

aws ec2 describe-volumes --volume-ids vol-066e24cb8d2294605 | jq -r  '.Volumes[].CreateTime'

2018-12-11T18:54:26.110Z

I'm trying to format the time using strftime, but it doesn't work. What I get is this error:

aws ec2 describe-volumes --volume-ids vol-066e24cb8d2294605 | jq -r  '.Volumes[] | .CreateTime | strftime ("%Y-%m-%d %H:%M:%S")'

jq: error (at <stdin>:39): strftime/1 requires parsed datetime inputs

What is the problem I'm having? How can I create a user friendly time given the time format provided to me by AWS?

peak
  • 105,803
  • 17
  • 152
  • 177
bluethundr
  • 1,005
  • 17
  • 68
  • 141

1 Answers1

3

You will first have to parse the string. You could use strptime. Here is an illustration of how to do so:

<<< '"2018-12-11T18:54.11Z"' jq -c '
strptime("%Y-%m-%dT%H:%M.%SZ")' 
[2018,11,11,18,54,11,2,344]

This does not quite match your needs, because in this context "%S" has a specific meaning:

%S     The second as a decimal number (range 00 to 60).
       (The range is up to 60 to allow for occasional leap seconds.)

So it looks like you might have to munge the incoming date-time string, e.g.:

if test("\\....Z$") then sub(".Z$"; "Z") else . end
| strptime("%Y-%m-%dT%H:%M.%SZ")
| strftime("%Y-%m-%d %H:%M:%S")
peak
  • 105,803
  • 17
  • 152
  • 177