Using time.google.com programatically

1,872 views
Skip to first unread message

רות ומיכאל האפרתי

unread,
Mar 7, 2017, 7:46:34 AM3/7/17
to public-ntp-discuss
Are there code samples for obtaining the current date and time from a software program? c++ would be best. 

Chris Schrier

unread,
Mar 7, 2017, 10:54:55 AM3/7/17
to רות ומיכאל האפרתי, public-ntp-discuss
The time.google.com service provides NTP, most NTP libraries should work. You can find several examples of NTP clients written in C++ on GitHub and StackOverflow.

I do not know your use case but you should be aware of inaccuracy caused by network latency and understand that changing the system clock, especially backwards, can have unexpected consequences. 

On Tue, Mar 7, 2017 at 7:46 AM, רות ומיכאל האפרתי <myma...@gmail.com> wrote:
Are there code samples for obtaining the current date and time from a software program? c++ would be best. 

--
You received this message because you are subscribed to the Google Groups "public-ntp-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to public-ntp-discuss+unsub...@googlegroups.com.
To post to this group, send email to public-ntp-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/public-ntp-discuss/5e157606-aab7-4da5-a9c8-f13a2da2baa5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eugen S

unread,
Mar 11, 2017, 3:43:36 AM3/11/17
to public-ntp-discuss, myma...@gmail.com
I suppose, this question is interesting not only to C++ coders. 
I have to get time on a controller. I can establish UDP connection to an ntp server but no libraries for udp are available for me.
So, can i manually send a request to UDP 216.239.35.0:123 and get a string to parse? I tried sending empty messages with packet sender, but it didn't work.
Or are there any services (not NTP, but constant IP), which can respond my TCP or UDP request with a string containing time and date?

вторник, 7 марта 2017 г., 18:54:55 UTC+3 пользователь Chris Schrier написал:
The time.google.com service provides NTP, most NTP libraries should work. You can find several examples of NTP clients written in C++ on GitHub and StackOverflow.

I do not know your use case but you should be aware of inaccuracy caused by network latency and understand that changing the system clock, especially backwards, can have unexpected consequences. 
On Tue, Mar 7, 2017 at 7:46 AM, רות ומיכאל האפרתי <myma...@gmail.com> wrote:
Are there code samples for obtaining the current date and time from a software program? c++ would be best. 

--
You received this message because you are subscribed to the Google Groups "public-ntp-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to public-ntp-discuss+unsub...@googlegroups.com.
To post to this group, send email to public-nt...@googlegroups.com.

Jamie Wilkinson

unread,
Mar 11, 2017, 6:12:21 AM3/11/17
to Eugen S, public-ntp-discuss, myma...@gmail.com
You can send NTP packets to that IP and it will respond to them.  Sending an empty packet won't work.

If you read the NTP v3 spec (RFC 1305) or any later one, and implement that on your controller, then you should have some success.

(You should also be able to send packets to *any* public NTP server, e.g. pool.ntp.org, to test your controller code.)



On 11 March 2017 at 19:43, Eugen S <letit...@gmail.com> wrote:
I suppose, this question is interesting not only to C++ coders. 
I have to get time on a controller. I can establish UDP connection to an ntp server but no libraries for udp are available for me.
So, can i manually send a request to UDP 216.239.35.0:123 and get a string to parse? I tried sending empty messages with packet sender, but it didn't work.
Or are there any services (not NTP, but constant IP), which can respond my TCP or UDP request with a string containing time and date?

вторник, 7 марта 2017 г., 18:54:55 UTC+3 пользователь Chris Schrier написал:
The time.google.com service provides NTP, most NTP libraries should work. You can find several examples of NTP clients written in C++ on GitHub and StackOverflow.

I do not know your use case but you should be aware of inaccuracy caused by network latency and understand that changing the system clock, especially backwards, can have unexpected consequences. 
On Tue, Mar 7, 2017 at 7:46 AM, רות ומיכאל האפרתי <myma...@gmail.com> wrote:
Are there code samples for obtaining the current date and time from a software program? c++ would be best. 

--
You received this message because you are subscribed to the Google Groups "public-ntp-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to public-ntp-discuss+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "public-ntp-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to public-ntp-discuss+unsub...@googlegroups.com.

Eugen S

unread,
Mar 13, 2017, 10:30:23 AM3/13/17
to public-ntp-discuss, letit...@gmail.com, myma...@gmail.com
Let this reply stay here:
  • send query of 48 bytes: first is 0x1B, others 0x00 to time1.google.com:123 UDP
  • get 48 bytes response
  • Bytes 41-44 of 48 (if counted from 0 to 47 then you need 40-43) are your timestamp in seconds from year 1900
  • enjoy
get and convert timestamps (epoch, from year 1970) https://www.epochconverter.com

суббота, 11 марта 2017 г., 14:12:21 UTC+3 пользователь Jamie Wilkinson написал:
You can send NTP packets to that IP and it will respond to them.  Sending an empty packet won't work.

If you read the NTP v3 spec (RFC 1305) or any later one, and implement that on your controller, then you should have some success.

(You should also be able to send packets to *any* public NTP server, e.g. pool.ntp.org, to test your controller code.)


On 11 March 2017 at 19:43, Eugen S <letit...@gmail.com> wrote:
I suppose, this question is interesting not only to C++ coders. 
I have to get time on a controller. I can establish UDP connection to an ntp server but no libraries for udp are available for me.
So, can i manually send a request to UDP 216.239.35.0:123 and get a string to parse? I tried sending empty messages with packet sender, but it didn't work.
Or are there any services (not NTP, but constant IP), which can respond my TCP or UDP request with a string containing time and date?

вторник, 7 марта 2017 г., 18:54:55 UTC+3 пользователь Chris Schrier написал:
The time.google.com service provides NTP, most NTP libraries should work. You can find several examples of NTP clients written in C++ on GitHub and StackOverflow.

I do not know your use case but you should be aware of inaccuracy caused by network latency and understand that changing the system clock, especially backwards, can have unexpected consequences. 
On Tue, Mar 7, 2017 at 7:46 AM, רות ומיכאל האפרתי <myma...@gmail.com> wrote:
Are there code samples for obtaining the current date and time from a software program? c++ would be best. 

--
You received this message because you are subscribed to the Google Groups "public-ntp-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to public-ntp-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "public-ntp-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to public-ntp-discuss+unsub...@googlegroups.com.
To post to this group, send email to public-nt...@googlegroups.com.

Jamie Wilkinson

unread,
Mar 13, 2017, 8:12:26 PM3/13/17
to Eugen S, public-ntp-discuss, myma...@gmail.com

Thanks for the followup, Eugen!


To unsubscribe from this group and stop receiving emails from it, send an email to public-ntp-disc...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "public-ntp-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to public-ntp-disc...@googlegroups.com.

To post to this group, send email to public-nt...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "public-ntp-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to public-ntp-disc...@googlegroups.com.

To post to this group, send email to public-nt...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages