How to get date&time using curl command?
up vote
-1
down vote
favorite
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
.
command-line curl
add a comment |
up vote
-1
down vote
favorite
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
.
command-line curl
I'm curious, why you need to do this usingcurl
?
– Vlad Tarniceru
Mar 2 '16 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 '16 at 7:29
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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
.
command-line curl
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
.
command-line curl
command-line curl
edited Jul 13 '17 at 12:50
αғsнιη
24.1k2294155
24.1k2294155
asked Mar 2 '16 at 17:38
manikanta
872213
872213
I'm curious, why you need to do this usingcurl
?
– Vlad Tarniceru
Mar 2 '16 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 '16 at 7:29
add a comment |
I'm curious, why you need to do this usingcurl
?
– Vlad Tarniceru
Mar 2 '16 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 '16 at 7:29
I'm curious, why you need to do this using
curl
?– Vlad Tarniceru
Mar 2 '16 at 17:49
I'm curious, why you need to do this using
curl
?– Vlad Tarniceru
Mar 2 '16 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 '16 at 7:29
In my PC RTC Not working. But, i want time Stamp in my code. So i use Curl to do that.
– manikanta
Mar 5 '16 at 7:29
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
First, curl
makes http(s) requests. Assuming that you have a webserver listening on the machine, so you have 2 options:
Create a script (php/asp/node) that gives you the date and time as response to the request.
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"
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 '17 at 11:36
what about usinghttps://google.com/
– bistoco
Feb 10 '17 at 19:12
1
Not possible to run thiscurl
command from some wrong date's computer (i.e: date at 2006 year): there will be aSSL certificate problem: certificate is not yet valid
. In this case, I have managed to solve it with the-k
(--insecure
) option.
– Sopalajo de Arrierez
Nov 26 at 4:39
add a comment |
up vote
1
down vote
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.%dn", $2 - 2209075200, "0x"$3}'
@1457497973.4177870717
$ ntpq -c raw -c 'rv 0 clock' localhost | gawk -F '[=.]' --non-decimal-data 'NR==2{printf "@%d.%dn", $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.
add a comment |
up vote
0
down vote
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.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
First, curl
makes http(s) requests. Assuming that you have a webserver listening on the machine, so you have 2 options:
Create a script (php/asp/node) that gives you the date and time as response to the request.
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"
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 '17 at 11:36
what about usinghttps://google.com/
– bistoco
Feb 10 '17 at 19:12
1
Not possible to run thiscurl
command from some wrong date's computer (i.e: date at 2006 year): there will be aSSL certificate problem: certificate is not yet valid
. In this case, I have managed to solve it with the-k
(--insecure
) option.
– Sopalajo de Arrierez
Nov 26 at 4:39
add a comment |
up vote
3
down vote
accepted
First, curl
makes http(s) requests. Assuming that you have a webserver listening on the machine, so you have 2 options:
Create a script (php/asp/node) that gives you the date and time as response to the request.
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"
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 '17 at 11:36
what about usinghttps://google.com/
– bistoco
Feb 10 '17 at 19:12
1
Not possible to run thiscurl
command from some wrong date's computer (i.e: date at 2006 year): there will be aSSL certificate problem: certificate is not yet valid
. In this case, I have managed to solve it with the-k
(--insecure
) option.
– Sopalajo de Arrierez
Nov 26 at 4:39
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
First, curl
makes http(s) requests. Assuming that you have a webserver listening on the machine, so you have 2 options:
Create a script (php/asp/node) that gives you the date and time as response to the request.
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"
First, curl
makes http(s) requests. Assuming that you have a webserver listening on the machine, so you have 2 options:
Create a script (php/asp/node) that gives you the date and time as response to the request.
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"
edited Nov 26 at 18:02
answered Mar 2 '16 at 18:45
bistoco
1,014717
1,014717
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 '17 at 11:36
what about usinghttps://google.com/
– bistoco
Feb 10 '17 at 19:12
1
Not possible to run thiscurl
command from some wrong date's computer (i.e: date at 2006 year): there will be aSSL certificate problem: certificate is not yet valid
. In this case, I have managed to solve it with the-k
(--insecure
) option.
– Sopalajo de Arrierez
Nov 26 at 4:39
add a comment |
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 '17 at 11:36
what about usinghttps://google.com/
– bistoco
Feb 10 '17 at 19:12
1
Not possible to run thiscurl
command from some wrong date's computer (i.e: date at 2006 year): there will be aSSL certificate problem: certificate is not yet valid
. In this case, I have managed to solve it with the-k
(--insecure
) option.
– Sopalajo de Arrierez
Nov 26 at 4:39
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 '17 at 11:36
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 '17 at 11:36
what about using
https://google.com/
– bistoco
Feb 10 '17 at 19:12
what about using
https://google.com/
– bistoco
Feb 10 '17 at 19:12
1
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.– Sopalajo de Arrierez
Nov 26 at 4:39
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.– Sopalajo de Arrierez
Nov 26 at 4:39
add a comment |
up vote
1
down vote
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.%dn", $2 - 2209075200, "0x"$3}'
@1457497973.4177870717
$ ntpq -c raw -c 'rv 0 clock' localhost | gawk -F '[=.]' --non-decimal-data 'NR==2{printf "@%d.%dn", $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.
add a comment |
up vote
1
down vote
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.%dn", $2 - 2209075200, "0x"$3}'
@1457497973.4177870717
$ ntpq -c raw -c 'rv 0 clock' localhost | gawk -F '[=.]' --non-decimal-data 'NR==2{printf "@%d.%dn", $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.
add a comment |
up vote
1
down vote
up vote
1
down vote
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.%dn", $2 - 2209075200, "0x"$3}'
@1457497973.4177870717
$ ntpq -c raw -c 'rv 0 clock' localhost | gawk -F '[=.]' --non-decimal-data 'NR==2{printf "@%d.%dn", $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.
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.%dn", $2 - 2209075200, "0x"$3}'
@1457497973.4177870717
$ ntpq -c raw -c 'rv 0 clock' localhost | gawk -F '[=.]' --non-decimal-data 'NR==2{printf "@%d.%dn", $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.
edited May 23 '17 at 12:39
Community♦
1
1
answered Mar 10 '16 at 4:37
muru
135k19288488
135k19288488
add a comment |
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Jul 13 '17 at 11:38
Koutheir Attouchi
1092
1092
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f741298%2fhow-to-get-datetime-using-curl-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm curious, why you need to do this using
curl
?– Vlad Tarniceru
Mar 2 '16 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 '16 at 7:29