Getting the current time from a website and parsing date

I am trying to work on a script to grab the UTC time from a website

So far I was able to cobble this together.

curl -s --head web-url | grep ^Date: | sed 's/Date: //g'

Which gives me the result I need.

Wed, 06 Dec 2017 21:43:50 GMT

What I need to is extract the 21:43:50 and convert it to seconds for the second part of my script.

What is the best approach to doing this? awk or piping to Sed.

Thanks for the feedback.

curl ... | awk '/^Date:/ { print $6 }'
21:43:50

What do you want to convert to seconds? Just the time?

curl ... | awk -F"[ :]" '/^Date:/ { print $7 * 60^2 + $8 * 60 + $9 }'
78230

Thanks for the info, cant I do this for the awk ?

.... | awk '/^Date:/ { print $6 * 60^2 + $8 * 60 + $9 }'

This seems to work

Does it work? Not really. Without a field separator, $6 is the whole time (h:m:s). $6 * 60^2 does give the right answer for the hours (75600 seconds), with a little help from awk, but $8 and $9 are empty fields, so you won't get the minutes or seconds using that. There are a number of ways you can take the time (h:m:s) and split in into its component parts. Changing the field separator is one, the substr function is another, but doing nothing won't give you the right answer.

Without any field separator, your input looks like this:

$1    $2   $3 $4  $5   $6       $7     $8     $9
Date: Wed, 06 Dec 2017 21:43:50 GMT

Apart from why?, and assuming GNU Date is in your toolkit and assuming a modern bash, ksh or zsh:

seconds=$((  $(date -u +"(%H*60 + %M)*60 + %S" --date="$(curl -s --head web-url | sed -n 's/^Date: //p')") ))

It may take a little longer than a pure awk route and relies on date understanding the format.

Or how about

seconds=$(($(curl -s --head web-url | \
             sed -n '/^Date: / s/^.*\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\).*$/(\1*60 + \2)*60 + \3/p') ))

(pure sed but still relies on $(()) operators in shell)

Andrew