1

My script is capturing time from docker container and next step i want to convert it to some format so i can substitute docker time with current system time and get the difference.

So for now i want at least to convert it so my script will understand that i'm working with the time.

#!/bin/bash

name=$1
matching=$(docker ps -a --filter="name=$name" -q | xargs)
current_time=$(date -u +"%Y-%m-%dT%T")
echo Current time: ${current_time}

for i in $matching
do
    time=sudo docker inspect $i | grep -w "StartedAt" | awk '{gsub(/"/, "", $2); print $2 }' | head -n 1 | cut -c -19
    echo ${time}    
    echo $(date -d +"%Y-%m-%dT%H:%M:%S" --date="$time")
done

The output i'm getting is

Thu Apr 20 00:00:00 PDT 2017
2017-04-19T00:57:15

But i expect to get:

Wed Apr 19 00:57:15 PDT 2017
2017-04-19T00:57:15
Dimu4
  • 213
  • 1
  • 4
  • 18
  • System clock is hardware! In case of virtual host, system clock is based on *Hardware Node* clock (the physical host). I think your goal is wrong... Have a look there: http://stackoverflow.com/a/24568137/1765658 – F. Hauri - Give Up GitHub Apr 21 '17 at 06:26

1 Answers1

1

docker inspect command output is not assigned to time variable. Try command substitution:

#!/bin/bash

name=$1
matching=$(docker ps -a --filter="name=$name" -q | xargs)
current_time=$(date -u +"%Y-%m-%dT%T")
echo Current time: ${current_time}

for i in $matching
do
    time=$(sudo docker inspect $i | grep -w "StartedAt" | awk '{gsub(/"/, "", $2); print $2 }' | head -n 1 | cut -c -19)
    echo ${time}    
    echo $(date -d +"%Y-%m-%dT%H:%M:%S" --date="$time")
done
Kadir
  • 1,664
  • 2
  • 19
  • 22