1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . C A L E N D A R --
9 -- Copyright (C) 1992-2012, 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
;
36 with System
.OS_Primitives
;
38 package body Ada
.Calendar
is
40 --------------------------
41 -- Implementation Notes --
42 --------------------------
44 -- In complex algorithms, some variables of type Ada.Calendar.Time carry
45 -- suffix _S or _N to denote units of seconds or nanoseconds.
47 -- Because time is measured in different units and from different origins
48 -- on various targets, a system independent model is incorporated into
49 -- Ada.Calendar. The idea behind the design is to encapsulate all target
50 -- dependent machinery in a single package, thus providing a uniform
51 -- interface to all existing and any potential children.
53 -- package Ada.Calendar
54 -- procedure Split (5 parameters) -------+
55 -- | Call from local routine
57 -- package Formatting_Operations |
58 -- procedure Split (11 parameters) <--+
59 -- end Formatting_Operations |
62 -- package Ada.Calendar.Formatting | Call from child routine
63 -- procedure Split (9 or 10 parameters) -+
64 -- end Ada.Calendar.Formatting
66 -- The behaviour of the interfacing routines is controlled via various
67 -- flags. All new Ada 2005 types from children of Ada.Calendar are
68 -- emulated by a similar type. For instance, type Day_Number is replaced
69 -- by Integer in various routines. One ramification of this model is that
70 -- the caller site must perform validity checks on returned results.
71 -- The end result of this model is the lack of target specific files per
72 -- child of Ada.Calendar (a-calfor, a-calfor-vms, a-calfor-vxwors, etc).
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 procedure Check_Within_Time_Bounds
(T
: Time_Rep
);
79 -- Ensure that a time representation value falls withing the bounds of Ada
80 -- time. Leap seconds support is taken into account.
82 procedure Cumulative_Leap_Seconds
83 (Start_Date
: Time_Rep
;
85 Elapsed_Leaps
: out Natural;
86 Next_Leap
: out Time_Rep
);
87 -- Elapsed_Leaps is the sum of the leap seconds that have occurred on or
88 -- after Start_Date and before (strictly before) End_Date. Next_Leap_Sec
89 -- represents the next leap second occurrence on or after End_Date. If
90 -- there are no leaps seconds after End_Date, End_Of_Time is returned.
91 -- End_Of_Time can be used as End_Date to count all the leap seconds that
92 -- have occurred on or after Start_Date.
94 -- Note: Any sub seconds of Start_Date and End_Date are discarded before
95 -- the calculations are done. For instance: if 113 seconds is a leap
96 -- second (it isn't) and 113.5 is input as an End_Date, the leap second
97 -- at 113 will not be counted in Leaps_Between, but it will be returned
98 -- as Next_Leap_Sec. Thus, if the caller wants to know if the End_Date is
99 -- a leap second, the comparison should be:
101 -- End_Date >= Next_Leap_Sec;
103 -- After_Last_Leap is designed so that this comparison works without
104 -- having to first check if Next_Leap_Sec is a valid leap second.
106 function Duration_To_Time_Rep
is
107 new Ada
.Unchecked_Conversion
(Duration, Time_Rep
);
108 -- Convert a duration value into a time representation value
110 function Time_Rep_To_Duration
is
111 new Ada
.Unchecked_Conversion
(Time_Rep
, Duration);
112 -- Convert a time representation value into a duration value
114 function UTC_Time_Offset
116 Is_Historic
: Boolean) return Long_Integer;
117 -- This routine acts as an Ada wrapper around __gnat_localtime_tzoff which
118 -- in turn utilizes various OS-dependent mechanisms to calculate the time
119 -- zone offset of a date. Formal parameter Date represents an arbitrary
120 -- time stamp, either in the past, now, or in the future. If the flag
121 -- Is_Historic is set, this routine would try to calculate to the best of
122 -- the OS's abilities the time zone offset that was or will be in effect
123 -- on Date. If the flag is set to False, the routine returns the current
124 -- time zone with Date effectively set to Clock.
126 -- NOTE: Targets which support localtime_r will aways return a historic
127 -- time zone even if flag Is_Historic is set to False because this is how
128 -- localtime_r operates.
134 -- An integer time duration. The type is used whenever a positive elapsed
135 -- duration is needed, for instance when splitting a time value. Here is
136 -- how Time_Rep and Time_Dur are related:
138 -- 'First Ada_Low Ada_High 'Last
139 -- Time_Rep: +-------+------------------------+---------+
140 -- Time_Dur: +------------------------+---------+
143 type Time_Dur
is range 0 .. 2 ** 63 - 1;
145 --------------------------
146 -- Leap seconds control --
147 --------------------------
150 pragma Import
(C
, Flag
, "__gl_leap_seconds_support");
151 -- This imported value is used to determine whether the compilation had
152 -- binder flag "-y" present which enables leap seconds. A value of zero
153 -- signifies no leap seconds support while a value of one enables support.
155 Leap_Support
: constant Boolean := (Flag
= 1);
156 -- Flag to controls the usage of leap seconds in all Ada.Calendar routines
158 Leap_Seconds_Count
: constant Natural := 25;
160 ---------------------
161 -- Local Constants --
162 ---------------------
164 Ada_Min_Year
: constant Year_Number
:= Year_Number
'First;
165 Secs_In_Four_Years
: constant := (3 * 365 + 366) * Secs_In_Day
;
166 Secs_In_Non_Leap_Year
: constant := 365 * Secs_In_Day
;
167 Nanos_In_Four_Years
: constant := Secs_In_Four_Years
* Nano
;
169 -- Lower and upper bound of Ada time. The zero (0) value of type Time is
170 -- positioned at year 2150. Note that the lower and upper bound account
171 -- for the non-leap centennial years.
173 Ada_Low
: constant Time_Rep
:= -(61 * 366 + 188 * 365) * Nanos_In_Day
;
174 Ada_High
: constant Time_Rep
:= (60 * 366 + 190 * 365) * Nanos_In_Day
;
176 -- Even though the upper bound of time is 2399-12-31 23:59:59.999999999
177 -- UTC, it must be increased to include all leap seconds.
179 Ada_High_And_Leaps
: constant Time_Rep
:=
180 Ada_High
+ Time_Rep
(Leap_Seconds_Count
) * Nano
;
182 -- Two constants used in the calculations of elapsed leap seconds.
183 -- End_Of_Time is later than Ada_High in time zone -28. Start_Of_Time
184 -- is earlier than Ada_Low in time zone +28.
186 End_Of_Time
: constant Time_Rep
:=
187 Ada_High
+ Time_Rep
(3) * Nanos_In_Day
;
188 Start_Of_Time
: constant Time_Rep
:=
189 Ada_Low
- Time_Rep
(3) * Nanos_In_Day
;
191 -- The Unix lower time bound expressed as nanoseconds since the start of
194 Unix_Min
: constant Time_Rep
:=
195 Ada_Low
+ Time_Rep
(17 * 366 + 52 * 365) * Nanos_In_Day
;
197 -- The Unix upper time bound expressed as nanoseconds since the start of
200 Unix_Max
: constant Time_Rep
:=
201 Ada_Low
+ Time_Rep
(34 * 366 + 102 * 365) * Nanos_In_Day
+
202 Time_Rep
(Leap_Seconds_Count
) * Nano
;
204 Epoch_Offset
: constant Time_Rep
:= (136 * 365 + 44 * 366) * Nanos_In_Day
;
205 -- The difference between 2150-1-1 UTC and 1970-1-1 UTC expressed in
206 -- nanoseconds. Note that year 2100 is non-leap.
208 Cumulative_Days_Before_Month
:
209 constant array (Month_Number
) of Natural :=
210 (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
212 -- The following table contains the hard time values of all existing leap
213 -- seconds. The values are produced by the utility program xleaps.adb. This
214 -- must be updated when additional leap second times are defined.
216 Leap_Second_Times
: constant array (1 .. Leap_Seconds_Count
) of Time_Rep
:=
217 (-5601484800000000000,
218 -5585587199000000000,
219 -5554051198000000000,
220 -5522515197000000000,
221 -5490979196000000000,
222 -5459356795000000000,
223 -5427820794000000000,
224 -5396284793000000000,
225 -5364748792000000000,
226 -5317487991000000000,
227 -5285951990000000000,
228 -5254415989000000000,
229 -5191257588000000000,
230 -5112287987000000000,
231 -5049129586000000000,
232 -5017593585000000000,
233 -4970332784000000000,
234 -4938796783000000000,
235 -4907260782000000000,
236 -4859827181000000000,
237 -4812566380000000000,
238 -4765132779000000000,
239 -4544207978000000000,
240 -4449513577000000000,
241 -4339180776000000000);
247 function "+" (Left
: Time
; Right
: Duration) return Time
is
248 pragma Unsuppress
(Overflow_Check
);
249 Left_N
: constant Time_Rep
:= Time_Rep
(Left
);
251 return Time
(Left_N
+ Duration_To_Time_Rep
(Right
));
253 when Constraint_Error
=>
257 function "+" (Left
: Duration; Right
: Time
) return Time
is
266 function "-" (Left
: Time
; Right
: Duration) return Time
is
267 pragma Unsuppress
(Overflow_Check
);
268 Left_N
: constant Time_Rep
:= Time_Rep
(Left
);
270 return Time
(Left_N
- Duration_To_Time_Rep
(Right
));
272 when Constraint_Error
=>
276 function "-" (Left
: Time
; Right
: Time
) return Duration is
277 pragma Unsuppress
(Overflow_Check
);
279 Dur_Low
: constant Time_Rep
:= Duration_To_Time_Rep
(Duration'First);
280 Dur_High
: constant Time_Rep
:= Duration_To_Time_Rep
(Duration'Last);
281 -- The bounds of type Duration expressed as time representations
286 Res_N
:= Time_Rep
(Left
) - Time_Rep
(Right
);
288 -- Due to the extended range of Ada time, "-" is capable of producing
289 -- results which may exceed the range of Duration. In order to prevent
290 -- the generation of bogus values by the Unchecked_Conversion, we apply
291 -- the following check.
293 if Res_N
< Dur_Low
or else Res_N
> Dur_High
then
297 return Time_Rep_To_Duration
(Res_N
);
300 when Constraint_Error
=>
308 function "<" (Left
, Right
: Time
) return Boolean is
310 return Time_Rep
(Left
) < Time_Rep
(Right
);
317 function "<=" (Left
, Right
: Time
) return Boolean is
319 return Time_Rep
(Left
) <= Time_Rep
(Right
);
326 function ">" (Left
, Right
: Time
) return Boolean is
328 return Time_Rep
(Left
) > Time_Rep
(Right
);
335 function ">=" (Left
, Right
: Time
) return Boolean is
337 return Time_Rep
(Left
) >= Time_Rep
(Right
);
340 ------------------------------
341 -- Check_Within_Time_Bounds --
342 ------------------------------
344 procedure Check_Within_Time_Bounds
(T
: Time_Rep
) is
347 if T
< Ada_Low
or else T
> Ada_High_And_Leaps
then
351 if T
< Ada_Low
or else T
> Ada_High
then
355 end Check_Within_Time_Bounds
;
361 function Clock
return Time
is
362 Elapsed_Leaps
: Natural;
363 Next_Leap_N
: Time_Rep
;
365 -- The system clock returns the time in UTC since the Unix Epoch of
366 -- 1970-01-01 00:00:00.0. We perform an origin shift to the Ada Epoch
367 -- by adding the number of nanoseconds between the two origins.
370 Duration_To_Time_Rep
(System
.OS_Primitives
.Clock
) + Unix_Min
;
373 -- If the target supports leap seconds, determine the number of leap
374 -- seconds elapsed until this moment.
377 Cumulative_Leap_Seconds
378 (Start_Of_Time
, Res_N
, Elapsed_Leaps
, Next_Leap_N
);
380 -- The system clock may fall exactly on a leap second
382 if Res_N
>= Next_Leap_N
then
383 Elapsed_Leaps
:= Elapsed_Leaps
+ 1;
386 -- The target does not support leap seconds
392 Res_N
:= Res_N
+ Time_Rep
(Elapsed_Leaps
) * Nano
;
397 -----------------------------
398 -- Cumulative_Leap_Seconds --
399 -----------------------------
401 procedure Cumulative_Leap_Seconds
402 (Start_Date
: Time_Rep
;
404 Elapsed_Leaps
: out Natural;
405 Next_Leap
: out Time_Rep
)
407 End_Index
: Positive;
408 End_T
: Time_Rep
:= End_Date
;
409 Start_Index
: Positive;
410 Start_T
: Time_Rep
:= Start_Date
;
413 -- Both input dates must be normalized to UTC
415 pragma Assert
(Leap_Support
and then End_Date
>= Start_Date
);
417 Next_Leap
:= End_Of_Time
;
419 -- Make sure that the end date does not exceed the upper bound
422 if End_Date
> Ada_High
then
426 -- Remove the sub seconds from both dates
428 Start_T
:= Start_T
- (Start_T
mod Nano
);
429 End_T
:= End_T
- (End_T
mod Nano
);
431 -- Some trivial cases:
432 -- Leap 1 . . . Leap N
433 -- ---+========+------+############+-------+========+-----
434 -- Start_T End_T Start_T End_T
436 if End_T
< Leap_Second_Times
(1) then
438 Next_Leap
:= Leap_Second_Times
(1);
441 elsif Start_T
> Leap_Second_Times
(Leap_Seconds_Count
) then
443 Next_Leap
:= End_Of_Time
;
447 -- Perform the calculations only if the start date is within the leap
448 -- second occurrences table.
450 if Start_T
<= Leap_Second_Times
(Leap_Seconds_Count
) then
453 -- +----+----+-- . . . --+-------+---+
454 -- | T1 | T2 | | N - 1 | N |
455 -- +----+----+-- . . . --+-------+---+
457 -- | Start_Index | End_Index
458 -- +-------------------+
461 -- The idea behind the algorithm is to iterate and find two
462 -- closest dates which are after Start_T and End_T. Their
463 -- corresponding index difference denotes the number of leap
468 exit when Leap_Second_Times
(Start_Index
) >= Start_T
;
469 Start_Index
:= Start_Index
+ 1;
472 End_Index
:= Start_Index
;
474 exit when End_Index
> Leap_Seconds_Count
475 or else Leap_Second_Times
(End_Index
) >= End_T
;
476 End_Index
:= End_Index
+ 1;
479 if End_Index
<= Leap_Seconds_Count
then
480 Next_Leap
:= Leap_Second_Times
(End_Index
);
483 Elapsed_Leaps
:= End_Index
- Start_Index
;
488 end Cumulative_Leap_Seconds
;
494 function Day
(Date
: Time
) return Day_Number
is
499 pragma Unreferenced
(Y
, M
, S
);
501 Split
(Date
, Y
, M
, D
, S
);
509 function Is_Leap
(Year
: Year_Number
) return Boolean is
511 -- Leap centennial years
513 if Year
mod 400 = 0 then
516 -- Non-leap centennial years
518 elsif Year
mod 100 = 0 then
524 return Year
mod 4 = 0;
532 function Month
(Date
: Time
) return Month_Number
is
537 pragma Unreferenced
(Y
, D
, S
);
539 Split
(Date
, Y
, M
, D
, S
);
547 function Seconds
(Date
: Time
) return Day_Duration
is
552 pragma Unreferenced
(Y
, M
, D
);
554 Split
(Date
, Y
, M
, D
, S
);
564 Year
: out Year_Number
;
565 Month
: out Month_Number
;
566 Day
: out Day_Number
;
567 Seconds
: out Day_Duration
)
575 pragma Unreferenced
(H
, M
, Se
, Ss
, Le
);
578 -- Even though the input time zone is UTC (0), the flag Use_TZ will
579 -- ensure that Split picks up the local time zone.
581 Formatting_Operations
.Split
598 if not Year
'Valid or else
599 not Month
'Valid or else
600 not Day
'Valid or else
613 Month
: Month_Number
;
615 Seconds
: Day_Duration
:= 0.0) return Time
617 -- The values in the following constants are irrelevant, they are just
618 -- placeholders; the choice of constructing a Day_Duration value is
619 -- controlled by the Use_Day_Secs flag.
621 H
: constant Integer := 1;
622 M
: constant Integer := 1;
623 Se
: constant Integer := 1;
624 Ss
: constant Duration := 0.1;
629 if not Year
'Valid or else
630 not Month
'Valid or else
631 not Day
'Valid or else
637 -- Even though the input time zone is UTC (0), the flag Use_TZ will
638 -- ensure that Split picks up the local time zone.
641 Formatting_Operations
.Time_Of
651 Use_Day_Secs
=> True,
657 ---------------------
658 -- UTC_Time_Offset --
659 ---------------------
661 function UTC_Time_Offset
663 Is_Historic
: Boolean) return Long_Integer
665 -- The following constants denote February 28 during non-leap centennial
666 -- years, the units are nanoseconds.
668 T_2100_2_28
: constant Time_Rep
:= Ada_Low
+
669 (Time_Rep
(49 * 366 + 150 * 365 + 59) * Secs_In_Day
+
670 Time_Rep
(Leap_Seconds_Count
)) * Nano
;
672 T_2200_2_28
: constant Time_Rep
:= Ada_Low
+
673 (Time_Rep
(73 * 366 + 226 * 365 + 59) * Secs_In_Day
+
674 Time_Rep
(Leap_Seconds_Count
)) * Nano
;
676 T_2300_2_28
: constant Time_Rep
:= Ada_Low
+
677 (Time_Rep
(97 * 366 + 302 * 365 + 59) * Secs_In_Day
+
678 Time_Rep
(Leap_Seconds_Count
)) * Nano
;
680 -- 56 years (14 leap years + 42 non-leap years) in nanoseconds:
682 Nanos_In_56_Years
: constant := (14 * 366 + 42 * 365) * Nanos_In_Day
;
684 type int_Pointer
is access all Interfaces
.C
.int
;
685 type long_Pointer
is access all Interfaces
.C
.long
;
688 range -(2 ** (Standard
'Address_Size - Integer'(1))) ..
689 +(2 ** (Standard'Address_Size - Integer'(1)) - 1);
690 type time_t_Pointer
is access all time_t
;
692 procedure localtime_tzoff
693 (timer
: time_t_Pointer
;
694 is_historic
: int_Pointer
;
696 pragma Import
(C
, localtime_tzoff
, "__gnat_localtime_tzoff");
697 -- This routine is a interfacing wrapper around the library function
698 -- __gnat_localtime_tzoff. Parameter 'timer' represents a Unix-based
699 -- time equivalent of the input date. If flag 'is_historic' is set, this
700 -- routine would try to calculate to the best of the OS's abilities the
701 -- time zone offset that was or will be in effect on 'timer'. If the
702 -- flag is set to False, the routine returns the current time zone
703 -- regardless of what 'timer' designates. Parameter 'off' captures the
704 -- UTC offset of 'timer'.
708 Flag
: aliased Interfaces
.C
.int
;
709 Offset
: aliased Interfaces
.C
.long
;
710 Secs_T
: aliased time_t
;
712 -- Start of processing for UTC_Time_Offset
715 Date_N
:= Time_Rep
(Date
);
717 -- Dates which are 56 years apart fall on the same day, day light saving
718 -- and so on. Non-leap centennial years violate this rule by one day and
719 -- as a consequence, special adjustment is needed.
722 (if Date_N
<= T_2100_2_28
then 0
723 elsif Date_N
<= T_2200_2_28
then 1
724 elsif Date_N
<= T_2300_2_28
then 2
728 Date_N
:= Date_N
- Time_Rep
(Adj_Cent
) * Nanos_In_Day
;
731 -- Shift the date within bounds of Unix time
733 while Date_N
< Unix_Min
loop
734 Date_N
:= Date_N
+ Nanos_In_56_Years
;
737 while Date_N
>= Unix_Max
loop
738 Date_N
:= Date_N
- Nanos_In_56_Years
;
741 -- Perform a shift in origins from Ada to Unix
743 Date_N
:= Date_N
- Unix_Min
;
745 -- Convert the date into seconds
747 Secs_T
:= time_t
(Date_N
/ Nano
);
749 -- Determine whether to treat the input date as historical or not. A
750 -- value of "0" signifies that the date is NOT historic.
752 Flag
:= (if Is_Historic
then 1 else 0);
755 (Secs_T
'Unchecked_Access,
756 Flag
'Unchecked_Access,
757 Offset
'Unchecked_Access);
759 return Long_Integer (Offset
);
766 function Year
(Date
: Time
) return Year_Number
is
771 pragma Unreferenced
(M
, D
, S
);
773 Split
(Date
, Y
, M
, D
, S
);
777 -- The following packages assume that Time is a signed 64 bit integer
778 -- type, the units are nanoseconds and the origin is the start of Ada
779 -- time (1901-01-01 00:00:00.0 UTC).
781 ---------------------------
782 -- Arithmetic_Operations --
783 ---------------------------
785 package body Arithmetic_Operations
is
791 function Add
(Date
: Time
; Days
: Long_Integer) return Time
is
792 pragma Unsuppress
(Overflow_Check
);
793 Date_N
: constant Time_Rep
:= Time_Rep
(Date
);
795 return Time
(Date_N
+ Time_Rep
(Days
) * Nanos_In_Day
);
797 when Constraint_Error
=>
808 Days
: out Long_Integer;
809 Seconds
: out Duration;
810 Leap_Seconds
: out Integer)
814 Elapsed_Leaps
: Natural;
816 Negate
: Boolean := False;
817 Next_Leap_N
: Time_Rep
;
819 Sub_Secs_Diff
: Time_Rep
;
822 -- Both input time values are assumed to be in UTC
824 if Left
>= Right
then
825 Later
:= Time_Rep
(Left
);
826 Earlier
:= Time_Rep
(Right
);
828 Later
:= Time_Rep
(Right
);
829 Earlier
:= Time_Rep
(Left
);
833 -- If the target supports leap seconds, process them
836 Cumulative_Leap_Seconds
837 (Earlier
, Later
, Elapsed_Leaps
, Next_Leap_N
);
839 if Later
>= Next_Leap_N
then
840 Elapsed_Leaps
:= Elapsed_Leaps
+ 1;
843 -- The target does not support leap seconds
849 -- Sub seconds processing. We add the resulting difference to one
850 -- of the input dates in order to account for any potential rounding
851 -- of the difference in the next step.
853 Sub_Secs_Diff
:= Later
mod Nano
- Earlier
mod Nano
;
854 Earlier
:= Earlier
+ Sub_Secs_Diff
;
855 Sub_Secs
:= Duration (Sub_Secs_Diff
) / Nano_F
;
857 -- Difference processing. This operation should be able to calculate
858 -- the difference between opposite values which are close to the end
859 -- and start of Ada time. To accommodate the large range, we convert
860 -- to seconds. This action may potentially round the two values and
861 -- either add or drop a second. We compensate for this issue in the
865 Time_Dur
(Later
/ Nano
- Earlier
/ Nano
) - Time_Dur
(Elapsed_Leaps
);
867 Days
:= Long_Integer (Res_Dur
/ Secs_In_Day
);
868 Seconds
:= Duration (Res_Dur
mod Secs_In_Day
) + Sub_Secs
;
869 Leap_Seconds
:= Integer (Elapsed_Leaps
);
875 if Leap_Seconds
/= 0 then
876 Leap_Seconds
:= -Leap_Seconds
;
885 function Subtract
(Date
: Time
; Days
: Long_Integer) return Time
is
886 pragma Unsuppress
(Overflow_Check
);
887 Date_N
: constant Time_Rep
:= Time_Rep
(Date
);
889 return Time
(Date_N
- Time_Rep
(Days
) * Nanos_In_Day
);
891 when Constraint_Error
=>
895 end Arithmetic_Operations
;
897 ---------------------------
898 -- Conversion_Operations --
899 ---------------------------
901 package body Conversion_Operations
is
907 function To_Ada_Time
(Unix_Time
: Long_Integer) return Time
is
908 pragma Unsuppress
(Overflow_Check
);
909 Unix_Rep
: constant Time_Rep
:= Time_Rep
(Unix_Time
) * Nano
;
911 return Time
(Unix_Rep
- Epoch_Offset
);
913 when Constraint_Error
=>
928 tm_isdst
: Integer) return Time
930 pragma Unsuppress
(Overflow_Check
);
932 Month
: Month_Number
;
941 Year
:= Year_Number
(1900 + tm_year
);
942 Month
:= Month_Number
(1 + tm_mon
);
943 Day
:= Day_Number
(tm_day
);
945 -- Step 1: Validity checks of input values
947 if not Year
'Valid or else not Month
'Valid or else not Day
'Valid
948 or else tm_hour
not in 0 .. 24
949 or else tm_min
not in 0 .. 59
950 or else tm_sec
not in 0 .. 60
951 or else tm_isdst
not in -1 .. 1
956 -- Step 2: Potential leap second
966 -- Step 3: Calculate the time value
970 (Formatting_Operations
.Time_Of
974 Day_Secs
=> 0.0, -- Time is given in h:m:s
978 Sub_Sec
=> 0.0, -- No precise sub second given
980 Use_Day_Secs
=> False, -- Time is given in h:m:s
981 Use_TZ
=> True, -- Force usage of explicit time zone
983 Time_Zone
=> 0)); -- Place the value in UTC
985 -- Step 4: Daylight Savings Time
988 Result
:= Result
+ Time_Rep
(3_600
) * Nano
;
991 return Time
(Result
);
994 when Constraint_Error
=>
1002 function To_Duration
1003 (tv_sec
: Long_Integer;
1004 tv_nsec
: Long_Integer) return Duration
1006 pragma Unsuppress
(Overflow_Check
);
1008 return Duration (tv_sec
) + Duration (tv_nsec
) / Nano_F
;
1011 ------------------------
1012 -- To_Struct_Timespec --
1013 ------------------------
1015 procedure To_Struct_Timespec
1017 tv_sec
: out Long_Integer;
1018 tv_nsec
: out Long_Integer)
1020 pragma Unsuppress
(Overflow_Check
);
1022 Nano_Secs
: Duration;
1025 -- Seconds extraction, avoid potential rounding errors
1028 tv_sec
:= Long_Integer (Secs
);
1030 -- Nanoseconds extraction
1032 Nano_Secs
:= D
- Duration (tv_sec
);
1033 tv_nsec
:= Long_Integer (Nano_Secs
* Nano
);
1034 end To_Struct_Timespec
;
1040 procedure To_Struct_Tm
1042 tm_year
: out Integer;
1043 tm_mon
: out Integer;
1044 tm_day
: out Integer;
1045 tm_hour
: out Integer;
1046 tm_min
: out Integer;
1047 tm_sec
: out Integer)
1049 pragma Unsuppress
(Overflow_Check
);
1051 Month
: Month_Number
;
1053 Day_Secs
: Day_Duration
;
1058 -- Step 1: Split the input time
1060 Formatting_Operations
.Split
1065 Day_Secs
=> Day_Secs
,
1070 Leap_Sec
=> Leap_Sec
,
1072 Is_Historic
=> False,
1075 -- Step 2: Correct the year and month
1077 tm_year
:= Year
- 1900;
1078 tm_mon
:= Month
- 1;
1080 -- Step 3: Handle leap second occurrences
1082 tm_sec
:= (if Leap_Sec
then 60 else Second
);
1089 function To_Unix_Time
(Ada_Time
: Time
) return Long_Integer is
1090 pragma Unsuppress
(Overflow_Check
);
1091 Ada_Rep
: constant Time_Rep
:= Time_Rep
(Ada_Time
);
1093 return Long_Integer ((Ada_Rep
+ Epoch_Offset
) / Nano
);
1095 when Constraint_Error
=>
1098 end Conversion_Operations
;
1100 ----------------------
1101 -- Delay_Operations --
1102 ----------------------
1104 package body Delay_Operations
is
1110 function To_Duration
(Date
: Time
) return Duration is
1111 pragma Unsuppress
(Overflow_Check
);
1113 Safe_Ada_High
: constant Time_Rep
:= Ada_High
- Epoch_Offset
;
1114 -- This value represents a "safe" end of time. In order to perform a
1115 -- proper conversion to Unix duration, we will have to shift origins
1116 -- at one point. For very distant dates, this means an overflow check
1117 -- failure. To prevent this, the function returns the "safe" end of
1118 -- time (roughly 2219) which is still distant enough.
1120 Elapsed_Leaps
: Natural;
1121 Next_Leap_N
: Time_Rep
;
1125 Res_N
:= Time_Rep
(Date
);
1127 -- Step 1: If the target supports leap seconds, remove any leap
1128 -- seconds elapsed up to the input date.
1130 if Leap_Support
then
1131 Cumulative_Leap_Seconds
1132 (Start_Of_Time
, Res_N
, Elapsed_Leaps
, Next_Leap_N
);
1134 -- The input time value may fall on a leap second occurrence
1136 if Res_N
>= Next_Leap_N
then
1137 Elapsed_Leaps
:= Elapsed_Leaps
+ 1;
1140 -- The target does not support leap seconds
1146 Res_N
:= Res_N
- Time_Rep
(Elapsed_Leaps
) * Nano
;
1148 -- Step 2: Perform a shift in origins to obtain a Unix equivalent of
1149 -- the input. Guard against very large delay values such as the end
1150 -- of time since the computation will overflow.
1152 Res_N
:= (if Res_N
> Safe_Ada_High
then Safe_Ada_High
1153 else Res_N
+ Epoch_Offset
);
1155 return Time_Rep_To_Duration
(Res_N
);
1158 end Delay_Operations
;
1160 ---------------------------
1161 -- Formatting_Operations --
1162 ---------------------------
1164 package body Formatting_Operations
is
1170 function Day_Of_Week
(Date
: Time
) return Integer is
1171 Date_N
: constant Time_Rep
:= Time_Rep
(Date
);
1172 Time_Zone
: constant Long_Integer := UTC_Time_Offset
(Date
, True);
1173 Ada_Low_N
: Time_Rep
;
1174 Day_Count
: Long_Integer;
1180 -- As declared, the Ada Epoch is set in UTC. For this calculation to
1181 -- work properly, both the Epoch and the input date must be in the
1182 -- same time zone. The following places the Epoch in the input date's
1185 Ada_Low_N
:= Ada_Low
- Time_Rep
(Time_Zone
) * Nano
;
1187 if Date_N
> Ada_Low_N
then
1191 High_N
:= Ada_Low_N
;
1195 -- Determine the elapsed seconds since the start of Ada time
1197 Day_Dur
:= Time_Dur
(High_N
/ Nano
- Low_N
/ Nano
);
1199 -- Count the number of days since the start of Ada time. 1901-01-01
1200 -- GMT was a Tuesday.
1202 Day_Count
:= Long_Integer (Day_Dur
/ Secs_In_Day
) + 1;
1204 return Integer (Day_Count
mod 7);
1213 Year
: out Year_Number
;
1214 Month
: out Month_Number
;
1215 Day
: out Day_Number
;
1216 Day_Secs
: out Day_Duration
;
1218 Minute
: out Integer;
1219 Second
: out Integer;
1220 Sub_Sec
: out Duration;
1221 Leap_Sec
: out Boolean;
1223 Is_Historic
: Boolean;
1224 Time_Zone
: Long_Integer)
1226 -- The following constants represent the number of nanoseconds
1227 -- elapsed since the start of Ada time to and including the non
1228 -- leap centennial years.
1230 Year_2101
: constant Time_Rep
:= Ada_Low
+
1231 Time_Rep
(49 * 366 + 151 * 365) * Nanos_In_Day
;
1232 Year_2201
: constant Time_Rep
:= Ada_Low
+
1233 Time_Rep
(73 * 366 + 227 * 365) * Nanos_In_Day
;
1234 Year_2301
: constant Time_Rep
:= Ada_Low
+
1235 Time_Rep
(97 * 366 + 303 * 365) * Nanos_In_Day
;
1237 Date_Dur
: Time_Dur
;
1239 Day_Seconds
: Natural;
1240 Elapsed_Leaps
: Natural;
1241 Four_Year_Segs
: Natural;
1242 Hour_Seconds
: Natural;
1243 Is_Leap_Year
: Boolean;
1244 Next_Leap_N
: Time_Rep
;
1245 Rem_Years
: Natural;
1246 Sub_Sec_N
: Time_Rep
;
1250 Date_N
:= Time_Rep
(Date
);
1252 -- Step 1: Leap seconds processing in UTC
1254 if Leap_Support
then
1255 Cumulative_Leap_Seconds
1256 (Start_Of_Time
, Date_N
, Elapsed_Leaps
, Next_Leap_N
);
1258 Leap_Sec
:= Date_N
>= Next_Leap_N
;
1261 Elapsed_Leaps
:= Elapsed_Leaps
+ 1;
1264 -- The target does not support leap seconds
1271 Date_N
:= Date_N
- Time_Rep
(Elapsed_Leaps
) * Nano
;
1273 -- Step 2: Time zone processing. This action converts the input date
1274 -- from GMT to the requested time zone. Applies from Ada 2005 on.
1277 if Time_Zone
/= 0 then
1278 Date_N
:= Date_N
+ Time_Rep
(Time_Zone
) * 60 * Nano
;
1285 Off
: constant Long_Integer :=
1286 UTC_Time_Offset
(Time
(Date_N
), Is_Historic
);
1289 Date_N
:= Date_N
+ Time_Rep
(Off
) * Nano
;
1293 -- Step 3: Non-leap centennial year adjustment in local time zone
1295 -- In order for all divisions to work properly and to avoid more
1296 -- complicated arithmetic, we add fake February 29s to dates which
1297 -- occur after a non-leap centennial year.
1299 if Date_N
>= Year_2301
then
1300 Date_N
:= Date_N
+ Time_Rep
(3) * Nanos_In_Day
;
1302 elsif Date_N
>= Year_2201
then
1303 Date_N
:= Date_N
+ Time_Rep
(2) * Nanos_In_Day
;
1305 elsif Date_N
>= Year_2101
then
1306 Date_N
:= Date_N
+ Time_Rep
(1) * Nanos_In_Day
;
1309 -- Step 4: Sub second processing in local time zone
1311 Sub_Sec_N
:= Date_N
mod Nano
;
1312 Sub_Sec
:= Duration (Sub_Sec_N
) / Nano_F
;
1313 Date_N
:= Date_N
- Sub_Sec_N
;
1315 -- Convert Date_N into a time duration value, changing the units
1318 Date_Dur
:= Time_Dur
(Date_N
/ Nano
- Ada_Low
/ Nano
);
1320 -- Step 5: Year processing in local time zone. Determine the number
1321 -- of four year segments since the start of Ada time and the input
1324 Four_Year_Segs
:= Natural (Date_Dur
/ Secs_In_Four_Years
);
1326 if Four_Year_Segs
> 0 then
1327 Date_Dur
:= Date_Dur
- Time_Dur
(Four_Year_Segs
) *
1331 -- Calculate the remaining non-leap years
1333 Rem_Years
:= Natural (Date_Dur
/ Secs_In_Non_Leap_Year
);
1335 if Rem_Years
> 3 then
1339 Date_Dur
:= Date_Dur
- Time_Dur
(Rem_Years
) * Secs_In_Non_Leap_Year
;
1341 Year
:= Ada_Min_Year
+ Natural (4 * Four_Year_Segs
+ Rem_Years
);
1342 Is_Leap_Year
:= Is_Leap
(Year
);
1344 -- Step 6: Month and day processing in local time zone
1346 Year_Day
:= Natural (Date_Dur
/ Secs_In_Day
) + 1;
1350 -- Processing for months after January
1352 if Year_Day
> 31 then
1354 Year_Day
:= Year_Day
- 31;
1356 -- Processing for a new month or a leap February
1359 and then (not Is_Leap_Year
or else Year_Day
> 29)
1362 Year_Day
:= Year_Day
- 28;
1364 if Is_Leap_Year
then
1365 Year_Day
:= Year_Day
- 1;
1370 while Year_Day
> Days_In_Month
(Month
) loop
1371 Year_Day
:= Year_Day
- Days_In_Month
(Month
);
1377 -- Step 7: Hour, minute, second and sub second processing in local
1380 Day
:= Day_Number
(Year_Day
);
1381 Day_Seconds
:= Integer (Date_Dur
mod Secs_In_Day
);
1382 Day_Secs
:= Duration (Day_Seconds
) + Sub_Sec
;
1383 Hour
:= Day_Seconds
/ 3_600
;
1384 Hour_Seconds
:= Day_Seconds
mod 3_600
;
1385 Minute
:= Hour_Seconds
/ 60;
1386 Second
:= Hour_Seconds
mod 60;
1394 (Year
: Year_Number
;
1395 Month
: Month_Number
;
1397 Day_Secs
: Day_Duration
;
1403 Use_Day_Secs
: Boolean;
1405 Is_Historic
: Boolean;
1406 Time_Zone
: Long_Integer) return Time
1409 Elapsed_Leaps
: Natural;
1410 Next_Leap_N
: Time_Rep
;
1412 Rounded_Res_N
: Time_Rep
;
1415 -- Step 1: Check whether the day, month and year form a valid date
1417 if Day
> Days_In_Month
(Month
)
1418 and then (Day
/= 29 or else Month
/= 2 or else not Is_Leap
(Year
))
1423 -- Start accumulating nanoseconds from the low bound of Ada time
1427 -- Step 2: Year processing and centennial year adjustment. Determine
1428 -- the number of four year segments since the start of Ada time and
1431 Count
:= (Year
- Year_Number
'First) / 4;
1433 for Four_Year_Segments
in 1 .. Count
loop
1434 Res_N
:= Res_N
+ Nanos_In_Four_Years
;
1437 -- Note that non-leap centennial years are automatically considered
1438 -- leap in the operation above. An adjustment of several days is
1439 -- required to compensate for this.
1442 Res_N
:= Res_N
- Time_Rep
(3) * Nanos_In_Day
;
1444 elsif Year
> 2200 then
1445 Res_N
:= Res_N
- Time_Rep
(2) * Nanos_In_Day
;
1447 elsif Year
> 2100 then
1448 Res_N
:= Res_N
- Time_Rep
(1) * Nanos_In_Day
;
1451 -- Add the remaining non-leap years
1453 Count
:= (Year
- Year_Number
'First) mod 4;
1454 Res_N
:= Res_N
+ Time_Rep
(Count
) * Secs_In_Non_Leap_Year
* Nano
;
1456 -- Step 3: Day of month processing. Determine the number of days
1457 -- since the start of the current year. Do not add the current
1458 -- day since it has not elapsed yet.
1460 Count
:= Cumulative_Days_Before_Month
(Month
) + Day
- 1;
1462 -- The input year is leap and we have passed February
1470 Res_N
:= Res_N
+ Time_Rep
(Count
) * Nanos_In_Day
;
1472 -- Step 4: Hour, minute, second and sub second processing
1474 if Use_Day_Secs
then
1475 Res_N
:= Res_N
+ Duration_To_Time_Rep
(Day_Secs
);
1479 Res_N
+ Time_Rep
(Hour
* 3_600
+ Minute
* 60 + Second
) * Nano
;
1481 if Sub_Sec
= 1.0 then
1482 Res_N
:= Res_N
+ Time_Rep
(1) * Nano
;
1484 Res_N
:= Res_N
+ Duration_To_Time_Rep
(Sub_Sec
);
1488 -- At this point, the generated time value should be withing the
1489 -- bounds of Ada time.
1491 Check_Within_Time_Bounds
(Res_N
);
1493 -- Step 4: Time zone processing. At this point we have built an
1494 -- arbitrary time value which is not related to any time zone.
1495 -- For simplicity, the time value is normalized to GMT, producing
1496 -- a uniform representation which can be treated by arithmetic
1497 -- operations for instance without any additional corrections.
1500 if Time_Zone
/= 0 then
1501 Res_N
:= Res_N
- Time_Rep
(Time_Zone
) * 60 * Nano
;
1508 Cur_Off
: constant Long_Integer :=
1509 UTC_Time_Offset
(Time
(Res_N
), Is_Historic
);
1510 Cur_Res_N
: constant Time_Rep
:=
1511 Res_N
- Time_Rep
(Cur_Off
) * Nano
;
1512 Off
: constant Long_Integer :=
1513 UTC_Time_Offset
(Time
(Cur_Res_N
), Is_Historic
);
1516 Res_N
:= Res_N
- Time_Rep
(Off
) * Nano
;
1520 -- Step 5: Leap seconds processing in GMT
1522 if Leap_Support
then
1523 Cumulative_Leap_Seconds
1524 (Start_Of_Time
, Res_N
, Elapsed_Leaps
, Next_Leap_N
);
1526 Res_N
:= Res_N
+ Time_Rep
(Elapsed_Leaps
) * Nano
;
1528 -- An Ada 2005 caller requesting an explicit leap second or an
1529 -- Ada 95 caller accounting for an invisible leap second.
1531 if Leap_Sec
or else Res_N
>= Next_Leap_N
then
1532 Res_N
:= Res_N
+ Time_Rep
(1) * Nano
;
1535 -- Leap second validity check
1537 Rounded_Res_N
:= Res_N
- (Res_N
mod Nano
);
1541 and then Rounded_Res_N
/= Next_Leap_N
1547 return Time
(Res_N
);
1550 end Formatting_Operations
;
1552 ---------------------------
1553 -- Time_Zones_Operations --
1554 ---------------------------
1556 package body Time_Zones_Operations
is
1558 ---------------------
1559 -- UTC_Time_Offset --
1560 ---------------------
1562 function UTC_Time_Offset
(Date
: Time
) return Long_Integer is
1564 return UTC_Time_Offset
(Date
, True);
1565 end UTC_Time_Offset
;
1567 end Time_Zones_Operations
;
1569 -- Start of elaboration code for Ada.Calendar
1572 System
.OS_Primitives
.Initialize
;