5

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.

2
  • I'm curious, why you need to do this using curl? Mar 2, 2016 at 17:49
  • In my PC RTC Not working. But, i want time Stamp in my code. So i use Curl to do that.
    – manikanta
    Mar 5, 2016 at 7:29

7 Answers 7

11

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"
4
  • 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
    – manikanta
    Feb 10, 2017 at 11:36
  • what about using https://google.com/
    – bistoco
    Feb 10, 2017 at 19:12
  • 1
    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. Nov 26, 2018 at 4:39
  • I fixed one of the answers: dateFromServer=$(curl -v --silent https://nist.time.gov/ 2>&1 \ | grep '< Date' | sed -e 's/< Date: //'); date +"%d%m%Y%H%M%S" -d "$dateFromServer" but it would be better to use: dateFromServer=$(curl -v --silent https://nist.time.gov/ 2>&1 \ | grep '< Date' | sed -e 's/< Date: //'); date +"%Y-%m-%d %H:%M:%S" -d "$dateFromServer" for sorting purposes (eventually).
    – pbies
    Jun 4, 2022 at 1:42
4

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.

3

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.

2
  • 1
    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)
    – Boop
    Oct 28, 2019 at 10:17
  • curl -ksI goo.gle | grep -iPo '^Date: \K[\S ]*' got Sat, 29 Oct 2022 21:30:49 GMT //for people who want less pipe cmd (need grep -P PCRE support)
    – yurenchen
    Oct 29, 2022 at 22:00
1

// for some reason ntpdate or htpdate can't work

just share a script (for bash)

to set system date & time from curl (http response header).
// combined others answer

  • curl -k (for https with wrong local time

  • curl -I (use HEAD request more efficient, need server support

  • grep -i (for server response date: or Date: ..

  • grep '^Date:' (avoid other headers like XXX-date: ...

// need grep -P PCRE support
// note: grep '^Date:.*' may print nothing, cause \r trick the output

#!/bin/bash

url="${1:-goo.gle}"

echo "time before: `date -Is`"

echo
time=`curl -ksI "$url" | grep -iPo '^Date: \K[\S ]*'`
echo "time on net: $time"
echo "time parsed: `date -Is -d "$time"`"

[ -n "$time" ] && {
    echo
    echo "time  apply: `date -s "$time"`"
    echo "time  after: `date -Is`"
}

outputs:

$ sudo TZ=GMT-4 ./htpdate.sh bing.com
time before: 2022-10-30T02:10:28+04:00

time on net: Sat, 29 Oct 2022 22:10:28 GMT
time parsed: 2022-10-30T02:10:28+04:00

time  apply: Sun Oct 30 02:10:28 GMT 2022
time  after: 2022-10-30T02:10:28+04:00

TZ=.. to pass timezone for display (only in this script)
arg1 to pass url (default goo.gle)

1

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"
0

use timestamp in htttp reponse header.

date -d "$(curl -sI baidu.com| grep -i '^date:'|cut -d' ' -f2-)"
date -d "$(curl -sI baidu.com| grep -i '^date:'|cut -d' ' -f2-)" | xargs -I {} date --date='{}' "+%Y-%m-%d %H:%M:%S"
# below cmd is set new date
date -s "$(date -d "$(curl -sI baidu.com| grep -i '^date:'|cut -d' ' -f2-)" | xargs -I {} date --date='{}' "+%Y-%m-%d %H:%M:%S")"
0

On Windows this (get date&time using curl), wou be:

CURL -s -I "https://google.com/" | FINDSTR "date:"

response, something like: date: Sat, 03 Jun 2023 15:44:25 GMT

NOTE: "something like", because time has the nature of changing quickly 😉

or, when you just want a date:

FOR /f "usebackq tokens=3,4,5" %f in (`CURL -s -I "https://google.com/" ^| FINDSTR "date:"`) DO ECHO %f %g %h

output: 03 Jun 2023

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .