From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: NTP Date: Sat, 16 Sep 2017 11:29:09 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <65e4e16d-3ab1-46e9-8351-d6185e15905a@googlegroups.com> NNTP-Posting-Host: MajGvm9MbNtGBKE7r8NgYA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 X-Notice: Filtered by postfilter v. 0.8.2 Content-Language: en-US Xref: news.eternal-september.org comp.lang.ada:48143 Date: 2017-09-16T11:29:09+02:00 List-Id: On 2017-09-16 08:40, Anatoly Chernyshev wrote: > Is there a way to get time via the Network Time Protocol in Ada? It > doesn't look like AWS has it (or I've missed something). > > Currently I'm getting around spawning the Cygwin "nc" command, but it hurts my feelings. Do you mean send a single UDP request to an NTP server and convert the response to Ada time? You can try this: ---------------------------------------------------------------------- with Ada.Text_IO; use Ada.Text_IO; with Ada.Calendar; use Ada.Calendar; with Ada.Calendar.Formatting; use Ada.Calendar.Formatting; with Ada.Exceptions; use Ada.Exceptions; with Ada.Streams; use Ada.Streams; with GNAT.Sockets; use GNAT.Sockets; with Interfaces; use Interfaces; procedure Test is function Get_NTP_Time ( Server : String; Timeout : Timeval_Duration := 10.0 ) return Time is NTP_Packet_Size : constant := 48; -- RFC 5905: Official NTP era begins at 1 Jan 1900. We cannot -- have it in Ada.Calendar.Time, so taking a later time. Note -- Time_Zone = 0 in order to have it UTC Era : constant Time := Time_Of (1999, 12, 31, Time_Zone => 0); -- RFC 5905: seconds since 1 Jan 1900 to 31 Dec 1999 Era_Offset : constant := 3_155_587_200; Socket : Socket_Type; Address : Sock_Addr_Type; Seconds : Unsigned_32; Fraction : Unsigned_32; Last : Stream_Element_Offset; Data : Stream_Element_Array (1..NTP_Packet_Size) := ( 1 => 2#1110_0011#, -- LI, Version, Mode 2 => 0, -- Stratum, or type of clock 3 => 0, -- Polling Interval 4 => 16#EC#, -- Peer Clock Precision 13 => 49, 14 => 16#4E#, 15 => 49, 16 => 52, others => 0 ); begin Address.Addr := Addresses (Get_Host_By_Name (Server), 1); Address.Port := 123; -- NTP port Create_Socket (Socket, Family_Inet, Socket_Datagram); Set_Socket_Option ( Socket, Socket_Level, (Receive_Timeout, Timeout) ); Send_Socket (Socket, Data, Last, Address); Receive_Socket (Socket, Data, Last, Address); if Last /= Data'Last then Raise_Exception (Data_Error'Identity, "Mangled response"); end if; Seconds := ( Unsigned_32 (Data (41)) * 2**24 + Unsigned_32 (Data (42)) * 2**16 + Unsigned_32 (Data (43)) * 2**8 + Unsigned_32 (Data (44)) - Era_OFfset ); Fraction := ( Unsigned_32 (Data (45)) * 2**24 + Unsigned_32 (Data (46)) * 2**16 + Unsigned_32 (Data (47)) * 2**8 + Unsigned_32 (Data (48)) ); return ( Era + Duration (Seconds) -- + Duration (Long_Float (Fraction) / 2.0**32) ); end Get_NTP_Time; Stamp : Time; begin Stamp := Get_NTP_Time ("time.nist.gov"); Put_Line ("NTP time " & Image (Stamp)); exception when Error : others => Put_Line ("Error: " & Exception_Information (Error)); end Test; ------------------------------------------------------------------- -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de