Debian Codegraph @

gcc/ada/libgnat/a-calend.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . C A L E N D A R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Ada.Unchecked_Conversion;
33
34 with Interfaces.C;
35
36 with System.OS_Primitives;
37
38 with System.OS_Lib;
39
40 package body Ada.Calendar with
41 SPARK_Mode => Off
42 is
43 --------------------------
44 -- Implementation Notes --
45 --------------------------
46
47 -- In complex algorithms, some variables of type Ada.Calendar.Time carry
48 -- suffix _S or _N to denote units of seconds or nanoseconds.
49 --
50 -- Because time is measured in different units and from different origins
51 -- on various targets, a system independent model is incorporated into
52 -- Ada.Calendar. The idea behind the design is to encapsulate all target
53 -- dependent machinery in a single package, thus providing a uniform
54 -- interface to all existing and any potential children.
55
56 -- package Ada.Calendar
57 -- procedure Split (5 parameters) -------+
58 -- | Call from local routine
59 -- private |
60 -- package Formatting_Operations |
61 -- procedure Split (11 parameters) <--+
62 -- end Formatting_Operations |
63 -- end Ada.Calendar |
64 -- |
65 -- package Ada.Calendar.Formatting | Call from child routine
66 -- procedure Split (9 or 10 parameters) -+
67 -- end Ada.Calendar.Formatting
68
69 -- The behavior of the interfacing routines is controlled via various
70 -- flags. All new Ada 2005 types from children of Ada.Calendar are
71 -- emulated by a similar type. For instance, type Day_Number is replaced
72 -- by Integer in various routines. One ramification of this model is that
73 -- the caller site must perform validity checks on returned results.
74 -- The end result of this model is the lack of target specific files per
75 -- child of Ada.Calendar (e.g. a-calfor).
76
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
80
81 procedure Check_Within_Time_Bounds (T : Time_Rep);
82 -- Ensure that a time representation value falls withing the bounds of Ada
83 -- time. Leap seconds support is taken into account.
84
85 procedure Cumulative_Leap_Seconds
86 (Start_Date : Time_Rep;
87 End_Date : Time_Rep;
88 Elapsed_Leaps : out Natural;
89 Next_Leap : out Time_Rep);
90 -- Elapsed_Leaps is the sum of the leap seconds that have occurred on or
91 -- after Start_Date and before (strictly before) End_Date. Next_Leap_Sec
92 -- represents the next leap second occurrence on or after End_Date. If
93 -- there are no leaps seconds after End_Date, End_Of_Time is returned.
94 -- End_Of_Time can be used as End_Date to count all the leap seconds that
95 -- have occurred on or after Start_Date.
96 --
97 -- Note: Any sub seconds of Start_Date and End_Date are discarded before
98 -- the calculations are done. For instance: if 113 seconds is a leap
99 -- second (it isn't) and 113.5 is input as an End_Date, the leap second
100 -- at 113 will not be counted in Leaps_Between, but it will be returned
101 -- as Next_Leap_Sec. Thus, if the caller wants to know if the End_Date is
102 -- a leap second, the comparison should be:
103 --
104 -- End_Date >= Next_Leap_Sec;
105 --
106 -- After_Last_Leap is designed so that this comparison works without
107 -- having to first check if Next_Leap_Sec is a valid leap second.
108
109 function Duration_To_Time_Rep is
110 new Ada.Unchecked_Conversion (Duration, Time_Rep);
111 -- Convert a duration value into a time representation value
112
113 function Elapsed_Leaps (Start_Time, End_Time : Time_Rep) return Natural
114 with Pre => (End_Time >= Start_Time);
115 -- If the target supports leap seconds, determine the number of leap
116 -- seconds elapsed between start_time and end_time.
117 --
118 -- NB! This function assumes that End_Time is not smaller than
119 -- Start_Time. There are usages of the function that correct the time
120 -- by passed leap seconds and use the results for another seach.
121 -- If negative leap seconds are introduced eventually, then such
122 -- calls should be revised as the correction can go to either direction.
123
124 function Time_Rep_To_Duration is
125 new Ada.Unchecked_Conversion (Time_Rep, Duration);
126 -- Convert a time representation value into a duration value
127
128 function UTC_Time_Offset
129 (Date : Time;
130 Is_Historic : Boolean) return Long_Integer;
131 -- This routine acts as an Ada wrapper around __gnat_localtime_tzoff which
132 -- in turn utilizes various OS-dependent mechanisms to calculate the time
133 -- zone offset of a date. Formal parameter Date represents an arbitrary
134 -- time stamp, either in the past, now, or in the future. If the flag
135 -- Is_Historic is set, this routine would try to calculate to the best of
136 -- the OS's abilities the time zone offset that was or will be in effect
137 -- on Date. If the flag is set to False, the routine returns the current
138 -- time zone with Date effectively set to Clock.
139 --
140 -- NOTE: Targets which support localtime_r will aways return a historic
141 -- time zone even if flag Is_Historic is set to False because this is how
142 -- localtime_r operates.
143
144 -----------------
145 -- Local Types --
146 -----------------
147
148 -- An integer time duration. The type is used whenever a positive elapsed
149 -- duration is needed, for instance when splitting a time value. Here is
150 -- how Time_Rep and Time_Dur are related:
151
152 -- 'First Ada_Low Ada_High 'Last
153 -- Time_Rep: +-------+------------------------+---------+
154 -- Time_Dur: +------------------------+---------+
155 -- 0 'Last
156
157 type Time_Dur is range 0 .. 2 ** 63 - 1;
158
159 --------------------------
160 -- Leap seconds control --
161 --------------------------
162
163 Flag : constant Integer;
164 pragma Import (C, Flag, "__gl_leap_seconds_support");
165 -- This imported value is used to determine whether the compilation had
166 -- binder flag "-y" present which enables leap seconds. A value of zero
167 -- signifies no leap seconds support while a value of one enables support.
168
169 Leap_Support : constant Boolean := (Flag = 1);
170 -- Flag to controls the usage of leap seconds in all Ada.Calendar routines
171
172 Leap_Seconds_Count : constant Natural := 27;
173
174 ---------------------
175 -- Local Constants --
176 ---------------------
177
178 Ada_Min_Year : constant Year_Number := Year_Number'First;
179 Secs_In_Four_Years : constant := (3 * 365 + 366) * Secs_In_Day;
180 Secs_In_Non_Leap_Year : constant := 365 * Secs_In_Day;
181 Nanos_In_Four_Years : constant := Secs_In_Four_Years * Nano;
182
183 -- Lower and upper bound of Ada time. Note that the lower and upper bound
184 -- account for the non-leap centennial years. See "Implementation of Time"
185 -- in the spec for what the zero value represents.
186
187 Ada_Low : constant Time_Rep := -(61 * 366 + 188 * 365) * Nanos_In_Day;
188 Ada_High : constant Time_Rep := (60 * 366 + 190 * 365) * Nanos_In_Day;
189
190 -- Even though the upper bound of time is 2399-12-31 23:59:59.999999999
191 -- UTC, it must be increased to include all leap seconds.
192
193 Ada_High_And_Leaps : constant Time_Rep :=
194 Ada_High + Time_Rep (Leap_Seconds_Count) * Nano;
195
196 -- Two constants used in the calculations of elapsed leap seconds.
197 -- End_Of_Time is later than Ada_High in time zone -28. Start_Of_Time
198 -- is earlier than Ada_Low in time zone +28.
199
200 End_Of_Time : constant Time_Rep :=
201 Ada_High + Time_Rep (3) * Nanos_In_Day;
202 Start_Of_Time : constant Time_Rep :=
203 Ada_Low - Time_Rep (3) * Nanos_In_Day;
204
205 -- The Unix lower time bound expressed as nanoseconds since the start of
206 -- Ada time in UTC.
207
208 Unix_Min : constant Time_Rep :=
209 Ada_Low + Time_Rep (17 * 366 + 52 * 365) * Nanos_In_Day;
210
211 -- The Unix upper time bound expressed as nanoseconds since the start of
212 -- Ada time in UTC.
213
214 Unix_Max : constant Time_Rep :=
215 Ada_Low + Time_Rep (34 * 366 + 102 * 365) * Nanos_In_Day +
216 Time_Rep (Leap_Seconds_Count) * Nano;
217
218 Cumulative_Days_Before_Month :
219 constant array (Month_Number) of Natural :=
220 [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
221
222 -- The following table contains the hard time values of all existing leap
223 -- seconds. The values are produced by the utility program xleaps.adb. This
224 -- must be updated when additional leap second times are defined.
225
226 Leap_Second_Times : constant array (1 .. Leap_Seconds_Count) of Time_Rep :=
227 [-5601484800000000000,
228 -5585587199000000000,
229 -5554051198000000000,
230 -5522515197000000000,
231 -5490979196000000000,
232 -5459356795000000000,
233 -5427820794000000000,
234 -5396284793000000000,
235 -5364748792000000000,
236 -5317487991000000000,
237 -5285951990000000000,
238 -5254415989000000000,
239 -5191257588000000000,
240 -5112287987000000000,
241 -5049129586000000000,
242 -5017593585000000000,
243 -4970332784000000000,
244 -4938796783000000000,
245 -4907260782000000000,
246 -4859827181000000000,
247 -4812566380000000000,
248 -4765132779000000000,
249 -4544207978000000000,
250 -4449513577000000000,
251 -4339180776000000000,
252 -4244572775000000000,
253 -4197052774000000000];
254
255 ---------
256 -- "+" --
257 ---------
258
259 function "+" (Left : Time; Right : Duration) return Time is
260 pragma Unsuppress (Overflow_Check);
261 Left_N : constant Time_Rep := Time_Rep (Left);
262 begin
263 return Time (Left_N + Duration_To_Time_Rep (Right));
264 exception
265 when Constraint_Error =>
266 raise Time_Error;
267 end "+";
268
269 function "+" (Left : Duration; Right : Time) return Time is
270 begin
271 return Right + Left;
272 end "+";
273
274 ---------
275 -- "-" --
276 ---------
277
278 function "-" (Left : Time; Right : Duration) return Time is
279 pragma Unsuppress (Overflow_Check);
280 Left_N : constant Time_Rep := Time_Rep (Left);
281 begin
282 return Time (Left_N - Duration_To_Time_Rep (Right));
283 exception
284 when Constraint_Error =>
285 raise Time_Error;
286 end "-";
287
288 function "-" (Left : Time; Right : Time) return Duration is
289 pragma Unsuppress (Overflow_Check);
290
291 Dur_Low : constant Time_Rep := Duration_To_Time_Rep (Duration'First);
292 Dur_High : constant Time_Rep := Duration_To_Time_Rep (Duration'Last);
293 -- The bounds of type Duration expressed as time representations
294
295 Res_N : Time_Rep;
296
297 begin
298 Res_N := Time_Rep (Left) - Time_Rep (Right);
299
300 -- Due to the extended range of Ada time, "-" is capable of producing
301 -- results which may exceed the range of Duration. In order to prevent
302 -- the generation of bogus values by the Unchecked_Conversion, we apply
303 -- the following check.
304
305 if Res_N < Dur_Low or else Res_N > Dur_High then
306 raise Time_Error;
307 end if;
308
309 return Time_Rep_To_Duration (Res_N);
310
311 exception
312 when Constraint_Error =>
313 raise Time_Error;
314 end "-";
315
316 ---------
317 -- "<" --
318 ---------
319
320 function "<" (Left, Right : Time) return Boolean is
321 begin
322 return Time_Rep (Left) < Time_Rep (Right);
323 end "<";
324
325 ----------
326 -- "<=" --
327 ----------
328
329 function "<=" (Left, Right : Time) return Boolean is
330 begin
331 return Time_Rep (Left) <= Time_Rep (Right);
332 end "<=";
333
334 ---------
335 -- ">" --
336 ---------
337
338 function ">" (Left, Right : Time) return Boolean is
339 begin
340 return Time_Rep (Left) > Time_Rep (Right);
341 end ">";
342
343 ----------
344 -- ">=" --
345 ----------
346
347 function ">=" (Left, Right : Time) return Boolean is
348 begin
349 return Time_Rep (Left) >= Time_Rep (Right);
350 end ">=";
351
352 ------------------------------
353 -- Check_Within_Time_Bounds --
354 ------------------------------
355
356 procedure Check_Within_Time_Bounds (T : Time_Rep) is
357 begin
358 if Leap_Support then
359 if T < Ada_Low or else T > Ada_High_And_Leaps then
360 raise Time_Error;
361 end if;
362 else
363 if T < Ada_Low or else T > Ada_High then
364 raise Time_Error;
365 end if;
366 end if;
367 end Check_Within_Time_Bounds;
368
369 -------------------
370 -- Elapsed_Leaps --
371 -------------------
372
373 function Elapsed_Leaps (Start_Time, End_Time : Time_Rep) return Natural
374 is
375 Elapsed : Natural := 0;
376 Next_Leap_N : Time_Rep;
377 begin
378 if Leap_Support then
379 Cumulative_Leap_Seconds
380 (Start_Time, End_Time, Elapsed, Next_Leap_N);
381
382 -- The system clock may fall exactly on a leap second
383
384 if End_Time >= Next_Leap_N then
385 Elapsed := Elapsed + 1;
386 end if;
387 end if;
388
389 return Elapsed;
390 end Elapsed_Leaps;
391
392 -----------
393 -- Clock --
394 -----------
395
396 function Clock return Time is
397
398 -- The system clock returns the time in UTC since the Unix Epoch of
399 -- 1970-01-01 00:00:00.0. We perform an origin shift to the Ada Epoch
400 -- by adding the number of nanoseconds between the two origins.
401
402 Res_N : Time_Rep :=
403 Duration_To_Time_Rep (System.OS_Primitives.Clock) + Unix_Min;
404
405 begin
406 Res_N := Res_N + Time_Rep (Elapsed_Leaps (Start_Of_Time, Res_N)) * Nano;
407
408 return Time (Res_N);
409 end Clock;
410
411 -----------------------------
412 -- Cumulative_Leap_Seconds --
413 -----------------------------
414
415 procedure Cumulative_Leap_Seconds
416 (Start_Date : Time_Rep;
417 End_Date : Time_Rep;
418 Elapsed_Leaps : out Natural;
419 Next_Leap : out Time_Rep)
420 is
421 End_Index : Positive;
422 End_T : Time_Rep := End_Date;
423 Start_Index : Positive;
424 Start_T : Time_Rep := Start_Date;
425
426 begin
427 -- Both input dates must be normalized to UTC
428
429 pragma Assert (Leap_Support and then End_Date >= Start_Date);
430
431 Next_Leap := End_Of_Time;
432
433 -- Make sure that the end date does not exceed the upper bound
434 -- of Ada time.
435
436 if End_Date > Ada_High then
437 End_T := Ada_High;
438 end if;
439
440 -- Remove the sub seconds from both dates
441
442 Start_T := Start_T - (Start_T mod Nano);
443 End_T := End_T - (End_T mod Nano);
444
445 -- Some trivial cases:
446 -- Leap 1 . . . Leap N
447 -- ---+========+------+############+-------+========+-----
448 -- Start_T End_T Start_T End_T
449
450 if End_T < Leap_Second_Times (1) then
451 Elapsed_Leaps := 0;
452 Next_Leap := Leap_Second_Times (1);
453
454 elsif Start_T > Leap_Second_Times (Leap_Seconds_Count) then
455 Elapsed_Leaps := 0;
456 Next_Leap := End_Of_Time;
457
458 else
459 -- Perform the calculations only if the start date is within the leap
460 -- second occurrences table.
461
462 -- 1 2 N - 1 N
463 -- +----+----+-- . . . --+-------+---+
464 -- | T1 | T2 | | N - 1 | N |
465 -- +----+----+-- . . . --+-------+---+
466 -- ^ ^
467 -- | Start_Index | End_Index
468 -- +-------------------+
469 -- Leaps_Between
470
471 -- The idea behind the algorithm is to iterate and find two
472 -- closest dates which are after Start_T and End_T. Their
473 -- corresponding index difference denotes the number of leap
474 -- seconds elapsed.
475
476 Start_Index := 1;
477 loop
478 exit when Leap_Second_Times (Start_Index) >= Start_T;
479 Start_Index := Start_Index + 1;
480 end loop;
481
482 End_Index := Start_Index;
483 loop
484 exit when End_Index > Leap_Seconds_Count
485 or else Leap_Second_Times (End_Index) >= End_T;
486 End_Index := End_Index + 1;
487 end loop;
488
489 if End_Index <= Leap_Seconds_Count then
490 Next_Leap := Leap_Second_Times (End_Index);
491 end if;
492
493 Elapsed_Leaps := End_Index - Start_Index;
494 end if;
495 end Cumulative_Leap_Seconds;
496
497 ---------
498 -- Day --
499 ---------
500
501 function Day (Date : Time) return Day_Number is
502 D : Day_Number;
503 Y : Year_Number;
504 M : Month_Number;
505 S : Day_Duration;
506 begin
507 Split (Date, Y, M, D, S);
508 return D;
509 end Day;
510
511 ------------------
512 -- Epoch_Offset --
513 ------------------
514
515 function Epoch_Offset return Time_Rep is
516 begin
517 return (136 * 365 + 44 * 366) * Nanos_In_Day;
518 end Epoch_Offset;
519
520 -------------
521 -- Is_Leap --
522 -------------
523
524 function Is_Leap (Year : Year_Number) return Boolean is
525 begin
526 -- Leap centennial years
527
528 if Year mod 400 = 0 then
529 return True;
530
531 -- Non-leap centennial years
532
533 elsif Year mod 100 = 0 then
534 return False;
535
536 -- Regular years
537
538 else
539 return Year mod 4 = 0;
540 end if;
541 end Is_Leap;
542
543 -----------
544 -- Month --
545 -----------
546
547 function Month (Date : Time) return Month_Number is
548 Y : Year_Number;
549 M : Month_Number;
550 D : Day_Number;
551 S : Day_Duration;
552 begin
553 Split (Date, Y, M, D, S);
554 return M;
555 end Month;
556
557 -------------
558 -- Seconds --
559 -------------
560
561 function Seconds (Date : Time) return Day_Duration is
562 Y : Year_Number;
563 M : Month_Number;
564 D : Day_Number;
565 S : Day_Duration;
566 begin
567 Split (Date, Y, M, D, S);
568 return S;
569 end Seconds;
570
571 -----------
572 -- Split --
573 -----------
574
575 procedure Split
576 (Date : Time;
577 Year : out Year_Number;
578 Month : out Month_Number;
579 Day : out Day_Number;
580 Seconds : out Day_Duration)
581 is
582 H : Integer;
583 M : Integer;
584 Se : Integer;
585 Ss : Duration;
586 Le : Boolean;
587
588 begin
589 -- Even though the input time zone is UTC (0), the flag Use_TZ will
590 -- ensure that Split picks up the local time zone.
591
592 Formatting_Operations.Split
593 (Date => Date,
594 Year => Year,
595 Month => Month,
596 Day => Day,
597 Day_Secs => Seconds,
598 Hour => H,
599 Minute => M,
600 Second => Se,
601 Sub_Sec => Ss,
602 Leap_Sec => Le,
603 Use_TZ => False,
604 Is_Historic => True,
605 Time_Zone => 0);
606
607 -- Validity checks
608
609 if not Year'Valid or else
610 not Month'Valid or else
611 not Day'Valid or else
612 not Seconds'Valid
613 then
614 raise Time_Error;
615 end if;
616 end Split;
617
618 -------------
619 -- Time_Of --
620 -------------
621
622 function Time_Of
623 (Year : Year_Number;
624 Month : Month_Number;
625 Day : Day_Number;
626 Seconds : Day_Duration := 0.0) return Time
627 is
628 -- The values in the following constants are irrelevant, they are just
629 -- placeholders; the choice of constructing a Day_Duration value is
630 -- controlled by the Use_Day_Secs flag.
631
632 H : constant Integer := 1;
633 M : constant Integer := 1;
634 Se : constant Integer := 1;
635 Ss : constant Duration := 0.1;
636
637 begin
638 -- Validity checks
639
640 if not Year'Valid or else
641 not Month'Valid or else
642 not Day'Valid or else
643 not Seconds'Valid
644 then
645 raise Time_Error;
646 end if;
647
648 -- Even though the input time zone is UTC (0), the flag Use_TZ will
649 -- ensure that Split picks up the local time zone.
650
651 return
652 Formatting_Operations.Time_Of
653 (Year => Year,
654 Month => Month,
655 Day => Day,
656 Day_Secs => Seconds,
657 Hour => H,
658 Minute => M,
659 Second => Se,
660 Sub_Sec => Ss,
661 Leap_Sec => False,
662 Use_Day_Secs => True,
663 Use_TZ => False,
664 Is_Historic => True,
665 Time_Zone => 0);
666 end Time_Of;
667
668 ---------------------
669 -- UTC_Time_Offset --
670 ---------------------
671
672 function UTC_Time_Offset
673 (Date : Time;
674 Is_Historic : Boolean) return Long_Integer
675 is
676 -- The following constants denote February 28 during non-leap centennial
677 -- years, the units are nanoseconds.
678
679 T_2100_2_28 : constant Time_Rep := Ada_Low +
680 (Time_Rep (49 * 366 + 150 * 365 + 59) * Secs_In_Day +
681 Time_Rep (Leap_Seconds_Count)) * Nano;
682
683 T_2200_2_28 : constant Time_Rep := Ada_Low +
684 (Time_Rep (73 * 366 + 226 * 365 + 59) * Secs_In_Day +
685 Time_Rep (Leap_Seconds_Count)) * Nano;
686
687 T_2300_2_28 : constant Time_Rep := Ada_Low +
688 (Time_Rep (97 * 366 + 302 * 365 + 59) * Secs_In_Day +
689 Time_Rep (Leap_Seconds_Count)) * Nano;
690
691 -- 56 years (14 leap years + 42 non-leap years) in nanoseconds:
692
693 Nanos_In_56_Years : constant := (14 * 366 + 42 * 365) * Nanos_In_Day;
694
695 type int_Pointer is access all Interfaces.C.int;
696 type long_Pointer is access all Interfaces.C.long;
697
698 type OS_Time_Pointer is access all System.OS_Lib.OS_Time;
699
700 procedure localtime_tzoff
701 (timer : OS_Time_Pointer;
702 is_historic : int_Pointer;
703 off : long_Pointer);
704 pragma Import (C, localtime_tzoff, "__gnat_localtime_tzoff");
705 -- This routine is a interfacing wrapper around the library function
706 -- __gnat_localtime_tzoff. Parameter 'timer' represents a Unix-based
707 -- time equivalent of the input date. If flag 'is_historic' is set, this
708 -- routine would try to calculate to the best of the OS's abilities the
709 -- time zone offset that was or will be in effect on 'timer'. If the
710 -- flag is set to False, the routine returns the current time zone
711 -- regardless of what 'timer' designates. Parameter 'off' captures the
712 -- UTC offset of 'timer'.
713
714 Adj_Cent : Integer;
715 Date_N : Time_Rep;
716 Flag : aliased Interfaces.C.int;
717 Offset : aliased Interfaces.C.long;
718 Secs_T : aliased System.OS_Lib.OS_Time;
719
720 -- Start of processing for UTC_Time_Offset
721
722 begin
723 Date_N := Time_Rep (Date);
724
725 -- Dates which are 56 years apart fall on the same day, day light saving
726 -- and so on. Non-leap centennial years violate this rule by one day and
727 -- as a consequence, special adjustment is needed.
728
729 Adj_Cent :=
730 (if Date_N <= T_2100_2_28 then 0
731 elsif Date_N <= T_2200_2_28 then 1
732 elsif Date_N <= T_2300_2_28 then 2
733 else 3);
734
735 if Adj_Cent > 0 then
736 Date_N := Date_N - Time_Rep (Adj_Cent) * Nanos_In_Day;
737 end if;
738
739 -- Shift the date within bounds of Unix time
740
741 while Date_N < Unix_Min loop
742 Date_N := Date_N + Nanos_In_56_Years;
743 end loop;
744
745 while Date_N >= Unix_Max loop
746 Date_N := Date_N - Nanos_In_56_Years;
747 end loop;
748
749 -- Perform a shift in origins from Ada to Unix
750
751 Date_N := Date_N - Unix_Min;
752
753 -- Convert the date into seconds
754
755 Secs_T := System.OS_Lib.To_Ada (Long_Long_Integer (Date_N / Nano));
756
757 -- Determine whether to treat the input date as historical or not. A
758 -- value of "0" signifies that the date is NOT historic.
759
760 Flag := (if Is_Historic then 1 else 0);
761
762 localtime_tzoff
763 (Secs_T'Unchecked_Access,
764 Flag'Unchecked_Access,
765 Offset'Unchecked_Access);
766 pragma Annotate (CodePeer, Modified, Offset);
767
768 return Long_Integer (Offset);
769 end UTC_Time_Offset;
770
771 ----------
772 -- Year --
773 ----------
774
775 function Year (Date : Time) return Year_Number is
776 Y : Year_Number;
777 M : Month_Number;
778 D : Day_Number;
779 S : Day_Duration;
780 begin
781 Split (Date, Y, M, D, S);
782 return Y;
783 end Year;
784
785 -- The following packages assume that Time is a signed 64 bit integer
786 -- type, the units are nanoseconds and the origin is the start of Ada
787 -- time (1901-01-01 00:00:00.0 UTC).
788
789 ---------------------------
790 -- Arithmetic_Operations --
791 ---------------------------
792
793 package body Arithmetic_Operations is
794
795 ---------
796 -- Add --
797 ---------
798
799 function Add (Date : Time; Days : Long_Integer) return Time is
800 pragma Unsuppress (Overflow_Check);
801 Date_N : constant Time_Rep := Time_Rep (Date);
802 begin
803 return Time (Date_N + Time_Rep (Days) * Nanos_In_Day);
804 exception
805 when Constraint_Error =>
806 raise Time_Error;
807 end Add;
808
809 ----------------
810 -- Difference --
811 ----------------
812
813 procedure Difference
814 (Left : Time;
815 Right : Time;
816 Days : out Long_Integer;
817 Seconds : out Duration;
818 Leap_Seconds : out Integer)
819 is
820 Res_Dur : Time_Dur;
821 Earlier : Time_Rep;
822 Later : Time_Rep;
823 Negate : Boolean := False;
824 Sub_Secs : Duration;
825 Sub_Secs_Diff : Time_Rep;
826
827 begin
828 -- Both input time values are assumed to be in UTC
829
830 if Left >= Right then
831 Later := Time_Rep (Left);
832 Earlier := Time_Rep (Right);
833 else
834 Later := Time_Rep (Right);
835 Earlier := Time_Rep (Left);
836 Negate := True;
837 end if;
838
839 -- Sub seconds processing. We add the resulting difference to one
840 -- of the input dates in order to account for any potential rounding
841 -- of the difference in the next step.
842
843 Sub_Secs_Diff := Later mod Nano - Earlier mod Nano;
844 Earlier := Earlier + Sub_Secs_Diff;
845 Sub_Secs := Duration (Sub_Secs_Diff) / Nano_F;
846
847 -- Difference processing. This operation should be able to calculate
848 -- the difference between opposite values which are close to the end
849 -- and start of Ada time. To accommodate the large range, we convert
850 -- to seconds. This action may potentially round the two values and
851 -- either add or drop a second. We compensate for this issue in the
852 -- previous step.
853
854 Leap_Seconds := Elapsed_Leaps (Earlier, Later);
855
856 Res_Dur :=
857 Time_Dur (Later / Nano - Earlier / Nano) -
858 Time_Dur (Leap_Seconds);
859
860 Days := Long_Integer (Res_Dur / Secs_In_Day);
861 Seconds := Duration (Res_Dur mod Secs_In_Day) + Sub_Secs;
862
863 if Negate then
864 Days := -Days;
865 Seconds := -Seconds;
866
867 if Leap_Seconds /= 0 then
868 Leap_Seconds := -Leap_Seconds;
869 end if;
870 end if;
871 end Difference;
872
873 --------------
874 -- Subtract --
875 --------------
876
877 function Subtract (Date : Time; Days : Long_Integer) return Time is
878 pragma Unsuppress (Overflow_Check);
879 Date_N : constant Time_Rep := Time_Rep (Date);
880 begin
881 return Time (Date_N - Time_Rep (Days) * Nanos_In_Day);
882 exception
883 when Constraint_Error =>
884 raise Time_Error;
885 end Subtract;
886
887 end Arithmetic_Operations;
888
889 ---------------------------
890 -- Conversion_Operations --
891 ---------------------------
892
893 package body Conversion_Operations is
894
895 -----------------
896 -- To_Ada_Time --
897 -----------------
898
899 function To_Ada_Time (Unix_Time : Long_Integer) return Time is
900 begin
901 return To_Ada_Time_64 (Long_Long_Integer (Unix_Time));
902 end To_Ada_Time;
903
904 --------------------
905 -- To_Ada_Time_64 --
906 --------------------
907
908 function To_Ada_Time_64 (Unix_Time : Long_Long_Integer) return Time is
909 pragma Unsuppress (Overflow_Check);
910 Ada_Rep : Time_Rep := Time_Rep (Unix_Time * Nano) - Epoch_Offset;
911
912 -- Count leaps passed until the converted time.
913
914 Leaps : constant Natural :=
915 Elapsed_Leaps (Start_Of_Time, Ada_Rep);
916 begin
917
918 -- If leap seconds were found then update the result accordingly
919
920 if Leaps /= 0 then
921 declare
922 -- adjust the time by the number of leap seconds
923 Corrected_Ada_Rep : constant Time_Rep :=
924 Ada_Rep + Time_Rep ((Leaps) * Nano);
925
926 -- Check if the corrected time passed the boundary
927 -- of another leap second
928 Extra_Leaps : constant Natural :=
929 Elapsed_Leaps (Ada_Rep, Corrected_Ada_Rep);
930 begin
931 Ada_Rep := Corrected_Ada_Rep + Time_Rep (Extra_Leaps * Nano);
932 end;
933 end if;
934
935 return Time (Ada_Rep);
936
937 exception
938 when Constraint_Error =>
939 raise Time_Error;
940 end To_Ada_Time_64;
941
942 -----------------
943 -- To_Ada_Time --
944 -----------------
945
946 function To_Ada_Time
947 (tm_year : Integer;
948 tm_mon : Integer;
949 tm_day : Integer;
950 tm_hour : Integer;
951 tm_min : Integer;
952 tm_sec : Integer;
953 tm_isdst : Integer) return Time
954 is
955 pragma Unsuppress (Overflow_Check);
956 Year : Year_Number;
957 Month : Month_Number;
958 Day : Day_Number;
959 Second : Integer;
960 Leap : Boolean;
961 Result : Time_Rep;
962
963 begin
964 -- Input processing
965
966 Year := Year_Number (1900 + tm_year);
967 Month := Month_Number (1 + tm_mon);
968 Day := Day_Number (tm_day);
969
970 -- Step 1: Validity checks of input values
971
972 if not Year'Valid or else not Month'Valid or else not Day'Valid
973 or else tm_hour not in 0 .. 24
974 or else tm_min not in 0 .. 59
975 or else tm_sec not in 0 .. 60
976 or else tm_isdst not in -1 .. 1
977 then
978 raise Time_Error;
979 end if;
980
981 -- Step 2: Potential leap second
982
983 if tm_sec = 60 then
984 Leap := True;
985 Second := 59;
986 else
987 Leap := False;
988 Second := tm_sec;
989 end if;
990
991 -- Step 3: Calculate the time value
992
993 Result :=
994 Time_Rep
995 (Formatting_Operations.Time_Of
996 (Year => Year,
997 Month => Month,
998 Day => Day,
999 Day_Secs => 0.0, -- Time is given in h:m:s
1000 Hour => tm_hour,
1001 Minute => tm_min,
1002 Second => Second,
1003 Sub_Sec => 0.0, -- No precise sub second given
1004 Leap_Sec => Leap,
1005 Use_Day_Secs => False, -- Time is given in h:m:s
1006 Use_TZ => True, -- Force usage of explicit time zone
1007 Is_Historic => True,
1008 Time_Zone => 0)); -- Place the value in UTC
1009
1010 -- Step 4: Daylight Savings Time
1011
1012 if tm_isdst = 1 then
1013 Result := Result + Time_Rep (3_600) * Nano;
1014 end if;
1015
1016 return Time (Result);
1017
1018 exception
1019 when Constraint_Error =>
1020 raise Time_Error;
1021 end To_Ada_Time;
1022
1023 -----------------
1024 -- To_Duration --
1025 -----------------
1026
1027 function To_Duration
1028 (tv_sec : Long_Integer;
1029 tv_nsec : Long_Integer) return Duration
1030 is
1031 begin
1032 return To_Duration_64 (Long_Long_Integer (tv_sec), tv_nsec);
1033 end To_Duration;
1034
1035 --------------------
1036 -- To_Duration_64 --
1037 --------------------
1038
1039 function To_Duration_64
1040 (tv_sec : Long_Long_Integer;
1041 tv_nsec : Long_Integer) return Duration
1042 is
1043 pragma Unsuppress (Overflow_Check);
1044 begin
1045 return Duration (tv_sec) + Duration (tv_nsec) / Nano_F;
1046 end To_Duration_64;
1047
1048 ------------------------
1049 -- To_Struct_Timespec --
1050 ------------------------
1051
1052 procedure To_Struct_Timespec
1053 (D : Duration;
1054 tv_sec : out Long_Integer;
1055 tv_nsec : out Long_Integer)
1056 is
1057 begin
1058 To_Struct_Timespec_64 (D, Long_Long_Integer (tv_sec), tv_nsec);
1059 end To_Struct_Timespec;
1060
1061 ---------------------------
1062 -- To_Struct_Timespec_64 --
1063 ---------------------------
1064
1065 procedure To_Struct_Timespec_64
1066 (D : Duration;
1067 tv_sec : out Long_Long_Integer;
1068 tv_nsec : out Long_Integer)
1069 is
1070 pragma Unsuppress (Overflow_Check);
1071 Secs : Duration;
1072 Nano_Secs : Duration;
1073
1074 begin
1075 -- Seconds extraction, avoid potential rounding errors
1076
1077 Secs := D - 0.5;
1078 tv_sec := Long_Long_Integer (Secs);
1079
1080 -- Nanoseconds extraction
1081
1082 Nano_Secs := D - Duration (tv_sec);
1083 tv_nsec := Long_Integer (Nano_Secs * Nano);
1084 end To_Struct_Timespec_64;
1085
1086 ------------------
1087 -- To_Struct_Tm --
1088 ------------------
1089
1090 procedure To_Struct_Tm
1091 (T : Time;
1092 tm_year : out Integer;
1093 tm_mon : out Integer;
1094 tm_day : out Integer;
1095 tm_hour : out Integer;
1096 tm_min : out Integer;
1097 tm_sec : out Integer)
1098 is
1099 pragma Unsuppress (Overflow_Check);
1100 Year : Year_Number;
1101 Month : Month_Number;
1102 Second : Integer;
1103 Day_Secs : Day_Duration;
1104 Sub_Sec : Duration;
1105 Leap_Sec : Boolean;
1106
1107 begin
1108 -- Step 1: Split the input time
1109
1110 Formatting_Operations.Split
1111 (Date => T,
1112 Year => Year,
1113 Month => Month,
1114 Day => tm_day,
1115 Day_Secs => Day_Secs,
1116 Hour => tm_hour,
1117 Minute => tm_min,
1118 Second => Second,
1119 Sub_Sec => Sub_Sec,
1120 Leap_Sec => Leap_Sec,
1121 Use_TZ => True,
1122 Is_Historic => False,
1123 Time_Zone => 0);
1124
1125 -- Step 2: Correct the year and month
1126
1127 tm_year := Year - 1900;
1128 tm_mon := Month - 1;
1129
1130 -- Step 3: Handle leap second occurrences
1131
1132 tm_sec := (if Leap_Sec then 60 else Second);
1133 end To_Struct_Tm;
1134
1135 ------------------
1136 -- To_Unix_Time --
1137 ------------------
1138
1139 function To_Unix_Time (Ada_Time : Time) return Long_Integer is
1140 begin
1141 return Long_Integer (To_Unix_Time_64 (Ada_Time));
1142 end To_Unix_Time;
1143
1144 ---------------------
1145 -- To_Unix_Time_64 --
1146 ---------------------
1147
1148 function To_Unix_Time_64 (Ada_Time : Time) return Long_Long_Integer is
1149 pragma Unsuppress (Overflow_Check);
1150 Ada_Rep : constant Time_Rep := Time_Rep (Ada_Time);
1151 begin
1152 return Long_Long_Integer ((Ada_Rep + Epoch_Offset) / Nano) -
1153 Long_Long_Integer (Elapsed_Leaps (Start_Of_Time, Ada_Rep));
1154 exception
1155 when Constraint_Error =>
1156 raise Time_Error;
1157 end To_Unix_Time_64;
1158 end Conversion_Operations;
1159
1160 ----------------------
1161 -- Delay_Operations --
1162 ----------------------
1163
1164 package body Delay_Operations is
1165
1166 -----------------
1167 -- To_Duration --
1168 -----------------
1169
1170 function To_Duration (Date : Time) return Duration is
1171 pragma Unsuppress (Overflow_Check);
1172
1173 Safe_Ada_High : constant Time_Rep := Ada_High - Epoch_Offset;
1174 -- This value represents a "safe" end of time. In order to perform a
1175 -- proper conversion to Unix duration, we will have to shift origins
1176 -- at one point. For very distant dates, this means an overflow check
1177 -- failure. To prevent this, the function returns the "safe" end of
1178 -- time (roughly 2219) which is still distant enough.
1179
1180 Res_N : Time_Rep;
1181
1182 begin
1183 Res_N := Time_Rep (Date);
1184
1185 -- Step 1: If the target supports leap seconds, remove any leap
1186 -- seconds elapsed up to the input date.
1187
1188 Res_N := Res_N -
1189 Time_Rep (Elapsed_Leaps (Start_Of_Time, Res_N)) * Nano;
1190
1191 -- Step 2: Perform a shift in origins to obtain a Unix equivalent of
1192 -- the input. Guard against very large delay values such as the end
1193 -- of time since the computation will overflow.
1194
1195 Res_N := (if Res_N > Safe_Ada_High then Safe_Ada_High
1196 else Res_N + Epoch_Offset);
1197
1198 return Time_Rep_To_Duration (Res_N);
1199 end To_Duration;
1200
1201 end Delay_Operations;
1202
1203 ---------------------------
1204 -- Formatting_Operations --
1205 ---------------------------
1206
1207 package body Formatting_Operations is
1208
1209 -----------------
1210 -- Day_Of_Week --
1211 -----------------
1212
1213 function Day_Of_Week (Date : Time) return Integer is
1214 Date_N : constant Time_Rep := Time_Rep (Date);
1215 Time_Zone : constant Long_Integer := UTC_Time_Offset (Date, True);
1216 Ada_Low_N : Time_Rep;
1217 Day_Count : Long_Integer;
1218 Day_Dur : Time_Dur;
1219 High_N : Time_Rep;
1220 Low_N : Time_Rep;
1221
1222 begin
1223 -- As declared, the Ada Epoch is set in UTC. For this calculation to
1224 -- work properly, both the Epoch and the input date must be in the
1225 -- same time zone. The following places the Epoch in the input date's
1226 -- time zone.
1227
1228 Ada_Low_N := Ada_Low - Time_Rep (Time_Zone) * Nano;
1229
1230 if Date_N > Ada_Low_N then
1231 High_N := Date_N;
1232 Low_N := Ada_Low_N;
1233 else
1234 High_N := Ada_Low_N;
1235 Low_N := Date_N;
1236 end if;
1237
1238 -- Determine the elapsed seconds since the start of Ada time
1239
1240 Day_Dur := Time_Dur (High_N / Nano - Low_N / Nano);
1241
1242 -- Count the number of days since the start of Ada time. 1901-01-01
1243 -- GMT was a Tuesday.
1244
1245 Day_Count := Long_Integer (Day_Dur / Secs_In_Day) + 1;
1246
1247 return Integer (Day_Count mod 7);
1248 end Day_Of_Week;
1249
1250 -----------
1251 -- Split --
1252 -----------
1253
1254 procedure Split
1255 (Date : Time;
1256 Year : out Year_Number;
1257 Month : out Month_Number;
1258 Day : out Day_Number;
1259 Day_Secs : out Day_Duration;
1260 Hour : out Integer;
1261 Minute : out Integer;
1262 Second : out Integer;
1263 Sub_Sec : out Duration;
1264 Leap_Sec : out Boolean;
1265 Use_TZ : Boolean;
1266 Is_Historic : Boolean;
1267 Time_Zone : Long_Integer)
1268 is
1269 -- The following constants represent the number of nanoseconds
1270 -- elapsed since the start of Ada time to and including the non
1271 -- leap centennial years.
1272
1273 Year_2101 : constant Time_Rep := Ada_Low +
1274 Time_Rep (49 * 366 + 151 * 365) * Nanos_In_Day;
1275 Year_2201 : constant Time_Rep := Ada_Low +
1276 Time_Rep (73 * 366 + 227 * 365) * Nanos_In_Day;
1277 Year_2301 : constant Time_Rep := Ada_Low +
1278 Time_Rep (97 * 366 + 303 * 365) * Nanos_In_Day;
1279
1280 Date_Dur : Time_Dur;
1281 Date_N : Time_Rep;
1282 Day_Seconds : Natural;
1283 Elapsed_Leaps : Natural;
1284 Four_Year_Segs : Natural;
1285 Hour_Seconds : Natural;
1286 Is_Leap_Year : Boolean;
1287 Next_Leap_N : Time_Rep;
1288 Rem_Years : Natural;
1289 Sub_Sec_N : Time_Rep;
1290 Year_Day : Natural;
1291
1292 begin
1293 Date_N := Time_Rep (Date);
1294
1295 -- Step 1: Leap seconds processing in UTC
1296
1297 if Leap_Support then
1298 Cumulative_Leap_Seconds
1299 (Start_Of_Time, Date_N, Elapsed_Leaps, Next_Leap_N);
1300
1301 Leap_Sec := Date_N >= Next_Leap_N;
1302
1303 if Leap_Sec then
1304 Elapsed_Leaps := Elapsed_Leaps + 1;
1305 end if;
1306
1307 -- The target does not support leap seconds
1308
1309 else
1310 Elapsed_Leaps := 0;
1311 Leap_Sec := False;
1312 end if;
1313
1314 Date_N := Date_N - Time_Rep (Elapsed_Leaps) * Nano;
1315
1316 -- Step 2: Time zone processing. This action converts the input date
1317 -- from GMT to the requested time zone. Applies from Ada 2005 on.
1318
1319 if Use_TZ then
1320 if Time_Zone /= 0 then
1321 Date_N := Date_N + Time_Rep (Time_Zone) * 60 * Nano;
1322 end if;
1323
1324 -- Ada 83 and 95
1325
1326 else
1327 declare
1328 Off : constant Long_Integer :=
1329 UTC_Time_Offset (Time (Date_N), Is_Historic);
1330
1331 begin
1332 Date_N := Date_N + Time_Rep (Off) * Nano;
1333 end;
1334 end if;
1335
1336 -- Step 3: Non-leap centennial year adjustment in local time zone
1337
1338 -- In order for all divisions to work properly and to avoid more
1339 -- complicated arithmetic, we add fake February 29s to dates which
1340 -- occur after a non-leap centennial year.
1341
1342 if Date_N >= Year_2301 then
1343 Date_N := Date_N + Time_Rep (3) * Nanos_In_Day;
1344
1345 elsif Date_N >= Year_2201 then
1346 Date_N := Date_N + Time_Rep (2) * Nanos_In_Day;
1347
1348 elsif Date_N >= Year_2101 then
1349 Date_N := Date_N + Time_Rep (1) * Nanos_In_Day;
1350 end if;
1351
1352 -- Step 4: Sub second processing in local time zone
1353
1354 Sub_Sec_N := Date_N mod Nano;
1355 Sub_Sec := Duration (Sub_Sec_N) / Nano_F;
1356 Date_N := Date_N - Sub_Sec_N;
1357
1358 -- Convert Date_N into a time duration value, changing the units
1359 -- to seconds.
1360
1361 Date_Dur := Time_Dur (Date_N / Nano - Ada_Low / Nano);
1362
1363 -- Step 5: Year processing in local time zone. Determine the number
1364 -- of four year segments since the start of Ada time and the input
1365 -- date.
1366
1367 Four_Year_Segs := Natural (Date_Dur / Secs_In_Four_Years);
1368
1369 if Four_Year_Segs > 0 then
1370 Date_Dur := Date_Dur - Time_Dur (Four_Year_Segs) *
1371 Secs_In_Four_Years;
1372 end if;
1373
1374 -- Calculate the remaining non-leap years
1375
1376 Rem_Years := Natural (Date_Dur / Secs_In_Non_Leap_Year);
1377
1378 if Rem_Years > 3 then
1379 Rem_Years := 3;
1380 end if;
1381
1382 Date_Dur := Date_Dur - Time_Dur (Rem_Years) * Secs_In_Non_Leap_Year;
1383
1384 Year := Ada_Min_Year + Natural (4 * Four_Year_Segs + Rem_Years);
1385 Is_Leap_Year := Is_Leap (Year);
1386
1387 -- Step 6: Month and day processing in local time zone
1388
1389 Year_Day := Natural (Date_Dur / Secs_In_Day) + 1;
1390
1391 Month := 1;
1392
1393 -- Processing for months after January
1394
1395 if Year_Day > 31 then
1396 Month := 2;
1397 Year_Day := Year_Day - 31;
1398
1399 -- Processing for a new month or a leap February
1400
1401 if Year_Day > 28
1402 and then (not Is_Leap_Year or else Year_Day > 29)
1403 then
1404 Month := 3;
1405 Year_Day := Year_Day - 28;
1406
1407 if Is_Leap_Year then
1408 Year_Day := Year_Day - 1;
1409 end if;
1410
1411 -- Remaining months
1412
1413 while Year_Day > Days_In_Month (Month) loop
1414 Year_Day := Year_Day - Days_In_Month (Month);
1415 Month := Month + 1;
1416 end loop;
1417 end if;
1418 end if;
1419
1420 -- Step 7: Hour, minute, second and sub second processing in local
1421 -- time zone.
1422
1423 Day := Day_Number (Year_Day);
1424 Day_Seconds := Integer (Date_Dur mod Secs_In_Day);
1425 Day_Secs := Duration (Day_Seconds) + Sub_Sec;
1426 Hour := Day_Seconds / 3_600;
1427 Hour_Seconds := Day_Seconds mod 3_600;
1428 Minute := Hour_Seconds / 60;
1429 Second := Hour_Seconds mod 60;
1430
1431 exception
1432 when Constraint_Error =>
1433 raise Time_Error;
1434 end Split;
1435
1436 -------------
1437 -- Time_Of --
1438 -------------
1439
1440 function Time_Of
1441 (Year : Year_Number;
1442 Month : Month_Number;
1443 Day : Day_Number;
1444 Day_Secs : Day_Duration;
1445 Hour : Integer;
1446 Minute : Integer;
1447 Second : Integer;
1448 Sub_Sec : Duration;
1449 Leap_Sec : Boolean;
1450 Use_Day_Secs : Boolean;
1451 Use_TZ : Boolean;
1452 Is_Historic : Boolean;
1453 Time_Zone : Long_Integer) return Time
1454 is
1455 Count : Integer;
1456 Elapsed_Leaps : Natural;
1457 Next_Leap_N : Time_Rep;
1458 Res_N : Time_Rep;
1459 Rounded_Res_N : Time_Rep;
1460
1461 begin
1462 -- Step 1: Check whether the day, month and year form a valid date
1463
1464 if Day > Days_In_Month (Month)
1465 and then (Day /= 29 or else Month /= 2 or else not Is_Leap (Year))
1466 then
1467 raise Time_Error;
1468 end if;
1469
1470 -- Start accumulating nanoseconds from the low bound of Ada time
1471
1472 Res_N := Ada_Low;
1473
1474 -- Step 2: Year processing and centennial year adjustment. Determine
1475 -- the number of four year segments since the start of Ada time and
1476 -- the input date.
1477
1478 Count := (Year - Year_Number'First) / 4;
1479
1480 for Four_Year_Segments in 1 .. Count loop
1481 Res_N := Res_N + Nanos_In_Four_Years;
1482 end loop;
1483
1484 -- Note that non-leap centennial years are automatically considered
1485 -- leap in the operation above. An adjustment of several days is
1486 -- required to compensate for this.
1487
1488 if Year > 2300 then
1489 Res_N := Res_N - Time_Rep (3) * Nanos_In_Day;
1490
1491 elsif Year > 2200 then
1492 Res_N := Res_N - Time_Rep (2) * Nanos_In_Day;
1493
1494 elsif Year > 2100 then
1495 Res_N := Res_N - Time_Rep (1) * Nanos_In_Day;
1496 end if;
1497
1498 -- Add the remaining non-leap years
1499
1500 Count := (Year - Year_Number'First) mod 4;
1501 Res_N := Res_N + Time_Rep (Count) * Secs_In_Non_Leap_Year * Nano;
1502
1503 -- Step 3: Day of month processing. Determine the number of days
1504 -- since the start of the current year. Do not add the current
1505 -- day since it has not elapsed yet.
1506
1507 Count := Cumulative_Days_Before_Month (Month) + Day - 1;
1508
1509 -- The input year is leap and we have passed February
1510
1511 if Is_Leap (Year)
1512 and then Month > 2
1513 then
1514 Count := Count + 1;
1515 end if;
1516
1517 Res_N := Res_N + Time_Rep (Count) * Nanos_In_Day;
1518
1519 -- Step 4: Hour, minute, second and sub second processing
1520
1521 if Use_Day_Secs then
1522 Res_N := Res_N + Duration_To_Time_Rep (Day_Secs);
1523
1524 else
1525 Res_N :=
1526 Res_N + Time_Rep (Hour * 3_600 + Minute * 60 + Second) * Nano;
1527
1528 if Sub_Sec = 1.0 then
1529 Res_N := Res_N + Time_Rep (1) * Nano;
1530 else
1531 Res_N := Res_N + Duration_To_Time_Rep (Sub_Sec);
1532 end if;
1533 end if;
1534
1535 -- At this point, the generated time value should be withing the
1536 -- bounds of Ada time.
1537
1538 Check_Within_Time_Bounds (Res_N);
1539
1540 -- Step 4: Time zone processing. At this point we have built an
1541 -- arbitrary time value which is not related to any time zone.
1542 -- For simplicity, the time value is normalized to GMT, producing
1543 -- a uniform representation which can be treated by arithmetic
1544 -- operations for instance without any additional corrections.
1545
1546 if Use_TZ then
1547 if Time_Zone /= 0 then
1548 Res_N := Res_N - Time_Rep (Time_Zone) * 60 * Nano;
1549 end if;
1550
1551 -- Ada 83 and 95
1552
1553 else
1554 declare
1555 Cur_Off : constant Long_Integer :=
1556 UTC_Time_Offset (Time (Res_N), Is_Historic);
1557 Cur_Res_N : constant Time_Rep :=
1558 Res_N - Time_Rep (Cur_Off) * Nano;
1559 Off : constant Long_Integer :=
1560 UTC_Time_Offset (Time (Cur_Res_N), Is_Historic);
1561
1562 begin
1563 Res_N := Res_N - Time_Rep (Off) * Nano;
1564 end;
1565 end if;
1566
1567 -- Step 5: Leap seconds processing in GMT
1568
1569 if Leap_Support then
1570 Cumulative_Leap_Seconds
1571 (Start_Of_Time, Res_N, Elapsed_Leaps, Next_Leap_N);
1572
1573 Res_N := Res_N + Time_Rep (Elapsed_Leaps) * Nano;
1574
1575 -- An Ada 2005 caller requesting an explicit leap second or an
1576 -- Ada 95 caller accounting for an invisible leap second.
1577
1578 if Leap_Sec or else Res_N >= Next_Leap_N then
1579 Res_N := Res_N + Time_Rep (1) * Nano;
1580 end if;
1581
1582 -- Leap second validity check
1583
1584 Rounded_Res_N := Res_N - (Res_N mod Nano);
1585
1586 if Use_TZ
1587 and then Leap_Sec
1588 and then Rounded_Res_N /= Next_Leap_N
1589 then
1590 raise Time_Error;
1591 end if;
1592 end if;
1593
1594 return Time (Res_N);
1595 end Time_Of;
1596
1597 end Formatting_Operations;
1598
1599 ---------------------------
1600 -- Time_Zones_Operations --
1601 ---------------------------
1602
1603 package body Time_Zones_Operations is
1604
1605 ---------------------
1606 -- UTC_Time_Offset --
1607 ---------------------
1608
1609 function UTC_Time_Offset (Date : Time) return Long_Integer is
1610 begin
1611 return UTC_Time_Offset (Date, True);
1612 end UTC_Time_Offset;
1613
1614 end Time_Zones_Operations;
1615
1616 -- Start of elaboration code for Ada.Calendar
1617
1618 begin
1619 System.OS_Primitives.Initialize;
1620
1621 end Ada.Calendar;