comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Ada.Calendar and NTP (and Unix Epoch)
Date: Tue, 24 Jul 2012 09:26:46 -0700 (PDT)
Date: 2012-07-24T09:26:46-07:00	[thread overview]
Message-ID: <5513b36d-560f-42ee-b6b6-bdb456097780@googlegroups.com> (raw)
In-Reply-To: <60c9c92b-280b-4178-a410-2bc8756c6b5e@googlegroups.com>

On Tuesday, July 24, 2012 12:24:17 AM UTC-7, erlo....@gmail.com wrote:

> NTP epoch 1 started 1-1-1900 0:00, just like the Unix epoch started 1-1-1970
> 0:00. So when I get a time from an NTP-server, I get seconds from the start of 
> the NTP epoch.

All right, then.  First of all, I don't know what Ada.Calendar.Conversions is.  It's not defined by the language, but rather by one particular implementor.  IMHO you should prefer to use the language-defined packages if at all feasible.  It will be more portable, plus a lot of work goes into making sure things in the Standard have precise definitions.  

If you have an integer value N representing the number of seconds since 1/1/1900, I think you want to convert this to an Ada.Calendar.Time that represents the same point in time, so that you can then compare it easily to another value of Ada.Calendar.Time.  If Ada had a Time that represented 1/1/1900, you could just take that Time and add N seconds to it.  But, as you found out, it didn't, so the first step is to convert N to a value N2 representing the number of seconds since 1/1/1901.  You can do this by setting N2 := N - constant (you'd better be able to figure out what the constant is!).  As I implied before, if N2 is negative then some very weird stuff is going on, so don't worry about that possibility.

Now, use Ada.Calendar.Time_Of to create a Time T_Base representing 1/1/1901. 
Or, better, if the NTP time is always UTC, then you should probably use Ada.Calendar.Formatting.Time_Of to create a time that represents midnight of 1/1/1901 in UTC time; I think you do this by passing Time_Zone => Ada.Calendar.Time_Zones.UTC_Time_Offset.  (Somebody please correct me if I'm supposed to negate the value.)

Now, you *may* be able to create a Time T representing your NTP time just by setting

   T := T_Base + Duration(N2);

This will work on some implementations.  On other implementations, though, Duration isn't guaranteed to have a range that covers 112 years or more (the language only requires that it covers one day).  You can get around this by breaking N2 into "days" and "seconds":

   N2_Days := N2 / 86_400;
   N2_Seconds := N2 mod 86_400;
   T := T_Base + Day_Count (N2_Days) + Duration (N2_Seconds);

The Day_Count type is defined in Ada.Calendar.Arithmetic.

Now T will be a Time value that represents the point in time that occurred N seconds after midnight on 1/1/1900 (UTC), and you can compare this to any other Time value.

                          -- Adam



  reply	other threads:[~2012-07-26 15:17 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-23 21:42 Ada.Calendar and NTP (and Unix Epoch) erlo
2012-07-23 22:07 ` Adam Beneschan
     [not found]   ` <5s8s08lv6dj1i4tkb99roq9roifsgr44vd@invalid.netcom.com>
2012-07-24  7:11     ` Dmitry A. Kazakov
2012-07-24  7:50       ` erlo.haugen
2012-07-24  8:14         ` Dmitry A. Kazakov
2012-07-24  8:34           ` erlo.haugen
2012-07-24  9:13             ` Dmitry A. Kazakov
2012-07-24 12:27               ` erlo.haugen
2012-07-24 13:02                 ` Dmitry A. Kazakov
2012-07-24 14:10                   ` erlo
2012-07-24 16:37                 ` Adam Beneschan
2012-07-24  7:24   ` erlo.haugen
2012-07-24 16:26     ` Adam Beneschan [this message]
2012-07-24 18:28       ` Dmitry A. Kazakov
2012-07-24 19:07         ` Adam Beneschan
2012-07-24 20:17           ` Dmitry A. Kazakov
2012-07-24 19:43         ` Vasiliy Molostov
2012-07-24 20:29           ` Dmitry A. Kazakov
2012-07-24 21:22             ` Vasiliy Molostov
2012-07-25  6:32               ` Dmitry A. Kazakov
2012-07-25  7:04                 ` Vasiliy Molostov
2012-07-25  7:33                   ` Dmitry A. Kazakov
2012-07-25  8:05                     ` Vasiliy Molostov
2012-07-25  8:30                       ` Dmitry A. Kazakov
2012-07-25  8:45                         ` Vasiliy Molostov
2012-07-25  9:30                           ` Dmitry A. Kazakov
2012-07-24 20:33     ` Simon Wright
2012-07-25 10:14       ` Simon Wright
2012-07-25 13:16         ` Dmitry A. Kazakov
2012-07-24  7:37   ` erlo.haugen
2012-07-24 11:34 ` Simon Wright
2012-07-24 11:59   ` Nasser M. Abbasi
2012-07-24 15:08     ` Simon Wright
2012-07-24 16:59       ` Georg Bauhaus
2012-07-24 19:25         ` Simon Wright
2012-07-24 22:07           ` Georg Bauhaus
2012-07-24 19:17     ` John B. Matthews
2012-07-25  2:23 ` sla29970
2012-07-25  6:40   ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox