1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT RUN-TIME COMPONENTS                         --
4--                                                                          --
5--                         A D A . C A L E N D A R                          --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1992-2012, Free Software Foundation, Inc.         --
10--                                                                          --
11-- This specification is derived from the Ada Reference Manual for use with --
12-- GNAT. The copyright notice above, and the license provisions that follow --
13-- apply solely to the  contents of the part following the private keyword. --
14--                                                                          --
15-- GNAT is free software;  you can  redistribute it  and/or modify it under --
16-- terms of the  GNU General Public License as published  by the Free Soft- --
17-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
18-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
19-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
20-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
21--                                                                          --
22-- As a special exception under Section 7 of GPL version 3, you are granted --
23-- additional permissions described in the GCC Runtime Library Exception,   --
24-- version 3.1, as published by the Free Software Foundation.               --
25--                                                                          --
26-- You should have received a copy of the GNU General Public License and    --
27-- a copy of the GCC Runtime Library Exception along with this program;     --
28-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
29-- <http://www.gnu.org/licenses/>.                                          --
30--                                                                          --
31-- GNAT was originally developed  by the GNAT team at  New York University. --
32-- Extensive contributions were provided by Ada Core Technologies Inc.      --
33--                                                                          --
34------------------------------------------------------------------------------
35
36--  This is the OpenVMS version
37
38with System.OS_Primitives;
39
40package Ada.Calendar is
41
42   type Time is private;
43
44   --  Declarations representing limits of allowed local time values. Note that
45   --  these do NOT constrain the possible stored values of time which may well
46   --  permit a larger range of times (this is explicitly allowed in Ada 95).
47
48   subtype Year_Number  is Integer range 1901 .. 2399;
49   subtype Month_Number is Integer range 1 .. 12;
50   subtype Day_Number   is Integer range 1 .. 31;
51
52   subtype Day_Duration is Duration range 0.0 .. 86_400.0;
53   --  Note that a value of 86_400.0 is the start of the next day
54
55   function Clock return Time;
56   --  The returned time value is the number of nanoseconds since the start
57   --  of Ada time (1901-01-01 00:00:00.0 UTC). If leap seconds are enabled,
58   --  the result will contain all elapsed leap seconds since the start of
59   --  Ada time until now.
60
61   function Year    (Date : Time) return Year_Number;
62   function Month   (Date : Time) return Month_Number;
63   function Day     (Date : Time) return Day_Number;
64   function Seconds (Date : Time) return Day_Duration;
65
66   procedure Split
67     (Date    : Time;
68      Year    : out Year_Number;
69      Month   : out Month_Number;
70      Day     : out Day_Number;
71      Seconds : out Day_Duration);
72   --  Break down a time value into its date components set in the current
73   --  time zone. If Split is called on a time value created using Ada 2005
74   --  Time_Of in some arbitrary time zone, the input value will always be
75   --  interpreted as relative to the local time zone.
76
77   function Time_Of
78     (Year    : Year_Number;
79      Month   : Month_Number;
80      Day     : Day_Number;
81      Seconds : Day_Duration := 0.0) return Time;
82   --  GNAT Note: Normally when procedure Split is called on a Time value
83   --  result of a call to function Time_Of, the out parameters of procedure
84   --  Split are identical to the in parameters of function Time_Of. However,
85   --  when a non-existent time of day is specified, the values for Seconds
86   --  may or may not be different. This may happen when Daylight Saving Time
87   --  (DST) is in effect, on the day when switching to DST, if Seconds
88   --  specifies a time of day in the hour that does not exist. For example,
89   --  in New York:
90   --
91   --    Time_Of (Year => 1998, Month => 4, Day => 5, Seconds => 10740.0)
92   --
93   --  will return a Time value T. If Split is called on T, the resulting
94   --  Seconds may be 14340.0 (3:59:00) instead of 10740.0 (2:59:00 being
95   --  a time that not exist).
96
97   function "+" (Left : Time;     Right : Duration) return Time;
98   function "+" (Left : Duration; Right : Time)     return Time;
99   function "-" (Left : Time;     Right : Duration) return Time;
100   function "-" (Left : Time;     Right : Time)     return Duration;
101   --  The first three functions will raise Time_Error if the resulting time
102   --  value is less than the start of Ada time in UTC or greater than the
103   --  end of Ada time in UTC. The last function will raise Time_Error if the
104   --  resulting difference cannot fit into a duration value.
105
106   function "<"  (Left, Right : Time) return Boolean;
107   function "<=" (Left, Right : Time) return Boolean;
108   function ">"  (Left, Right : Time) return Boolean;
109   function ">=" (Left, Right : Time) return Boolean;
110
111   Time_Error : exception;
112
113private
114   pragma Inline (Clock);
115
116   pragma Inline (Year);
117   pragma Inline (Month);
118   pragma Inline (Day);
119
120   pragma Inline ("+");
121   pragma Inline ("-");
122
123   pragma Inline ("<");
124   pragma Inline ("<=");
125   pragma Inline (">");
126   pragma Inline (">=");
127
128   --  Although the units are 100 nanoseconds, for the purpose of better
129   --  readability, this unit will be called "mili".
130
131   Mili         : constant := 10_000_000;
132   Mili_F       : constant := 10_000_000.0;
133   Milis_In_Day : constant := 864_000_000_000;
134   Secs_In_Day  : constant := 86_400;
135
136   --  Time is represented as the number of 100-nanosecond (ns) units from the
137   --  system base date and time 1858-11-17 0.0 (the Smithsonian base date and
138   --  time for the astronomic calendar).
139
140   --  The time value stored is typically a UTC value, as provided in standard
141   --  Unix environments. If this is the case then Split and Time_Of perform
142   --  required conversions to and from local times.
143
144   --  Notwithstanding this definition, Time is not quite the same as OS_Time.
145   --  Relative Time is positive, whereas relative OS_Time is negative,
146   --  but this declaration makes for easier conversion.
147
148   type Time is new System.OS_Primitives.OS_Time;
149
150   Days_In_Month : constant array (Month_Number) of Day_Number :=
151                     (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
152   --  Days in month for non-leap year, leap year case is adjusted in code
153
154   Invalid_Time_Zone_Offset : Long_Integer;
155   pragma Import (C, Invalid_Time_Zone_Offset, "__gnat_invalid_tzoff");
156
157   function Is_Leap (Year : Year_Number) return Boolean;
158   --  Determine whether a given year is leap
159
160   ----------------------------------------------------------
161   -- Target-Independent Interface to Children of Calendar --
162   ----------------------------------------------------------
163
164   --  The following packages provide a target-independent interface to the
165   --  children of Calendar - Arithmetic, Conversions, Delays, Formatting and
166   --  Time_Zones.
167
168   --  NOTE: Delays does not need a target independent interface because
169   --  VMS already has a target specific file for that package.
170
171   ---------------------------
172   -- Arithmetic_Operations --
173   ---------------------------
174
175   package Arithmetic_Operations is
176
177      function Add (Date : Time; Days : Long_Integer) return Time;
178      --  Add a certain number of days to a time value
179
180      procedure Difference
181        (Left         : Time;
182         Right        : Time;
183         Days         : out Long_Integer;
184         Seconds      : out Duration;
185         Leap_Seconds : out Integer);
186      --  Calculate the difference between two time values in terms of days,
187      --  seconds and leap seconds elapsed. The leap seconds are not included
188      --  in the seconds returned. If Left is greater than Right, the returned
189      --  values are positive, negative otherwise.
190
191      function Subtract (Date : Time; Days : Long_Integer) return Time;
192      --  Subtract a certain number of days from a time value
193
194   end Arithmetic_Operations;
195
196   ---------------------------
197   -- Conversion_Operations --
198   ---------------------------
199
200   package Conversion_Operations is
201
202      function To_Ada_Time (Unix_Time : Long_Integer) return Time;
203      --  Unix to Ada Epoch conversion
204
205      function To_Ada_Time
206        (tm_year  : Integer;
207         tm_mon   : Integer;
208         tm_day   : Integer;
209         tm_hour  : Integer;
210         tm_min   : Integer;
211         tm_sec   : Integer;
212         tm_isdst : Integer) return Time;
213      --  Struct tm to Ada Epoch conversion
214
215      function To_Duration
216        (tv_sec  : Long_Integer;
217         tv_nsec : Long_Integer) return Duration;
218      --  Struct timespec to Duration conversion
219
220      procedure To_Struct_Timespec
221        (D       : Duration;
222         tv_sec  : out Long_Integer;
223         tv_nsec : out Long_Integer);
224      --  Duration to struct timespec conversion
225
226      procedure To_Struct_Tm
227        (T       : Time;
228         tm_year : out Integer;
229         tm_mon  : out Integer;
230         tm_day  : out Integer;
231         tm_hour : out Integer;
232         tm_min  : out Integer;
233         tm_sec  : out Integer);
234      --  Time to struct tm conversion
235
236      function To_Unix_Time (Ada_Time : Time) return Long_Integer;
237      --  Ada to Unix Epoch conversion
238
239   end Conversion_Operations;
240
241   ---------------------------
242   -- Formatting_Operations --
243   ---------------------------
244
245   package Formatting_Operations is
246
247      function Day_Of_Week (Date : Time) return Integer;
248      --  Determine which day of week Date falls on. The returned values are
249      --  within the range of 0 .. 6 (Monday .. Sunday).
250
251      procedure Split
252        (Date        : Time;
253         Year        : out Year_Number;
254         Month       : out Month_Number;
255         Day         : out Day_Number;
256         Day_Secs    : out Day_Duration;
257         Hour        : out Integer;
258         Minute      : out Integer;
259         Second      : out Integer;
260         Sub_Sec     : out Duration;
261         Leap_Sec    : out Boolean;
262         Use_TZ      : Boolean;
263         Is_Historic : Boolean;
264         Time_Zone   : Long_Integer);
265      pragma Export (Ada, Split, "__gnat_split");
266      --  Split a time value into its components. If flag Is_Historic is set,
267      --  this routine would try to use to the best of the OS's abilities the
268      --  time zone offset that was or will be in effect on Date. Set Use_TZ
269      --  to use the local time zone (the value in Time_Zone is ignored) when
270      --  splitting a time value.
271
272      function Time_Of
273        (Year         : Year_Number;
274         Month        : Month_Number;
275         Day          : Day_Number;
276         Day_Secs     : Day_Duration;
277         Hour         : Integer;
278         Minute       : Integer;
279         Second       : Integer;
280         Sub_Sec      : Duration;
281         Leap_Sec     : Boolean;
282         Use_Day_Secs : Boolean;
283         Use_TZ       : Boolean;
284         Is_Historic  : Boolean;
285         Time_Zone    : Long_Integer) return Time;
286      pragma Export (Ada, Time_Of, "__gnat_time_of");
287      --  Given all the components of a date, return the corresponding time
288      --  value. Set Use_Day_Secs to use the value in Day_Secs, otherwise the
289      --  day duration will be calculated from Hour, Minute, Second and Sub_
290      --  Sec. If flag Is_Historic is set, this routine would try to use to the
291      --  best of the OS's abilities the time zone offset that was or will be
292      --  in effect on the input date. Set Use_TZ to use the local time zone
293      --  (the value in formal Time_Zone is ignored) when building a time value
294      --  and to verify the validity of a requested leap second.
295
296   end Formatting_Operations;
297
298   ---------------------------
299   -- Time_Zones_Operations --
300   ---------------------------
301
302   package Time_Zones_Operations is
303
304      function UTC_Time_Offset (Date : Time) return Long_Integer;
305      --  Return (in seconds) the difference between the local time zone and
306      --  UTC time at a specific historic date.
307
308   end Time_Zones_Operations;
309
310end Ada.Calendar;
311