How to get date&time using curl command?

42,371

Solution 1

First, curl makes http(s) requests. Assuming that you have a webserver listening on the machine, so you have 2 options:

  1. Create a script (php/asp/node) that gives you the date and time as response to the request.

  2. Get the time from server response headers.

    curl -v https://google.com/
    

    this will give you among other things, a line like this

    ...
    < Date: Wed, 02 Mar 2016 18:39:13 GMT
    ...
    

    With that result, you can parse and transform to the format that you want.

    dateFromServer=$(curl -v --silent https://google.com/ 2>&1 \
       | grep Date | sed -e 's/< Date: //'); date +"%d%m%Y%H%M%S" -d "$dateFromServer"
    

    You must note the difference of GMT vs. local time, and adapt it to your needs.

Update 2018-11-26:
As @SopalajodeArrierez noted, it's not possible to run this when the Certificates are not updated or the time is way off the correct value, so the --insecure options is needed on curl command.

    dateFromServer=$(curl -v --insecure --silent https://google.com/ 2>&1 \
       | grep Date | sed -e 's/< Date: //'); date +"%d%m%Y%H%M%S" -d "$dateFromServer"

Solution 2

Get the date from a HTTP response header. Remove clutter. Set the date.

$ date -s `curl -I 'https://google.com/' 2>/dev/null | grep -i '^date:' | sed 's/^[Dd]ate: //g'`

Hope it helps.

Solution 3

If you must get the time from a remote server, use an NTP server.

For example:

$ ntpq -c 'rv 0 clock' localhost
clock=da8b6fe7.ae195287  Thu, Mar 10 2016  9:30:39.680

The clock value is the an NTP timestamp in hex (the epoch being 01/01/1900). You can process it a bit to get an Unix timestamp which you can then use with date:

$ ntpq -c raw -c 'rv 0 clock' localhost
Output set to raw
clock=0xda8b775d.ee94c630
$ ntpq -c raw -c 'rv 0 clock' localhost | gawk -F '[ =.]' --non-decimal-data 'NR==2{printf "@%d.%d\n", $2 - 2209075200, "0x"$3}'
@1457497973.4177870717
$ ntpq -c raw -c 'rv 0 clock' localhost | gawk -F '[=.]' --non-decimal-data 'NR==2{printf "@%d.%d\n", $2 - 2209075200, "0x"$3}' | xargs date +'%d%m%Y%H%M%S' -d 
09032016100323

In my case, I have an NTP daemon running locally, which I could query, hence the usage of localhost. You could use, instead, one of the many public NTP servers, such as ntp.ubuntu.com.

The awk command reads the line, splits it on = and ., so that the three fields are clock, the integer part of the timestamp and the fractional part. Then, it subtracts 2209075200 (the difference in seconds between the Unix and NTP epochs, as obtained from this SO post) from the integer part and prints both the parts in decimal.

Solution 4

Here's how I do it. I keep a switch statement for translating from the three character month representation returned from a server to the digit value of the month. This should work on many standard Unix environments, as long as curl, echo, cut, date, and bash are installed.

    #!/bin/bash
    # Automatically Updates System Time According to the NIST Atomic Clock in a Linux Environment
    nistTime=$(curl -I --insecure 'https://nist.time.gov/' | grep "Date")
    echo $nistTime
    dateString=$(echo $nistTime | cut -d' ' -f2-7)
    dayString=$(echo $nistTime | cut -d' ' -f2-2)
    dateValue=$(echo $nistTime | cut -d' ' -f3-3)
    monthValue=$(echo $nistTime | cut -d' ' -f4-4)
    yearValue=$(echo $nistTime | cut -d' ' -f5-5)
    timeValue=$(echo $nistTime | cut -d' ' -f6-6)
    timeZoneValue=$(echo $nistTime | cut -d' ' -f7-7)
    #echo $dateString
    case $monthValue in
        "Jan")
            monthValue="01"
            ;;
        "Feb")
            monthValue="02"
            ;;
        "Mar")
            monthValue="03"
            ;;
        "Apr")
            monthValue="04"
            ;;
        "May")
            monthValue="05"
            ;;
        "Jun")
            monthValue="06"
            ;;
        "Jul")
            monthValue="07"
            ;;
        "Aug")
            monthValue="08"
            ;;
        "Sep")
            monthValue="09"
            ;;
        "Oct")
            monthValue="10"
            ;;
        "Nov")
            monthValue="11"
            ;;
        "Dec")
            monthValue="12"
            ;;
        *)
            continue
    esac
    date --utc --set $yearValue.$monthValue.$dateValue-$timeValue
Share:
42,371

Related videos on Youtube

manikanta
Author by

manikanta

Working on linux device drivers for ARM. Having knowledge on Linux OS customization ( u-boot, kernel , file system ) , Ubuntu Desktop OS.

Updated on September 18, 2022

Comments

  • manikanta
    manikanta over 1 year

    In my PC, RTC is not working, So I want Date&Time from server using curl command or is there any possibilities to get date and time without RTC in PC. Required format for date&time is DDMMYYYYHHMMSS.

    • Vlad Tarniceru
      Vlad Tarniceru about 8 years
      I'm curious, why you need to do this using curl?
    • manikanta
      manikanta about 8 years
      In my PC RTC Not working. But, i want time Stamp in my code. So i use Curl to do that.
  • manikanta
    manikanta about 7 years
    getting like this not working...root@sai:~# curl -v https://google.cl * Rebuilt URL to: https://google.cl/ * Hostname was NOT found in DNS cache * Could not resolve host: google.cl * Closing connection 0 curl: (6) Could not resolve host: google.cl
  • bistoco
    bistoco about 7 years
    what about using https://google.com/
  • Sopalajo de Arrierez
    Sopalajo de Arrierez over 5 years
    Not possible to run this curl command from some wrong date's computer (i.e: date at 2006 year): there will be a SSL certificate problem: certificate is not yet valid. In this case, I have managed to solve it with the -k (--insecure) option.
  • Boop
    Boop over 4 years
    Alternatively if you just want to print: date -d "$(curl -sI google.com| grep -i '^date:'|cut -d' ' -f2-)" also when I tested, google.co was really offset : ~20mins. others where ok though. For Info I use this to check if my VM is ntp desync (in addition of chrony checks)