* NTP
@ 2017-09-16 6:40 Anatoly Chernyshev
2017-09-16 9:29 ` NTP Dmitry A. Kazakov
0 siblings, 1 reply; 6+ messages in thread
From: Anatoly Chernyshev @ 2017-09-16 6:40 UTC (permalink / raw)
Hello everyone,
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.
Thanks in advance.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: NTP
2017-09-16 6:40 NTP Anatoly Chernyshev
@ 2017-09-16 9:29 ` Dmitry A. Kazakov
2017-09-16 12:56 ` NTP Anatoly Chernyshev
2017-09-18 7:54 ` NTP Tarjei Jensen
0 siblings, 2 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2017-09-16 9:29 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: NTP
2017-09-16 9:29 ` NTP Dmitry A. Kazakov
@ 2017-09-16 12:56 ` Anatoly Chernyshev
2017-09-18 7:54 ` NTP Tarjei Jensen
1 sibling, 0 replies; 6+ messages in thread
From: Anatoly Chernyshev @ 2017-09-16 12:56 UTC (permalink / raw)
Thanks Dmitry,
That's been comprehensive, and works just the way I wanted.
A.
On Saturday, September 16, 2017 at 9:29:13 PM UTC+12, Dmitry A. Kazakov wrote:
> Do you mean send a single UDP request to an NTP server and convert the
> response to Ada time?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: NTP
2017-09-16 9:29 ` NTP Dmitry A. Kazakov
2017-09-16 12:56 ` NTP Anatoly Chernyshev
@ 2017-09-18 7:54 ` Tarjei Jensen
2017-09-18 8:21 ` NTP Dmitry A. Kazakov
1 sibling, 1 reply; 6+ messages in thread
From: Tarjei Jensen @ 2017-09-18 7:54 UTC (permalink / raw)
I'd just like to point out to the unwary that the time you get is GMT or UTC.
Which means that for most people, local time will be different by varying degrees.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: NTP
2017-09-18 7:54 ` NTP Tarjei Jensen
@ 2017-09-18 8:21 ` Dmitry A. Kazakov
[not found] ` <cb9fcb70-1fb6-4f22-aa25-10044b4cc9a7@googlegroups.com>
0 siblings, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2017-09-18 8:21 UTC (permalink / raw)
On 18/09/2017 09:54, Tarjei Jensen wrote:
> I'd just like to point out to the unwary that the time you get is GMT or UTC.
You mean that Ada.Calendar.Formatting.Time_Of with Time_Zone = 0 returns
GMT and not UTC? And specifically that
Ada.Calendar.Time_Of (...) /=
Ada.Calendar.Formatting.Time_Of (..., Time_Zone => UTC_Time_Offset)
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: NTP
[not found] ` <cb9fcb70-1fb6-4f22-aa25-10044b4cc9a7@googlegroups.com>
@ 2017-09-27 19:25 ` erlo
0 siblings, 0 replies; 6+ messages in thread
From: erlo @ 2017-09-27 19:25 UTC (permalink / raw)
On Tue, 19 Sep 2017 05:09:32 -0700, Tarjei Jensen wrote:
> GMT and UTC is the same. Just different name.
Not quite so - GMT is a time zone, UTC is a time standard. See https://
www.timeanddate.com/time/gmt-utc-time.html
Br
Erlo
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2017-09-27 19:25 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-16 6:40 NTP Anatoly Chernyshev
2017-09-16 9:29 ` NTP Dmitry A. Kazakov
2017-09-16 12:56 ` NTP Anatoly Chernyshev
2017-09-18 7:54 ` NTP Tarjei Jensen
2017-09-18 8:21 ` NTP Dmitry A. Kazakov
[not found] ` <cb9fcb70-1fb6-4f22-aa25-10044b4cc9a7@googlegroups.com>
2017-09-27 19:25 ` NTP erlo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox