1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . C A L E N D A R --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada
.Unchecked_Conversion
;
34 with System
.OS_Primitives
;
36 package body Ada
.Calendar
is
38 --------------------------
39 -- Implementation Notes --
40 --------------------------
42 -- In complex algorithms, some variables of type Ada.Calendar.Time carry
43 -- suffix _S or _N to denote units of seconds or nanoseconds.
45 -- Because time is measured in different units and from different origins
46 -- on various targets, a system independent model is incorporated into
47 -- Ada.Calendar. The idea behind the design is to encapsulate all target
48 -- dependent machinery in a single package, thus providing a uniform
49 -- interface to all existing and any potential children.
51 -- package Ada.Calendar
52 -- procedure Split (5 parameters) -------+
53 -- | Call from local routine
55 -- package Formatting_Operations |
56 -- procedure Split (11 parameters) <--+
57 -- end Formatting_Operations |
60 -- package Ada.Calendar.Formatting | Call from child routine
61 -- procedure Split (9 or 10 parameters) -+
62 -- end Ada.Calendar.Formatting
64 -- The behaviour of the interfacing routines is controlled via various
65 -- flags. All new Ada 2005 types from children of Ada.Calendar are
66 -- emulated by a similar type. For instance, type Day_Number is replaced
67 -- by Integer in various routines. One ramification of this model is that
68 -- the caller site must perform validity checks on returned results.
69 -- The end result of this model is the lack of target specific files per
70 -- child of Ada.Calendar (a-calfor, a-calfor-vms, a-calfor-vxwors, etc).
72 -----------------------
73 -- Local Subprograms --
74 -----------------------
76 procedure Check_Within_Time_Bounds
(T
: Time_Rep
);
77 -- Ensure that a time representation value falls withing the bounds of Ada
78 -- time. Leap seconds support is taken into account.
80 procedure Cumulative_Leap_Seconds
81 (Start_Date
: Time_Rep
;
83 Elapsed_Leaps
: out Natural;
84 Next_Leap
: out Time_Rep
);
85 -- Elapsed_Leaps is the sum of the leap seconds that have occurred on or
86 -- after Start_Date and before (strictly before) End_Date. Next_Leap_Sec
87 -- represents the next leap second occurrence on or after End_Date. If
88 -- there are no leaps seconds after End_Date, End_Of_Time is returned.
89 -- End_Of_Time can be used as End_Date to count all the leap seconds that
90 -- have occurred on or after Start_Date.
92 -- Note: Any sub seconds of Start_Date and End_Date are discarded before
93 -- the calculations are done. For instance: if 113 seconds is a leap
94 -- second (it isn't) and 113.5 is input as an End_Date, the leap second
95 -- at 113 will not be counted in Leaps_Between, but it will be returned
96 -- as Next_Leap_Sec. Thus, if the caller wants to know if the End_Date is
97 -- a leap second, the comparison should be:
99 -- End_Date >= Next_Leap_Sec;
101 -- After_Last_Leap is designed so that this comparison works without
102 -- having to first check if Next_Leap_Sec is a valid leap second.
104 function Duration_To_Time_Rep
is
105 new Ada
.Unchecked_Conversion
(Duration, Time_Rep
);
106 -- Convert a duration value into a time representation value
108 function Time_Rep_To_Duration
is
109 new Ada
.Unchecked_Conversion
(Time_Rep
, Duration);
110 -- Convert a time representation value into a duration value
116 -- An integer time duration. The type is used whenever a positive elapsed
117 -- duration is needed, for instance when splitting a time value. Here is
118 -- how Time_Rep and Time_Dur are related:
120 -- 'First Ada_Low Ada_High 'Last
121 -- Time_Rep: +-------+------------------------+---------+
122 -- Time_Dur: +------------------------+---------+
125 type Time_Dur
is range 0 .. 2 ** 63 - 1;
127 --------------------------
128 -- Leap seconds control --
129 --------------------------
132 pragma Import
(C
, Flag
, "__gl_leap_seconds_support");
133 -- This imported value is used to determine whether the compilation had
134 -- binder flag "-y" present which enables leap seconds. A value of zero
135 -- signifies no leap seconds support while a value of one enables the
138 Leap_Support
: constant Boolean := Flag
= 1;
139 -- The above flag controls the usage of leap seconds in all Ada.Calendar
142 Leap_Seconds_Count
: constant Natural := 24;
144 ---------------------
145 -- Local Constants --
146 ---------------------
148 Ada_Min_Year
: constant Year_Number
:= Year_Number
'First;
149 Secs_In_Four_Years
: constant := (3 * 365 + 366) * Secs_In_Day
;
150 Secs_In_Non_Leap_Year
: constant := 365 * Secs_In_Day
;
151 Nanos_In_Four_Years
: constant := Secs_In_Four_Years
* Nano
;
153 -- Lower and upper bound of Ada time. The zero (0) value of type Time is
154 -- positioned at year 2150. Note that the lower and upper bound account
155 -- for the non-leap centennial years.
157 Ada_Low
: constant Time_Rep
:= -(61 * 366 + 188 * 365) * Nanos_In_Day
;
158 Ada_High
: constant Time_Rep
:= (60 * 366 + 190 * 365) * Nanos_In_Day
;
160 -- Even though the upper bound of time is 2399-12-31 23:59:59.999999999
161 -- UTC, it must be increased to include all leap seconds.
163 Ada_High_And_Leaps
: constant Time_Rep
:=
164 Ada_High
+ Time_Rep
(Leap_Seconds_Count
) * Nano
;
166 -- Two constants used in the calculations of elapsed leap seconds.
167 -- End_Of_Time is later than Ada_High in time zone -28. Start_Of_Time
168 -- is earlier than Ada_Low in time zone +28.
170 End_Of_Time
: constant Time_Rep
:=
171 Ada_High
+ Time_Rep
(3) * Nanos_In_Day
;
172 Start_Of_Time
: constant Time_Rep
:=
173 Ada_Low
- Time_Rep
(3) * Nanos_In_Day
;
175 -- The Unix lower time bound expressed as nanoseconds since the
176 -- start of Ada time in UTC.
178 Unix_Min
: constant Time_Rep
:=
179 Ada_Low
+ Time_Rep
(17 * 366 + 52 * 365) * Nanos_In_Day
;
181 Epoch_Offset
: constant Time_Rep
:= (136 * 365 + 44 * 366) * Nanos_In_Day
;
182 -- The difference between 2150-1-1 UTC and 1970-1-1 UTC expressed in
183 -- nanoseconds. Note that year 2100 is non-leap.
185 Cumulative_Days_Before_Month
:
186 constant array (Month_Number
) of Natural :=
187 (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
189 -- The following table contains the hard time values of all existing leap
190 -- seconds. The values are produced by the utility program xleaps.adb.
192 Leap_Second_Times
: constant array (1 .. Leap_Seconds_Count
) of Time_Rep
:=
193 (-5601484800000000000,
194 -5585587199000000000,
195 -5554051198000000000,
196 -5522515197000000000,
197 -5490979196000000000,
198 -5459356795000000000,
199 -5427820794000000000,
200 -5396284793000000000,
201 -5364748792000000000,
202 -5317487991000000000,
203 -5285951990000000000,
204 -5254415989000000000,
205 -5191257588000000000,
206 -5112287987000000000,
207 -5049129586000000000,
208 -5017593585000000000,
209 -4970332784000000000,
210 -4938796783000000000,
211 -4907260782000000000,
212 -4859827181000000000,
213 -4812566380000000000,
214 -4765132779000000000,
215 -4544207978000000000,
216 -4449513577000000000);
222 function "+" (Left
: Time
; Right
: Duration) return Time
is
223 pragma Unsuppress
(Overflow_Check
);
224 Left_N
: constant Time_Rep
:= Time_Rep
(Left
);
226 return Time
(Left_N
+ Duration_To_Time_Rep
(Right
));
228 when Constraint_Error
=>
232 function "+" (Left
: Duration; Right
: Time
) return Time
is
241 function "-" (Left
: Time
; Right
: Duration) return Time
is
242 pragma Unsuppress
(Overflow_Check
);
243 Left_N
: constant Time_Rep
:= Time_Rep
(Left
);
245 return Time
(Left_N
- Duration_To_Time_Rep
(Right
));
247 when Constraint_Error
=>
251 function "-" (Left
: Time
; Right
: Time
) return Duration is
252 pragma Unsuppress
(Overflow_Check
);
254 -- The bounds of type Duration expressed as time representations
256 Dur_Low
: constant Time_Rep
:= Duration_To_Time_Rep
(Duration'First);
257 Dur_High
: constant Time_Rep
:= Duration_To_Time_Rep
(Duration'Last);
262 Res_N
:= Time_Rep
(Left
) - Time_Rep
(Right
);
264 -- Due to the extended range of Ada time, "-" is capable of producing
265 -- results which may exceed the range of Duration. In order to prevent
266 -- the generation of bogus values by the Unchecked_Conversion, we apply
267 -- the following check.
270 or else Res_N
> Dur_High
275 return Time_Rep_To_Duration
(Res_N
);
277 when Constraint_Error
=>
285 function "<" (Left
, Right
: Time
) return Boolean is
287 return Time_Rep
(Left
) < Time_Rep
(Right
);
294 function "<=" (Left
, Right
: Time
) return Boolean is
296 return Time_Rep
(Left
) <= Time_Rep
(Right
);
303 function ">" (Left
, Right
: Time
) return Boolean is
305 return Time_Rep
(Left
) > Time_Rep
(Right
);
312 function ">=" (Left
, Right
: Time
) return Boolean is
314 return Time_Rep
(Left
) >= Time_Rep
(Right
);
317 ------------------------------
318 -- Check_Within_Time_Bounds --
319 ------------------------------
321 procedure Check_Within_Time_Bounds
(T
: Time_Rep
) is
324 if T
< Ada_Low
or else T
> Ada_High_And_Leaps
then
328 if T
< Ada_Low
or else T
> Ada_High
then
332 end Check_Within_Time_Bounds
;
338 function Clock
return Time
is
339 Elapsed_Leaps
: Natural;
340 Next_Leap_N
: Time_Rep
;
342 -- The system clock returns the time in UTC since the Unix Epoch of
343 -- 1970-01-01 00:00:00.0. We perform an origin shift to the Ada Epoch
344 -- by adding the number of nanoseconds between the two origins.
347 Duration_To_Time_Rep
(System
.OS_Primitives
.Clock
) +
351 -- If the target supports leap seconds, determine the number of leap
352 -- seconds elapsed until this moment.
355 Cumulative_Leap_Seconds
356 (Start_Of_Time
, Res_N
, Elapsed_Leaps
, Next_Leap_N
);
358 -- The system clock may fall exactly on a leap second
360 if Res_N
>= Next_Leap_N
then
361 Elapsed_Leaps
:= Elapsed_Leaps
+ 1;
364 -- The target does not support leap seconds
370 Res_N
:= Res_N
+ Time_Rep
(Elapsed_Leaps
) * Nano
;
375 -----------------------------
376 -- Cumulative_Leap_Seconds --
377 -----------------------------
379 procedure Cumulative_Leap_Seconds
380 (Start_Date
: Time_Rep
;
382 Elapsed_Leaps
: out Natural;
383 Next_Leap
: out Time_Rep
)
385 End_Index
: Positive;
386 End_T
: Time_Rep
:= End_Date
;
387 Start_Index
: Positive;
388 Start_T
: Time_Rep
:= Start_Date
;
391 -- Both input dates must be normalized to UTC
393 pragma Assert
(Leap_Support
and then End_Date
>= Start_Date
);
395 Next_Leap
:= End_Of_Time
;
397 -- Make sure that the end date does not exceed the upper bound
400 if End_Date
> Ada_High
then
404 -- Remove the sub seconds from both dates
406 Start_T
:= Start_T
- (Start_T
mod Nano
);
407 End_T
:= End_T
- (End_T
mod Nano
);
409 -- Some trivial cases:
410 -- Leap 1 . . . Leap N
411 -- ---+========+------+############+-------+========+-----
412 -- Start_T End_T Start_T End_T
414 if End_T
< Leap_Second_Times
(1) then
416 Next_Leap
:= Leap_Second_Times
(1);
419 elsif Start_T
> Leap_Second_Times
(Leap_Seconds_Count
) then
421 Next_Leap
:= End_Of_Time
;
425 -- Perform the calculations only if the start date is within the leap
426 -- second occurrences table.
428 if Start_T
<= Leap_Second_Times
(Leap_Seconds_Count
) then
431 -- +----+----+-- . . . --+-------+---+
432 -- | T1 | T2 | | N - 1 | N |
433 -- +----+----+-- . . . --+-------+---+
435 -- | Start_Index | End_Index
436 -- +-------------------+
439 -- The idea behind the algorithm is to iterate and find two
440 -- closest dates which are after Start_T and End_T. Their
441 -- corresponding index difference denotes the number of leap
446 exit when Leap_Second_Times
(Start_Index
) >= Start_T
;
447 Start_Index
:= Start_Index
+ 1;
450 End_Index
:= Start_Index
;
452 exit when End_Index
> Leap_Seconds_Count
453 or else Leap_Second_Times
(End_Index
) >= End_T
;
454 End_Index
:= End_Index
+ 1;
457 if End_Index
<= Leap_Seconds_Count
then
458 Next_Leap
:= Leap_Second_Times
(End_Index
);
461 Elapsed_Leaps
:= End_Index
- Start_Index
;
466 end Cumulative_Leap_Seconds
;
472 function Day
(Date
: Time
) return Day_Number
is
477 pragma Unreferenced
(Y
, M
, S
);
479 Split
(Date
, Y
, M
, D
, S
);
487 function Is_Leap
(Year
: Year_Number
) return Boolean is
489 -- Leap centennial years
491 if Year
mod 400 = 0 then
494 -- Non-leap centennial years
496 elsif Year
mod 100 = 0 then
502 return Year
mod 4 = 0;
510 function Month
(Date
: Time
) return Month_Number
is
515 pragma Unreferenced
(Y
, D
, S
);
517 Split
(Date
, Y
, M
, D
, S
);
525 function Seconds
(Date
: Time
) return Day_Duration
is
530 pragma Unreferenced
(Y
, M
, D
);
532 Split
(Date
, Y
, M
, D
, S
);
542 Year
: out Year_Number
;
543 Month
: out Month_Number
;
544 Day
: out Day_Number
;
545 Seconds
: out Day_Duration
)
553 pragma Unreferenced
(H
, M
, Se
, Ss
, Le
);
556 -- Even though the input time zone is UTC (0), the flag Is_Ada_05 will
557 -- ensure that Split picks up the local time zone.
559 Formatting_Operations
.Split
576 or else not Month
'Valid
577 or else not Day
'Valid
578 or else not Seconds
'Valid
590 Month
: Month_Number
;
592 Seconds
: Day_Duration
:= 0.0) return Time
594 -- The values in the following constants are irrelevant, they are just
595 -- placeholders; the choice of constructing a Day_Duration value is
596 -- controlled by the Use_Day_Secs flag.
598 H
: constant Integer := 1;
599 M
: constant Integer := 1;
600 Se
: constant Integer := 1;
601 Ss
: constant Duration := 0.1;
607 or else not Month
'Valid
608 or else not Day
'Valid
609 or else not Seconds
'Valid
614 -- Even though the input time zone is UTC (0), the flag Is_Ada_05 will
615 -- ensure that Split picks up the local time zone.
618 Formatting_Operations
.Time_Of
628 Use_Day_Secs
=> True,
637 function Year
(Date
: Time
) return Year_Number
is
642 pragma Unreferenced
(M
, D
, S
);
644 Split
(Date
, Y
, M
, D
, S
);
648 -- The following packages assume that Time is a signed 64 bit integer
649 -- type, the units are nanoseconds and the origin is the start of Ada
650 -- time (1901-01-01 00:00:00.0 UTC).
652 ---------------------------
653 -- Arithmetic_Operations --
654 ---------------------------
656 package body Arithmetic_Operations
is
662 function Add
(Date
: Time
; Days
: Long_Integer) return Time
is
663 pragma Unsuppress
(Overflow_Check
);
664 Date_N
: constant Time_Rep
:= Time_Rep
(Date
);
666 return Time
(Date_N
+ Time_Rep
(Days
) * Nanos_In_Day
);
668 when Constraint_Error
=>
679 Days
: out Long_Integer;
680 Seconds
: out Duration;
681 Leap_Seconds
: out Integer)
685 Elapsed_Leaps
: Natural;
687 Negate
: Boolean := False;
688 Next_Leap_N
: Time_Rep
;
690 Sub_Secs_Diff
: Time_Rep
;
693 -- Both input time values are assumed to be in UTC
695 if Left
>= Right
then
696 Later
:= Time_Rep
(Left
);
697 Earlier
:= Time_Rep
(Right
);
699 Later
:= Time_Rep
(Right
);
700 Earlier
:= Time_Rep
(Left
);
704 -- If the target supports leap seconds, process them
707 Cumulative_Leap_Seconds
708 (Earlier
, Later
, Elapsed_Leaps
, Next_Leap_N
);
710 if Later
>= Next_Leap_N
then
711 Elapsed_Leaps
:= Elapsed_Leaps
+ 1;
714 -- The target does not support leap seconds
720 -- Sub seconds processing. We add the resulting difference to one
721 -- of the input dates in order to account for any potential rounding
722 -- of the difference in the next step.
724 Sub_Secs_Diff
:= Later
mod Nano
- Earlier
mod Nano
;
725 Earlier
:= Earlier
+ Sub_Secs_Diff
;
726 Sub_Secs
:= Duration (Sub_Secs_Diff
) / Nano_F
;
728 -- Difference processing. This operation should be able to calculate
729 -- the difference between opposite values which are close to the end
730 -- and start of Ada time. To accommodate the large range, we convert
731 -- to seconds. This action may potentially round the two values and
732 -- either add or drop a second. We compensate for this issue in the
736 Time_Dur
(Later
/ Nano
- Earlier
/ Nano
) - Time_Dur
(Elapsed_Leaps
);
738 Days
:= Long_Integer (Res_Dur
/ Secs_In_Day
);
739 Seconds
:= Duration (Res_Dur
mod Secs_In_Day
) + Sub_Secs
;
740 Leap_Seconds
:= Integer (Elapsed_Leaps
);
746 if Leap_Seconds
/= 0 then
747 Leap_Seconds
:= -Leap_Seconds
;
756 function Subtract
(Date
: Time
; Days
: Long_Integer) return Time
is
757 pragma Unsuppress
(Overflow_Check
);
758 Date_N
: constant Time_Rep
:= Time_Rep
(Date
);
760 return Time
(Date_N
- Time_Rep
(Days
) * Nanos_In_Day
);
762 when Constraint_Error
=>
766 end Arithmetic_Operations
;
768 ---------------------------
769 -- Conversion_Operations --
770 ---------------------------
772 package body Conversion_Operations
is
778 function To_Ada_Time
(Unix_Time
: Long_Integer) return Time
is
779 pragma Unsuppress
(Overflow_Check
);
780 Unix_Rep
: constant Time_Rep
:= Time_Rep
(Unix_Time
) * Nano
;
782 return Time
(Unix_Rep
- Epoch_Offset
);
784 when Constraint_Error
=>
799 tm_isdst
: Integer) return Time
801 pragma Unsuppress
(Overflow_Check
);
803 Month
: Month_Number
;
812 Year
:= Year_Number
(1900 + tm_year
);
813 Month
:= Month_Number
(1 + tm_mon
);
814 Day
:= Day_Number
(tm_day
);
816 -- Step 1: Validity checks of input values
819 or else not Month
'Valid
820 or else not Day
'Valid
821 or else tm_hour
not in 0 .. 24
822 or else tm_min
not in 0 .. 59
823 or else tm_sec
not in 0 .. 60
824 or else tm_isdst
not in -1 .. 1
829 -- Step 2: Potential leap second
839 -- Step 3: Calculate the time value
843 (Formatting_Operations
.Time_Of
847 Day_Secs
=> 0.0, -- Time is given in h:m:s
851 Sub_Sec
=> 0.0, -- No precise sub second given
853 Use_Day_Secs
=> False, -- Time is given in h:m:s
854 Is_Ada_05
=> True, -- Force usage of explicit time zone
855 Time_Zone
=> 0)); -- Place the value in UTC
857 -- Step 4: Daylight Savings Time
860 Result
:= Result
+ Time_Rep
(3_600
) * Nano
;
863 return Time
(Result
);
866 when Constraint_Error
=>
875 (tv_sec
: Long_Integer;
876 tv_nsec
: Long_Integer) return Duration
878 pragma Unsuppress
(Overflow_Check
);
880 return Duration (tv_sec
) + Duration (tv_nsec
) / Nano_F
;
883 ------------------------
884 -- To_Struct_Timespec --
885 ------------------------
887 procedure To_Struct_Timespec
889 tv_sec
: out Long_Integer;
890 tv_nsec
: out Long_Integer)
892 pragma Unsuppress
(Overflow_Check
);
894 Nano_Secs
: Duration;
897 -- Seconds extraction, avoid potential rounding errors
900 tv_sec
:= Long_Integer (Secs
);
902 -- Nanoseconds extraction
904 Nano_Secs
:= D
- Duration (tv_sec
);
905 tv_nsec
:= Long_Integer (Nano_Secs
* Nano
);
906 end To_Struct_Timespec
;
912 procedure To_Struct_Tm
914 tm_year
: out Integer;
915 tm_mon
: out Integer;
916 tm_day
: out Integer;
917 tm_hour
: out Integer;
918 tm_min
: out Integer;
919 tm_sec
: out Integer)
921 pragma Unsuppress
(Overflow_Check
);
923 Month
: Month_Number
;
925 Day_Secs
: Day_Duration
;
930 -- Step 1: Split the input time
932 Formatting_Operations
.Split
933 (T
, Year
, Month
, tm_day
, Day_Secs
,
934 tm_hour
, tm_min
, Second
, Sub_Sec
, Leap_Sec
, True, 0);
936 -- Step 2: Correct the year and month
938 tm_year
:= Year
- 1900;
941 -- Step 3: Handle leap second occurrences
943 tm_sec
:= (if Leap_Sec
then 60 else Second
);
950 function To_Unix_Time
(Ada_Time
: Time
) return Long_Integer is
951 pragma Unsuppress
(Overflow_Check
);
952 Ada_Rep
: constant Time_Rep
:= Time_Rep
(Ada_Time
);
954 return Long_Integer ((Ada_Rep
+ Epoch_Offset
) / Nano
);
956 when Constraint_Error
=>
959 end Conversion_Operations
;
961 ----------------------
962 -- Delay_Operations --
963 ----------------------
965 package body Delay_Operations
is
971 function To_Duration
(Date
: Time
) return Duration is
972 pragma Unsuppress
(Overflow_Check
);
974 Safe_Ada_High
: constant Time_Rep
:= Ada_High
- Epoch_Offset
;
975 -- This value represents a "safe" end of time. In order to perform a
976 -- proper conversion to Unix duration, we will have to shift origins
977 -- at one point. For very distant dates, this means an overflow check
978 -- failure. To prevent this, the function returns the "safe" end of
979 -- time (roughly 2219) which is still distant enough.
981 Elapsed_Leaps
: Natural;
982 Next_Leap_N
: Time_Rep
;
986 Res_N
:= Time_Rep
(Date
);
988 -- Step 1: If the target supports leap seconds, remove any leap
989 -- seconds elapsed up to the input date.
992 Cumulative_Leap_Seconds
993 (Start_Of_Time
, Res_N
, Elapsed_Leaps
, Next_Leap_N
);
995 -- The input time value may fall on a leap second occurrence
997 if Res_N
>= Next_Leap_N
then
998 Elapsed_Leaps
:= Elapsed_Leaps
+ 1;
1001 -- The target does not support leap seconds
1007 Res_N
:= Res_N
- Time_Rep
(Elapsed_Leaps
) * Nano
;
1009 -- Step 2: Perform a shift in origins to obtain a Unix equivalent of
1010 -- the input. Guard against very large delay values such as the end
1011 -- of time since the computation will overflow.
1013 Res_N
:= (if Res_N
> Safe_Ada_High
then Safe_Ada_High
1014 else Res_N
+ Epoch_Offset
);
1016 return Time_Rep_To_Duration
(Res_N
);
1019 end Delay_Operations
;
1021 ---------------------------
1022 -- Formatting_Operations --
1023 ---------------------------
1025 package body Formatting_Operations
is
1031 function Day_Of_Week
(Date
: Time
) return Integer is
1032 Date_N
: constant Time_Rep
:= Time_Rep
(Date
);
1033 Time_Zone
: constant Long_Integer :=
1034 Time_Zones_Operations
.UTC_Time_Offset
(Date
);
1036 Ada_Low_N
: Time_Rep
;
1037 Day_Count
: Long_Integer;
1043 -- As declared, the Ada Epoch is set in UTC. For this calculation to
1044 -- work properly, both the Epoch and the input date must be in the
1045 -- same time zone. The following places the Epoch in the input date's
1048 Ada_Low_N
:= Ada_Low
- Time_Rep
(Time_Zone
) * Nano
;
1050 if Date_N
> Ada_Low_N
then
1054 High_N
:= Ada_Low_N
;
1058 -- Determine the elapsed seconds since the start of Ada time
1060 Day_Dur
:= Time_Dur
(High_N
/ Nano
- Low_N
/ Nano
);
1062 -- Count the number of days since the start of Ada time. 1901-01-01
1063 -- GMT was a Tuesday.
1065 Day_Count
:= Long_Integer (Day_Dur
/ Secs_In_Day
) + 1;
1067 return Integer (Day_Count
mod 7);
1076 Year
: out Year_Number
;
1077 Month
: out Month_Number
;
1078 Day
: out Day_Number
;
1079 Day_Secs
: out Day_Duration
;
1081 Minute
: out Integer;
1082 Second
: out Integer;
1083 Sub_Sec
: out Duration;
1084 Leap_Sec
: out Boolean;
1085 Is_Ada_05
: Boolean;
1086 Time_Zone
: Long_Integer)
1088 -- The following constants represent the number of nanoseconds
1089 -- elapsed since the start of Ada time to and including the non
1090 -- leap centennial years.
1092 Year_2101
: constant Time_Rep
:= Ada_Low
+
1093 Time_Rep
(49 * 366 + 151 * 365) * Nanos_In_Day
;
1094 Year_2201
: constant Time_Rep
:= Ada_Low
+
1095 Time_Rep
(73 * 366 + 227 * 365) * Nanos_In_Day
;
1096 Year_2301
: constant Time_Rep
:= Ada_Low
+
1097 Time_Rep
(97 * 366 + 303 * 365) * Nanos_In_Day
;
1099 Date_Dur
: Time_Dur
;
1101 Day_Seconds
: Natural;
1102 Elapsed_Leaps
: Natural;
1103 Four_Year_Segs
: Natural;
1104 Hour_Seconds
: Natural;
1105 Is_Leap_Year
: Boolean;
1106 Next_Leap_N
: Time_Rep
;
1107 Rem_Years
: Natural;
1108 Sub_Sec_N
: Time_Rep
;
1112 Date_N
:= Time_Rep
(Date
);
1114 -- Step 1: Leap seconds processing in UTC
1116 if Leap_Support
then
1117 Cumulative_Leap_Seconds
1118 (Start_Of_Time
, Date_N
, Elapsed_Leaps
, Next_Leap_N
);
1120 Leap_Sec
:= Date_N
>= Next_Leap_N
;
1123 Elapsed_Leaps
:= Elapsed_Leaps
+ 1;
1126 -- The target does not support leap seconds
1133 Date_N
:= Date_N
- Time_Rep
(Elapsed_Leaps
) * Nano
;
1135 -- Step 2: Time zone processing. This action converts the input date
1136 -- from GMT to the requested time zone.
1139 if Time_Zone
/= 0 then
1140 Date_N
:= Date_N
+ Time_Rep
(Time_Zone
) * 60 * Nano
;
1147 Off
: constant Long_Integer :=
1148 Time_Zones_Operations
.UTC_Time_Offset
(Time
(Date_N
));
1150 Date_N
:= Date_N
+ Time_Rep
(Off
) * Nano
;
1154 -- Step 3: Non-leap centennial year adjustment in local time zone
1156 -- In order for all divisions to work properly and to avoid more
1157 -- complicated arithmetic, we add fake February 29s to dates which
1158 -- occur after a non-leap centennial year.
1160 if Date_N
>= Year_2301
then
1161 Date_N
:= Date_N
+ Time_Rep
(3) * Nanos_In_Day
;
1163 elsif Date_N
>= Year_2201
then
1164 Date_N
:= Date_N
+ Time_Rep
(2) * Nanos_In_Day
;
1166 elsif Date_N
>= Year_2101
then
1167 Date_N
:= Date_N
+ Time_Rep
(1) * Nanos_In_Day
;
1170 -- Step 4: Sub second processing in local time zone
1172 Sub_Sec_N
:= Date_N
mod Nano
;
1173 Sub_Sec
:= Duration (Sub_Sec_N
) / Nano_F
;
1174 Date_N
:= Date_N
- Sub_Sec_N
;
1176 -- Convert Date_N into a time duration value, changing the units
1179 Date_Dur
:= Time_Dur
(Date_N
/ Nano
- Ada_Low
/ Nano
);
1181 -- Step 5: Year processing in local time zone. Determine the number
1182 -- of four year segments since the start of Ada time and the input
1185 Four_Year_Segs
:= Natural (Date_Dur
/ Secs_In_Four_Years
);
1187 if Four_Year_Segs
> 0 then
1188 Date_Dur
:= Date_Dur
- Time_Dur
(Four_Year_Segs
) *
1192 -- Calculate the remaining non-leap years
1194 Rem_Years
:= Natural (Date_Dur
/ Secs_In_Non_Leap_Year
);
1196 if Rem_Years
> 3 then
1200 Date_Dur
:= Date_Dur
- Time_Dur
(Rem_Years
) * Secs_In_Non_Leap_Year
;
1202 Year
:= Ada_Min_Year
+ Natural (4 * Four_Year_Segs
+ Rem_Years
);
1203 Is_Leap_Year
:= Is_Leap
(Year
);
1205 -- Step 6: Month and day processing in local time zone
1207 Year_Day
:= Natural (Date_Dur
/ Secs_In_Day
) + 1;
1211 -- Processing for months after January
1213 if Year_Day
> 31 then
1215 Year_Day
:= Year_Day
- 31;
1217 -- Processing for a new month or a leap February
1220 and then (not Is_Leap_Year
or else Year_Day
> 29)
1223 Year_Day
:= Year_Day
- 28;
1225 if Is_Leap_Year
then
1226 Year_Day
:= Year_Day
- 1;
1231 while Year_Day
> Days_In_Month
(Month
) loop
1232 Year_Day
:= Year_Day
- Days_In_Month
(Month
);
1238 -- Step 7: Hour, minute, second and sub second processing in local
1241 Day
:= Day_Number
(Year_Day
);
1242 Day_Seconds
:= Integer (Date_Dur
mod Secs_In_Day
);
1243 Day_Secs
:= Duration (Day_Seconds
) + Sub_Sec
;
1244 Hour
:= Day_Seconds
/ 3_600
;
1245 Hour_Seconds
:= Day_Seconds
mod 3_600
;
1246 Minute
:= Hour_Seconds
/ 60;
1247 Second
:= Hour_Seconds
mod 60;
1255 (Year
: Year_Number
;
1256 Month
: Month_Number
;
1258 Day_Secs
: Day_Duration
;
1263 Leap_Sec
: Boolean := False;
1264 Use_Day_Secs
: Boolean := False;
1265 Is_Ada_05
: Boolean := False;
1266 Time_Zone
: Long_Integer := 0) return Time
1269 Elapsed_Leaps
: Natural;
1270 Next_Leap_N
: Time_Rep
;
1272 Rounded_Res_N
: Time_Rep
;
1275 -- Step 1: Check whether the day, month and year form a valid date
1277 if Day
> Days_In_Month
(Month
)
1278 and then (Day
/= 29 or else Month
/= 2 or else not Is_Leap
(Year
))
1283 -- Start accumulating nanoseconds from the low bound of Ada time
1287 -- Step 2: Year processing and centennial year adjustment. Determine
1288 -- the number of four year segments since the start of Ada time and
1291 Count
:= (Year
- Year_Number
'First) / 4;
1292 for Four_Year_Segments
in 1 .. Count
loop
1293 Res_N
:= Res_N
+ Nanos_In_Four_Years
;
1296 -- Note that non-leap centennial years are automatically considered
1297 -- leap in the operation above. An adjustment of several days is
1298 -- required to compensate for this.
1301 Res_N
:= Res_N
- Time_Rep
(3) * Nanos_In_Day
;
1303 elsif Year
> 2200 then
1304 Res_N
:= Res_N
- Time_Rep
(2) * Nanos_In_Day
;
1306 elsif Year
> 2100 then
1307 Res_N
:= Res_N
- Time_Rep
(1) * Nanos_In_Day
;
1310 -- Add the remaining non-leap years
1312 Count
:= (Year
- Year_Number
'First) mod 4;
1313 Res_N
:= Res_N
+ Time_Rep
(Count
) * Secs_In_Non_Leap_Year
* Nano
;
1315 -- Step 3: Day of month processing. Determine the number of days
1316 -- since the start of the current year. Do not add the current
1317 -- day since it has not elapsed yet.
1319 Count
:= Cumulative_Days_Before_Month
(Month
) + Day
- 1;
1321 -- The input year is leap and we have passed February
1329 Res_N
:= Res_N
+ Time_Rep
(Count
) * Nanos_In_Day
;
1331 -- Step 4: Hour, minute, second and sub second processing
1333 if Use_Day_Secs
then
1334 Res_N
:= Res_N
+ Duration_To_Time_Rep
(Day_Secs
);
1338 Res_N
+ Time_Rep
(Hour
* 3_600
+ Minute
* 60 + Second
) * Nano
;
1340 if Sub_Sec
= 1.0 then
1341 Res_N
:= Res_N
+ Time_Rep
(1) * Nano
;
1343 Res_N
:= Res_N
+ Duration_To_Time_Rep
(Sub_Sec
);
1347 -- At this point, the generated time value should be withing the
1348 -- bounds of Ada time.
1350 Check_Within_Time_Bounds
(Res_N
);
1352 -- Step 4: Time zone processing. At this point we have built an
1353 -- arbitrary time value which is not related to any time zone.
1354 -- For simplicity, the time value is normalized to GMT, producing
1355 -- a uniform representation which can be treated by arithmetic
1356 -- operations for instance without any additional corrections.
1359 if Time_Zone
/= 0 then
1360 Res_N
:= Res_N
- Time_Rep
(Time_Zone
) * 60 * Nano
;
1367 Current_Off
: constant Long_Integer :=
1368 Time_Zones_Operations
.UTC_Time_Offset
1370 Current_Res_N
: constant Time_Rep
:=
1371 Res_N
- Time_Rep
(Current_Off
) * Nano
;
1372 Off
: constant Long_Integer :=
1373 Time_Zones_Operations
.UTC_Time_Offset
1374 (Time
(Current_Res_N
));
1376 Res_N
:= Res_N
- Time_Rep
(Off
) * Nano
;
1380 -- Step 5: Leap seconds processing in GMT
1382 if Leap_Support
then
1383 Cumulative_Leap_Seconds
1384 (Start_Of_Time
, Res_N
, Elapsed_Leaps
, Next_Leap_N
);
1386 Res_N
:= Res_N
+ Time_Rep
(Elapsed_Leaps
) * Nano
;
1388 -- An Ada 2005 caller requesting an explicit leap second or an
1389 -- Ada 95 caller accounting for an invisible leap second.
1392 or else Res_N
>= Next_Leap_N
1394 Res_N
:= Res_N
+ Time_Rep
(1) * Nano
;
1397 -- Leap second validity check
1399 Rounded_Res_N
:= Res_N
- (Res_N
mod Nano
);
1403 and then Rounded_Res_N
/= Next_Leap_N
1409 return Time
(Res_N
);
1412 end Formatting_Operations
;
1414 ---------------------------
1415 -- Time_Zones_Operations --
1416 ---------------------------
1418 package body Time_Zones_Operations
is
1420 -- The Unix time bounds in nanoseconds: 1970/1/1 .. 2037/1/1
1422 Unix_Min
: constant Time_Rep
:= Ada_Low
+
1423 Time_Rep
(17 * 366 + 52 * 365) * Nanos_In_Day
;
1425 Unix_Max
: constant Time_Rep
:= Ada_Low
+
1426 Time_Rep
(34 * 366 + 102 * 365) * Nanos_In_Day
+
1427 Time_Rep
(Leap_Seconds_Count
) * Nano
;
1429 -- The following constants denote February 28 during non-leap
1430 -- centennial years, the units are nanoseconds.
1432 T_2100_2_28
: constant Time_Rep
:= Ada_Low
+
1433 (Time_Rep
(49 * 366 + 150 * 365 + 59) * Secs_In_Day
+
1434 Time_Rep
(Leap_Seconds_Count
)) * Nano
;
1436 T_2200_2_28
: constant Time_Rep
:= Ada_Low
+
1437 (Time_Rep
(73 * 366 + 226 * 365 + 59) * Secs_In_Day
+
1438 Time_Rep
(Leap_Seconds_Count
)) * Nano
;
1440 T_2300_2_28
: constant Time_Rep
:= Ada_Low
+
1441 (Time_Rep
(97 * 366 + 302 * 365 + 59) * Secs_In_Day
+
1442 Time_Rep
(Leap_Seconds_Count
)) * Nano
;
1444 -- 56 years (14 leap years + 42 non leap years) in nanoseconds:
1446 Nanos_In_56_Years
: constant := (14 * 366 + 42 * 365) * Nanos_In_Day
;
1448 subtype long
is Long_Integer;
1449 type long_Pointer
is access all long
;
1452 range -(2 ** (Standard
'Address_Size - Integer'(1))) ..
1453 +(2 ** (Standard'Address_Size - Integer'(1)) - 1);
1454 type time_t_Pointer
is access all time_t
;
1456 procedure localtime_tzoff
1457 (timer
: time_t_Pointer
;
1458 off
: long_Pointer
);
1459 pragma Import
(C
, localtime_tzoff
, "__gnat_localtime_tzoff");
1460 -- This is a lightweight wrapper around the system library function
1461 -- localtime_r. Parameter 'off' captures the UTC offset which is either
1462 -- retrieved from the tm struct or calculated from the 'timezone' extern
1463 -- and the tm_isdst flag in the tm struct.
1465 ---------------------
1466 -- UTC_Time_Offset --
1467 ---------------------
1469 function UTC_Time_Offset
(Date
: Time
) return Long_Integer is
1472 Offset
: aliased long
;
1473 Secs_T
: aliased time_t
;
1476 Date_N
:= Time_Rep
(Date
);
1478 -- Dates which are 56 years apart fall on the same day, day light
1479 -- saving and so on. Non-leap centennial years violate this rule by
1480 -- one day and as a consequence, special adjustment is needed.
1483 (if Date_N
<= T_2100_2_28
then 0
1484 elsif Date_N
<= T_2200_2_28
then 1
1485 elsif Date_N
<= T_2300_2_28
then 2
1488 if Adj_Cent
> 0 then
1489 Date_N
:= Date_N
- Time_Rep
(Adj_Cent
) * Nanos_In_Day
;
1492 -- Shift the date within bounds of Unix time
1494 while Date_N
< Unix_Min
loop
1495 Date_N
:= Date_N
+ Nanos_In_56_Years
;
1498 while Date_N
>= Unix_Max
loop
1499 Date_N
:= Date_N
- Nanos_In_56_Years
;
1502 -- Perform a shift in origins from Ada to Unix
1504 Date_N
:= Date_N
- Unix_Min
;
1506 -- Convert the date into seconds
1508 Secs_T
:= time_t
(Date_N
/ Nano
);
1511 (Secs_T
'Unchecked_Access,
1512 Offset
'Unchecked_Access);
1515 end UTC_Time_Offset
;
1517 end Time_Zones_Operations
;
1519 -- Start of elaboration code for Ada.Calendar
1522 System
.OS_Primitives
.Initialize
;