111
|
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 -- --
|
131
|
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
|
111
|
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 package Ada.Calendar with
|
|
37 SPARK_Mode,
|
131
|
38 Abstract_State => (Clock_Time with Synchronous),
|
111
|
39 Initializes => Clock_Time
|
|
40 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 -- A Day_Duration value of 86_400.0 designates a new day
|
|
53
|
|
54 subtype Day_Duration is Duration range 0.0 .. 86_400.0;
|
|
55
|
|
56 function Clock return Time with
|
|
57 Volatile_Function,
|
|
58 Global => Clock_Time;
|
|
59 -- The returned time value is the number of nanoseconds since the start
|
|
60 -- of Ada time (1901-01-01 00:00:00.0 UTC). If leap seconds are enabled,
|
|
61 -- the result will contain all elapsed leap seconds since the start of
|
|
62 -- Ada time until now.
|
|
63
|
|
64 function Year (Date : Time) return Year_Number;
|
|
65 function Month (Date : Time) return Month_Number;
|
|
66 function Day (Date : Time) return Day_Number;
|
|
67 function Seconds (Date : Time) return Day_Duration;
|
|
68
|
|
69 procedure Split
|
|
70 (Date : Time;
|
|
71 Year : out Year_Number;
|
|
72 Month : out Month_Number;
|
|
73 Day : out Day_Number;
|
|
74 Seconds : out Day_Duration);
|
|
75 -- Break down a time value into its date components set in the current
|
|
76 -- time zone. If Split is called on a time value created using Ada 2005
|
|
77 -- Time_Of in some arbitrary time zone, the input value will always be
|
|
78 -- interpreted as relative to the local time zone.
|
|
79
|
|
80 function Time_Of
|
|
81 (Year : Year_Number;
|
|
82 Month : Month_Number;
|
|
83 Day : Day_Number;
|
|
84 Seconds : Day_Duration := 0.0) return Time;
|
|
85 -- GNAT Note: Normally when procedure Split is called on a Time value
|
|
86 -- result of a call to function Time_Of, the out parameters of procedure
|
|
87 -- Split are identical to the in parameters of function Time_Of. However,
|
|
88 -- when a non-existent time of day is specified, the values for Seconds
|
|
89 -- may or may not be different. This may happen when Daylight Saving Time
|
|
90 -- (DST) is in effect, on the day when switching to DST, if Seconds
|
|
91 -- specifies a time of day in the hour that does not exist. For example,
|
|
92 -- in New York:
|
|
93 --
|
|
94 -- Time_Of (Year => 1998, Month => 4, Day => 5, Seconds => 10740.0)
|
|
95 --
|
|
96 -- will return a Time value T. If Split is called on T, the resulting
|
|
97 -- Seconds may be 14340.0 (3:59:00) instead of 10740.0 (2:59:00 being
|
|
98 -- a time that not exist).
|
|
99
|
|
100 function "+" (Left : Time; Right : Duration) return Time;
|
|
101 function "+" (Left : Duration; Right : Time) return Time;
|
|
102 function "-" (Left : Time; Right : Duration) return Time;
|
|
103 function "-" (Left : Time; Right : Time) return Duration;
|
|
104 -- The first three functions will raise Time_Error if the resulting time
|
|
105 -- value is less than the start of Ada time in UTC or greater than the
|
|
106 -- end of Ada time in UTC. The last function will raise Time_Error if the
|
|
107 -- resulting difference cannot fit into a duration value.
|
|
108
|
|
109 function "<" (Left, Right : Time) return Boolean;
|
|
110 function "<=" (Left, Right : Time) return Boolean;
|
|
111 function ">" (Left, Right : Time) return Boolean;
|
|
112 function ">=" (Left, Right : Time) return Boolean;
|
|
113
|
|
114 Time_Error : exception;
|
|
115
|
|
116 private
|
|
117 -- Mark the private part as SPARK_Mode Off to avoid accounting for variable
|
|
118 -- Invalid_Time_Zone_Offset in abstract state.
|
|
119
|
|
120 pragma SPARK_Mode (Off);
|
|
121
|
|
122 pragma Inline (Clock);
|
|
123
|
|
124 pragma Inline (Year);
|
|
125 pragma Inline (Month);
|
|
126 pragma Inline (Day);
|
|
127
|
|
128 pragma Inline ("+");
|
|
129 pragma Inline ("-");
|
|
130
|
|
131 pragma Inline ("<");
|
|
132 pragma Inline ("<=");
|
|
133 pragma Inline (">");
|
|
134 pragma Inline (">=");
|
|
135
|
|
136 -- The units used in this version of Ada.Calendar are nanoseconds. The
|
|
137 -- following constants provide values used in conversions of seconds or
|
|
138 -- days to the underlying units.
|
|
139
|
|
140 Nano : constant := 1_000_000_000;
|
|
141 Nano_F : constant := 1_000_000_000.0;
|
|
142 Nanos_In_Day : constant := 86_400_000_000_000;
|
|
143 Secs_In_Day : constant := 86_400;
|
|
144
|
|
145 ----------------------------
|
|
146 -- Implementation of Time --
|
|
147 ----------------------------
|
|
148
|
|
149 -- Time is represented as a signed 64 bit integer count of nanoseconds
|
|
150 -- since the start of Ada time (1901-01-01 00:00:00.0 UTC). Time values
|
|
151 -- produced by Time_Of are internally normalized to UTC regardless of their
|
|
152 -- local time zone. This representation ensures correct handling of leap
|
|
153 -- seconds as well as performing arithmetic. In Ada 95, Split and Time_Of
|
|
154 -- will treat a time value as being in the local time zone, in Ada 2005,
|
|
155 -- Split and Time_Of will treat a time value as being in the designated
|
|
156 -- time zone by the formal parameter or in UTC by default. The size of the
|
|
157 -- type is large enough to cover the Ada 2005 range of time (1901-01-01
|
|
158 -- 00:00:00.0 UTC - 2399-12-31-23:59:59.999999999 UTC).
|
|
159
|
|
160 ------------------
|
|
161 -- Leap Seconds --
|
|
162 ------------------
|
|
163
|
|
164 -- Due to Earth's slowdown, the astronomical time is not as precise as the
|
|
165 -- International Atomic Time. To compensate for this inaccuracy, a single
|
|
166 -- leap second is added after the last day of June or December. The count
|
|
167 -- of seconds during those occurrences becomes:
|
|
168
|
|
169 -- ... 58, 59, leap second 60, 0, 1, 2 ...
|
|
170
|
|
171 -- Unlike leap days, leap seconds occur simultaneously around the world.
|
|
172 -- In other words, if a leap second occurs at 23:59:60 UTC, it also occurs
|
|
173 -- on 18:59:60 -5 the same day or 2:59:60 +2 on the next day.
|
|
174
|
|
175 -- Leap seconds do not follow a formula. The International Earth Rotation
|
|
176 -- and Reference System Service decides when to add one. Leap seconds are
|
|
177 -- included in the representation of time in Ada 95 mode. As a result,
|
|
178 -- the following two time values will differ by two seconds:
|
|
179
|
|
180 -- 1972-06-30 23:59:59.0
|
|
181 -- 1972-07-01 00:00:00.0
|
|
182
|
|
183 -- When a new leap second is introduced, the following steps must be
|
|
184 -- carried out:
|
|
185
|
|
186 -- 1) Increment Leap_Seconds_Count in a-calend.adb by one
|
|
187 -- 2) Increment LS_Count in xleaps.adb by one
|
|
188 -- 3) Add the new date to the aggregate of array LS_Dates in
|
|
189 -- xleaps.adb
|
|
190 -- 4) Compile and execute xleaps
|
|
191 -- 5) Replace the values of Leap_Second_Times in a-calend.adb with the
|
|
192 -- aggregate generated by xleaps
|
|
193
|
|
194 -- The algorithms that build the actual leap second values and discover
|
|
195 -- how many leap seconds have occurred between two dates do not need any
|
|
196 -- modification.
|
|
197
|
|
198 ------------------------------
|
|
199 -- Non-leap Centennial Years --
|
|
200 ------------------------------
|
|
201
|
|
202 -- Over the range of Ada time, centennial years 2100, 2200 and 2300 are
|
|
203 -- non-leap. As a consequence, seven non-leap years occur over the period
|
|
204 -- of year - 4 to year + 4. Internally, routines Split and Time_Of add or
|
|
205 -- subtract a "fake" February 29 to facilitate the arithmetic involved.
|
|
206
|
|
207 ------------------------
|
|
208 -- Local Declarations --
|
|
209 ------------------------
|
|
210
|
|
211 type Time_Rep is new Long_Long_Integer;
|
|
212 type Time is new Time_Rep;
|
|
213 -- The underlying type of Time has been chosen to be a 64 bit signed
|
|
214 -- integer number since it allows for easier processing of sub-seconds
|
|
215 -- and arithmetic. We use Long_Long_Integer to allow this unit to compile
|
|
216 -- when using custom target configuration files where the max integer is
|
|
217 -- 32 bits. This is useful for static analysis tools such as SPARK or
|
|
218 -- CodePeer.
|
|
219 --
|
|
220 -- Note: the reason we have two separate types here is to avoid problems
|
|
221 -- with overloading ambiguities in the body if we tried to use Time as an
|
|
222 -- internal computational type.
|
|
223
|
131
|
224 function Epoch_Offset return Time_Rep;
|
|
225 pragma Inline (Epoch_Offset);
|
|
226 -- Return the difference between 2150-1-1 UTC and 1970-1-1 UTC expressed in
|
|
227 -- nanoseconds. Note that year 2100 is non-leap.
|
|
228
|
111
|
229 Days_In_Month : constant array (Month_Number) of Day_Number :=
|
|
230 (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
|
|
231 -- Days in month for non-leap year, leap year case is adjusted in code
|
|
232
|
|
233 Invalid_Time_Zone_Offset : Long_Integer;
|
|
234 pragma Import (C, Invalid_Time_Zone_Offset, "__gnat_invalid_tzoff");
|
|
235
|
|
236 function Is_Leap (Year : Year_Number) return Boolean;
|
|
237 -- Determine whether a given year is leap
|
|
238
|
|
239 ----------------------------------------------------------
|
|
240 -- Target-Independent Interface to Children of Calendar --
|
|
241 ----------------------------------------------------------
|
|
242
|
|
243 -- The following packages provide a target-independent interface to the
|
|
244 -- children of Calendar - Arithmetic, Conversions, Delays, Formatting and
|
|
245 -- Time_Zones.
|
|
246
|
|
247 ---------------------------
|
|
248 -- Arithmetic_Operations --
|
|
249 ---------------------------
|
|
250
|
|
251 package Arithmetic_Operations is
|
|
252
|
|
253 function Add (Date : Time; Days : Long_Integer) return Time;
|
|
254 -- Add a certain number of days to a time value
|
|
255
|
|
256 procedure Difference
|
|
257 (Left : Time;
|
|
258 Right : Time;
|
|
259 Days : out Long_Integer;
|
|
260 Seconds : out Duration;
|
|
261 Leap_Seconds : out Integer);
|
|
262 -- Calculate the difference between two time values in terms of days,
|
|
263 -- seconds and leap seconds elapsed. The leap seconds are not included
|
|
264 -- in the seconds returned. If Left is greater than Right, the returned
|
|
265 -- values are positive, negative otherwise.
|
|
266
|
|
267 function Subtract (Date : Time; Days : Long_Integer) return Time;
|
|
268 -- Subtract a certain number of days from a time value
|
|
269
|
|
270 end Arithmetic_Operations;
|
|
271
|
|
272 ---------------------------
|
|
273 -- Conversion_Operations --
|
|
274 ---------------------------
|
|
275
|
|
276 package Conversion_Operations is
|
|
277
|
|
278 function To_Ada_Time (Unix_Time : Long_Integer) return Time;
|
|
279 -- Unix to Ada Epoch conversion
|
|
280
|
|
281 function To_Ada_Time
|
|
282 (tm_year : Integer;
|
|
283 tm_mon : Integer;
|
|
284 tm_day : Integer;
|
|
285 tm_hour : Integer;
|
|
286 tm_min : Integer;
|
|
287 tm_sec : Integer;
|
|
288 tm_isdst : Integer) return Time;
|
|
289 -- Struct tm to Ada Epoch conversion
|
|
290
|
|
291 function To_Duration
|
|
292 (tv_sec : Long_Integer;
|
|
293 tv_nsec : Long_Integer) return Duration;
|
|
294 -- Struct timespec to Duration conversion
|
|
295
|
|
296 procedure To_Struct_Timespec
|
|
297 (D : Duration;
|
|
298 tv_sec : out Long_Integer;
|
|
299 tv_nsec : out Long_Integer);
|
|
300 -- Duration to struct timespec conversion
|
|
301
|
|
302 procedure To_Struct_Tm
|
|
303 (T : Time;
|
|
304 tm_year : out Integer;
|
|
305 tm_mon : out Integer;
|
|
306 tm_day : out Integer;
|
|
307 tm_hour : out Integer;
|
|
308 tm_min : out Integer;
|
|
309 tm_sec : out Integer);
|
|
310 -- Time to struct tm conversion
|
|
311
|
|
312 function To_Unix_Time (Ada_Time : Time) return Long_Integer;
|
|
313 -- Ada to Unix Epoch conversion
|
|
314
|
|
315 end Conversion_Operations;
|
|
316
|
|
317 ----------------------
|
|
318 -- Delay_Operations --
|
|
319 ----------------------
|
|
320
|
|
321 package Delay_Operations is
|
|
322
|
|
323 function To_Duration (Date : Time) return Duration;
|
|
324 -- Given a time value in nanoseconds since 1901, convert it into a
|
|
325 -- duration value giving the number of nanoseconds since the Unix Epoch.
|
|
326
|
|
327 end Delay_Operations;
|
|
328
|
|
329 ---------------------------
|
|
330 -- Formatting_Operations --
|
|
331 ---------------------------
|
|
332
|
|
333 package Formatting_Operations is
|
|
334
|
|
335 function Day_Of_Week (Date : Time) return Integer;
|
|
336 -- Determine which day of week Date falls on. The returned values are
|
|
337 -- within the range of 0 .. 6 (Monday .. Sunday).
|
|
338
|
|
339 procedure Split
|
|
340 (Date : Time;
|
|
341 Year : out Year_Number;
|
|
342 Month : out Month_Number;
|
|
343 Day : out Day_Number;
|
|
344 Day_Secs : out Day_Duration;
|
|
345 Hour : out Integer;
|
|
346 Minute : out Integer;
|
|
347 Second : out Integer;
|
|
348 Sub_Sec : out Duration;
|
|
349 Leap_Sec : out Boolean;
|
|
350 Use_TZ : Boolean;
|
|
351 Is_Historic : Boolean;
|
|
352 Time_Zone : Long_Integer);
|
|
353 pragma Export (Ada, Split, "__gnat_split");
|
|
354 -- Split a time value into its components. If flag Is_Historic is set,
|
|
355 -- this routine would try to use to the best of the OS's abilities the
|
|
356 -- time zone offset that was or will be in effect on Date. Set Use_TZ
|
|
357 -- to use the local time zone (the value in Time_Zone is ignored) when
|
|
358 -- splitting a time value.
|
|
359
|
|
360 function Time_Of
|
|
361 (Year : Year_Number;
|
|
362 Month : Month_Number;
|
|
363 Day : Day_Number;
|
|
364 Day_Secs : Day_Duration;
|
|
365 Hour : Integer;
|
|
366 Minute : Integer;
|
|
367 Second : Integer;
|
|
368 Sub_Sec : Duration;
|
|
369 Leap_Sec : Boolean;
|
|
370 Use_Day_Secs : Boolean;
|
|
371 Use_TZ : Boolean;
|
|
372 Is_Historic : Boolean;
|
|
373 Time_Zone : Long_Integer) return Time;
|
|
374 pragma Export (Ada, Time_Of, "__gnat_time_of");
|
|
375 -- Given all the components of a date, return the corresponding time
|
|
376 -- value. Set Use_Day_Secs to use the value in Day_Secs, otherwise the
|
|
377 -- day duration will be calculated from Hour, Minute, Second and Sub_
|
|
378 -- Sec. If flag Is_Historic is set, this routine would try to use to the
|
|
379 -- best of the OS's abilities the time zone offset that was or will be
|
|
380 -- in effect on the input date. Set Use_TZ to use the local time zone
|
|
381 -- (the value in formal Time_Zone is ignored) when building a time value
|
|
382 -- and to verify the validity of a requested leap second.
|
|
383
|
|
384 end Formatting_Operations;
|
|
385
|
|
386 ---------------------------
|
|
387 -- Time_Zones_Operations --
|
|
388 ---------------------------
|
|
389
|
|
390 package Time_Zones_Operations is
|
|
391
|
|
392 function UTC_Time_Offset (Date : Time) return Long_Integer;
|
|
393 -- Return (in seconds) the difference between the local time zone and
|
|
394 -- UTC time at a specific historic date.
|
|
395
|
|
396 end Time_Zones_Operations;
|
|
397
|
|
398 end Ada.Calendar;
|