1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . C A L E N D A R --
9 -- Copyright (C) 1992-2007, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This is the Alpha/VMS version
36 with System.Aux_DEC; use System.Aux_DEC;
38 with Ada.Unchecked_Conversion;
40 pragma Warnings (Off); -- temp till we fix out param warnings ???
42 package body Ada.Calendar is
44 --------------------------
45 -- Implementation Notes --
46 --------------------------
48 -- Variables of type Ada.Calendar.Time have suffix _S or _M to denote
49 -- units of seconds or milis.
51 -- Because time is measured in different units and from different origins
52 -- on various targets, a system independent model is incorporated into
53 -- Ada.Calendar. The idea behing the design is to encapsulate all target
54 -- dependent machinery in a single package, thus providing a uniform
55 -- interface to all existing and any potential children.
57 -- package Ada.Calendar
58 -- procedure Split (5 parameters) -------+
59 -- | Call from local routine
61 -- package Formatting_Operations |
62 -- procedure Split (11 parameters) <--+
63 -- end Formatting_Operations |
66 -- package Ada.Calendar.Formatting | Call from child routine
67 -- procedure Split (9 or 10 parameters) -+
68 -- end Ada.Calendar.Formatting
70 -- The behaviour of the interfacing routines is controlled via various
71 -- flags. All new Ada 2005 types from children of Ada.Calendar are
72 -- emulated by a similar type. For instance, type Day_Number is replaced
73 -- by Integer in various routines. One ramification of this model is that
74 -- the caller site must perform validity checks on returned results.
75 -- The end result of this model is the lack of target specific files per
76 -- child of Ada.Calendar (a-calfor, a-calfor-vms, a-calfor-vxwors, etc).
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Check_Within_Time_Bounds (T : Time);
83 -- Ensure that a time representation value falls withing the bounds of Ada
84 -- time. Leap seconds support is taken into account.
86 procedure Cumulative_Leap_Seconds
89 Elapsed_Leaps : out Natural;
90 Next_Leap_Sec : out Time);
91 -- Elapsed_Leaps is the sum of the leap seconds that have occured on or
92 -- after Start_Date and before (strictly before) End_Date. Next_Leap_Sec
93 -- represents the next leap second occurence on or after End_Date. If
94 -- there are no leaps seconds after End_Date, End_Of_Time is returned.
95 -- End_Of_Time can be used as End_Date to count all the leap seconds that
96 -- have occured on or after Start_Date.
98 -- Note: Any sub seconds of Start_Date and End_Date are discarded before
99 -- the calculations are done. For instance: if 113 seconds is a leap
100 -- second (it isn't) and 113.5 is input as an End_Date, the leap second
101 -- at 113 will not be counted in Leaps_Between, but it will be returned
102 -- as Next_Leap_Sec. Thus, if the caller wants to know if the End_Date is
103 -- a leap second, the comparison should be:
105 -- End_Date >= Next_Leap_Sec;
107 -- After_Last_Leap is designed so that this comparison works without
108 -- having to first check if Next_Leap_Sec is a valid leap second.
110 function To_Duration (T : Time) return Duration;
111 function To_Relative_Time (D : Duration) return Time;
112 -- It is important to note that duration's fractional part denotes nano
113 -- seconds while the units of Time are 100 nanoseconds. If a regular
114 -- Unchecked_Conversion was employed, the resulting values would be off
117 --------------------------
118 -- Leap seconds control --
119 --------------------------
122 pragma Import (C, Flag, "__gl_leap_seconds_support");
123 -- This imported value is used to determine whether the compilation had
124 -- binder flag "-y" present which enables leap seconds. A value of zero
125 -- signifies no leap seconds support while a value of one enables the
128 Leap_Support : constant Boolean := Flag = 1;
129 -- The above flag controls the usage of leap seconds in all Ada.Calendar
132 Leap_Seconds_Count : constant Natural := 23;
134 ---------------------
135 -- Local Constants --
136 ---------------------
138 -- The range of Ada time expressed as milis since the VMS Epoch
140 Ada_Low : constant Time := (10 * 366 + 32 * 365 + 45) * Milis_In_Day;
141 Ada_High : constant Time := (131 * 366 + 410 * 365 + 45) * Milis_In_Day;
143 -- Even though the upper bound of time is 2399-12-31 23:59:59.9999999
144 -- UTC, it must be increased to include all leap seconds.
146 Ada_High_And_Leaps : constant Time :=
147 Ada_High + Time (Leap_Seconds_Count) * Mili;
149 -- Two constants used in the calculations of elapsed leap seconds.
150 -- End_Of_Time is later than Ada_High in time zone -28. Start_Of_Time
151 -- is earlier than Ada_Low in time zone +28.
153 End_Of_Time : constant Time := Ada_High + Time (3) * Milis_In_Day;
154 Start_Of_Time : constant Time := Ada_Low - Time (3) * Milis_In_Day;
156 -- The following table contains the hard time values of all existing leap
157 -- seconds. The values are produced by the utility program xleaps.adb.
159 Leap_Second_Times : constant array (1 .. Leap_Seconds_Count) of Time :=
188 function "+" (Left : Time; Right : Duration) return Time is
189 pragma Unsuppress (Overflow_Check);
191 return Left + To_Relative_Time (Right);
193 when Constraint_Error =>
197 function "+" (Left : Duration; Right : Time) return Time is
198 pragma Unsuppress (Overflow_Check);
202 when Constraint_Error =>
210 function "-" (Left : Time; Right : Duration) return Time is
211 pragma Unsuppress (Overflow_Check);
213 return Left - To_Relative_Time (Right);
215 when Constraint_Error =>
219 function "-" (Left : Time; Right : Time) return Duration is
220 pragma Unsuppress (Overflow_Check);
222 -- The bound of type Duration expressed as time
224 Dur_High : constant Time := To_Relative_Time (Duration'Last);
225 Dur_Low : constant Time := To_Relative_Time (Duration'First);
230 Res_M := Left - Right;
232 -- Due to the extended range of Ada time, "-" is capable of producing
233 -- results which may exceed the range of Duration. In order to prevent
234 -- the generation of bogus values by the Unchecked_Conversion, we apply
235 -- the following check.
238 or else Res_M >= Dur_High
242 -- Normal case, result fits
245 return To_Duration (Res_M);
249 when Constraint_Error =>
257 function "<" (Left, Right : Time) return Boolean is
259 return Long_Integer (Left) < Long_Integer (Right);
266 function "<=" (Left, Right : Time) return Boolean is
268 return Long_Integer (Left) <= Long_Integer (Right);
275 function ">" (Left, Right : Time) return Boolean is
277 return Long_Integer (Left) > Long_Integer (Right);
284 function ">=" (Left, Right : Time) return Boolean is
286 return Long_Integer (Left) >= Long_Integer (Right);
289 ------------------------------
290 -- Check_Within_Time_Bounds --
291 ------------------------------
293 procedure Check_Within_Time_Bounds (T : Time) is
296 if T < Ada_Low or else T > Ada_High_And_Leaps then
300 if T < Ada_Low or else T > Ada_High then
304 end Check_Within_Time_Bounds;
310 function Clock return Time is
311 Elapsed_Leaps : Natural;
313 Res_M : constant Time := Time (OSP.OS_Clock);
316 -- Note that on other targets a soft-link is used to get a different
317 -- clock depending whether tasking is used or not. On VMS this isn't
318 -- needed since all clock calls end up using SYS$GETTIM, so call the
319 -- OS_Primitives version for efficiency.
321 -- If the target supports leap seconds, determine the number of leap
322 -- seconds elapsed until this moment.
325 Cumulative_Leap_Seconds
326 (Start_Of_Time, Res_M, Elapsed_Leaps, Next_Leap_M);
328 -- The system clock may fall exactly on a leap second
330 if Res_M >= Next_Leap_M then
331 Elapsed_Leaps := Elapsed_Leaps + 1;
334 -- The target does not support leap seconds
340 return Res_M + Time (Elapsed_Leaps) * Mili;
343 -----------------------------
344 -- Cumulative_Leap_Seconds --
345 -----------------------------
347 procedure Cumulative_Leap_Seconds
350 Elapsed_Leaps : out Natural;
351 Next_Leap_Sec : out Time)
353 End_Index : Positive;
354 End_T : Time := End_Date;
355 Start_Index : Positive;
356 Start_T : Time := Start_Date;
359 pragma Assert (Leap_Support and then End_Date >= Start_Date);
361 Next_Leap_Sec := End_Of_Time;
363 -- Make sure that the end date does not excede the upper bound
366 if End_Date > Ada_High then
370 -- Remove the sub seconds from both dates
372 Start_T := Start_T - (Start_T mod Mili);
373 End_T := End_T - (End_T mod Mili);
375 -- Some trivial cases:
376 -- Leap 1 . . . Leap N
377 -- ---+========+------+############+-------+========+-----
378 -- Start_T End_T Start_T End_T
380 if End_T < Leap_Second_Times (1) then
382 Next_Leap_Sec := Leap_Second_Times (1);
385 elsif Start_T > Leap_Second_Times (Leap_Seconds_Count) then
387 Next_Leap_Sec := End_Of_Time;
391 -- Perform the calculations only if the start date is within the leap
392 -- second occurences table.
394 if Start_T <= Leap_Second_Times (Leap_Seconds_Count) then
397 -- +----+----+-- . . . --+-------+---+
398 -- | T1 | T2 | | N - 1 | N |
399 -- +----+----+-- . . . --+-------+---+
401 -- | Start_Index | End_Index
402 -- +-------------------+
405 -- The idea behind the algorithm is to iterate and find two closest
406 -- dates which are after Start_T and End_T. Their corresponding
407 -- index difference denotes the number of leap seconds elapsed.
411 exit when Leap_Second_Times (Start_Index) >= Start_T;
412 Start_Index := Start_Index + 1;
415 End_Index := Start_Index;
417 exit when End_Index > Leap_Seconds_Count
418 or else Leap_Second_Times (End_Index) >= End_T;
419 End_Index := End_Index + 1;
422 if End_Index <= Leap_Seconds_Count then
423 Next_Leap_Sec := Leap_Second_Times (End_Index);
426 Elapsed_Leaps := End_Index - Start_Index;
431 end Cumulative_Leap_Seconds;
437 function Day (Date : Time) return Day_Number is
443 Split (Date, Y, M, D, S);
451 function Is_Leap (Year : Year_Number) return Boolean is
453 -- Leap centenial years
455 if Year mod 400 = 0 then
458 -- Non-leap centenial years
460 elsif Year mod 100 = 0 then
466 return Year mod 4 = 0;
474 function Month (Date : Time) return Month_Number is
480 Split (Date, Y, M, D, S);
488 function Seconds (Date : Time) return Day_Duration is
494 Split (Date, Y, M, D, S);
504 Year : out Year_Number;
505 Month : out Month_Number;
506 Day : out Day_Number;
507 Seconds : out Day_Duration)
516 -- Use UTC as the local time zone on VMS, the status of flag Is_Ada_05
517 -- is irrelevant in this case.
519 Formatting_Operations.Split
536 or else not Month'Valid
537 or else not Day'Valid
538 or else not Seconds'Valid
550 Month : Month_Number;
552 Seconds : Day_Duration := 0.0) return Time
554 -- The values in the following constants are irrelevant, they are just
555 -- placeholders; the choice of constructing a Day_Duration value is
556 -- controlled by the Use_Day_Secs flag.
558 H : constant Integer := 1;
559 M : constant Integer := 1;
560 Se : constant Integer := 1;
561 Ss : constant Duration := 0.1;
565 or else not Month'Valid
566 or else not Day'Valid
567 or else not Seconds'Valid
572 -- Use UTC as the local time zone on VMS, the status of flag Is_Ada_05
573 -- is irrelevant in this case.
576 Formatting_Operations.Time_Of
586 Use_Day_Secs => True,
595 function To_Duration (T : Time) return Duration is
596 function Time_To_Duration is
597 new Ada.Unchecked_Conversion (Time, Duration);
599 return Time_To_Duration (T * 100);
602 ----------------------
603 -- To_Relative_Time --
604 ----------------------
606 function To_Relative_Time (D : Duration) return Time is
607 function Duration_To_Time is
608 new Ada.Unchecked_Conversion (Duration, Time);
610 return Duration_To_Time (D / 100.0);
611 end To_Relative_Time;
617 function Year (Date : Time) return Year_Number is
623 Split (Date, Y, M, D, S);
627 -- The following packages assume that Time is a Long_Integer, the units
628 -- are 100 nanoseconds and the starting point in the VMS Epoch.
630 ---------------------------
631 -- Arithmetic_Operations --
632 ---------------------------
634 package body Arithmetic_Operations is
640 function Add (Date : Time; Days : Long_Integer) return Time is
641 pragma Unsuppress (Overflow_Check);
643 return Date + Time (Days) * Milis_In_Day;
645 when Constraint_Error =>
656 Days : out Long_Integer;
657 Seconds : out Duration;
658 Leap_Seconds : out Integer)
660 Mili_F : constant Duration := 10_000_000.0;
665 Elapsed_Leaps : Natural;
667 Negate : Boolean := False;
669 Sub_Seconds : Duration;
672 -- This classification is necessary in order to avoid a Time_Error
673 -- being raised by the arithmetic operators in Ada.Calendar.
675 if Left >= Right then
684 -- If the target supports leap seconds, process them
687 Cumulative_Leap_Seconds
688 (Earlier, Later, Elapsed_Leaps, Next_Leap);
690 if Later >= Next_Leap then
691 Elapsed_Leaps := Elapsed_Leaps + 1;
694 -- The target does not support leap seconds
700 Diff_M := Later - Earlier - Time (Elapsed_Leaps) * Mili;
702 -- Sub second processing
704 Sub_Seconds := Duration (Diff_M mod Mili) / Mili_F;
706 -- Convert to seconds. Note that his action eliminates the sub
707 -- seconds automatically.
709 Diff_S := Diff_M / Mili;
711 Days := Long_Integer (Diff_S / Secs_In_Day);
712 Seconds := Duration (Diff_S mod Secs_In_Day) + Sub_Seconds;
713 Leap_Seconds := Integer (Elapsed_Leaps);
719 if Leap_Seconds /= 0 then
720 Leap_Seconds := -Leap_Seconds;
729 function Subtract (Date : Time; Days : Long_Integer) return Time is
730 pragma Unsuppress (Overflow_Check);
732 return Date - Time (Days) * Milis_In_Day;
734 when Constraint_Error =>
737 end Arithmetic_Operations;
739 ---------------------------
740 -- Formatting_Operations --
741 ---------------------------
743 package body Formatting_Operations is
749 function Day_Of_Week (Date : Time) return Integer is
755 Day_Count : Long_Integer;
756 Midday_Date_S : Time;
759 Split (Date, Y, M, D, S);
761 -- Build a time value in the middle of the same day and convert the
762 -- time value to seconds.
764 Midday_Date_S := Time_Of (Y, M, D, 43_200.0) / Mili;
766 -- Count the number of days since the start of VMS time. 1858-11-17
769 Day_Count := Long_Integer (Midday_Date_S / Secs_In_Day) + 2;
771 return Integer (Day_Count mod 7);
780 Year : out Year_Number;
781 Month : out Month_Number;
782 Day : out Day_Number;
783 Day_Secs : out Day_Duration;
785 Minute : out Integer;
786 Second : out Integer;
787 Sub_Sec : out Duration;
788 Leap_Sec : out Boolean;
790 Time_Zone : Long_Integer)
792 -- The flag Is_Ada_05 is present for interfacing purposes
794 pragma Unreferenced (Is_Ada_05);
797 (Status : out Unsigned_Longword;
798 Timbuf : out Unsigned_Word_Array;
801 pragma Interface (External, Numtim);
803 pragma Import_Valued_Procedure
804 (Numtim, "SYS$NUMTIM",
805 (Unsigned_Longword, Unsigned_Word_Array, Time),
806 (Value, Reference, Reference));
808 Status : Unsigned_Longword;
809 Timbuf : Unsigned_Word_Array (1 .. 7);
811 Ada_Min_Year : constant := 1901;
812 Ada_Max_Year : constant := 2399;
813 Mili_F : constant Duration := 10_000_000.0;
816 Elapsed_Leaps : Natural;
822 -- Step 1: Leap seconds processing
825 Cumulative_Leap_Seconds
826 (Start_Of_Time, Date, Elapsed_Leaps, Next_Leap_M);
828 Leap_Sec := Date_M >= Next_Leap_M;
831 Elapsed_Leaps := Elapsed_Leaps + 1;
834 -- The target does not support leap seconds
841 Date_M := Date_M - Time (Elapsed_Leaps) * Mili;
843 -- Step 2: Time zone processing
845 if Time_Zone /= 0 then
846 Date_M := Date_M + Time (Time_Zone) * 60 * Mili;
849 -- After the leap seconds and time zone have been accounted for,
850 -- the date should be within the bounds of Ada time.
853 or else Date_M > Ada_High
858 -- Step 3: Sub second processing
860 Sub_Sec := Duration (Date_M mod Mili) / Mili_F;
862 -- Drop the sub seconds
864 Date_M := Date_M - (Date_M mod Mili);
866 -- Step 4: VMS system call
868 Numtim (Status, Timbuf, Date_M);
871 or else Timbuf (1) not in Ada_Min_Year .. Ada_Max_Year
876 -- Step 5: Time components processing
878 Year := Year_Number (Timbuf (1));
879 Month := Month_Number (Timbuf (2));
880 Day := Day_Number (Timbuf (3));
881 Hour := Integer (Timbuf (4));
882 Minute := Integer (Timbuf (5));
883 Second := Integer (Timbuf (6));
885 Day_Secs := Day_Duration (Hour * 3_600) +
886 Day_Duration (Minute * 60) +
887 Day_Duration (Second) +
897 Month : Month_Number;
899 Day_Secs : Day_Duration;
905 Use_Day_Secs : Boolean;
907 Time_Zone : Long_Integer) return Time
910 (Status : out Unsigned_Longword;
911 Input_Time : Unsigned_Word_Array;
912 Resultant_Time : out Time);
914 pragma Interface (External, Cvt_Vectim);
916 pragma Import_Valued_Procedure
917 (Cvt_Vectim, "LIB$CVT_VECTIM",
918 (Unsigned_Longword, Unsigned_Word_Array, Time),
919 (Value, Reference, Reference));
921 Status : Unsigned_Longword;
922 Timbuf : Unsigned_Word_Array (1 .. 7);
924 Mili_F : constant := 10_000_000.0;
926 Y : Year_Number := Year;
927 Mo : Month_Number := Month;
928 D : Day_Number := Day;
930 Mi : Integer := Minute;
931 Se : Integer := Second;
932 Su : Duration := Sub_Sec;
934 Elapsed_Leaps : Natural;
935 Int_Day_Secs : Integer;
938 Rounded_Res_M : Time;
941 -- No validity checks are performed on the input values since it is
942 -- assumed that the called has already performed them.
944 -- Step 1: Hour, minute, second and sub second processing
948 -- A day seconds value of 86_400 designates a new day
950 if Day_Secs = 86_400.0 then
952 Adj_Year : Year_Number := Year;
953 Adj_Month : Month_Number := Month;
954 Adj_Day : Day_Number := Day;
957 if Day < Days_In_Month (Month)
959 and then Is_Leap (Year))
963 -- The day adjustment moves the date to a new month
969 Adj_Month := Month + 1;
971 -- The month adjustment moves the date to a new year
975 Adj_Year := Year + 1;
988 -- Normal case (not exactly one day)
991 -- Sub second extraction
993 if Day_Secs > 0.0 then
994 Int_Day_Secs := Integer (Day_Secs - 0.5);
996 Int_Day_Secs := Integer (Day_Secs);
999 H := Int_Day_Secs / 3_600;
1000 Mi := (Int_Day_Secs / 60) mod 60;
1001 Se := Int_Day_Secs mod 60;
1002 Su := Day_Secs - Duration (Int_Day_Secs);
1006 -- Step 2: System call to VMS
1008 Timbuf (1) := Unsigned_Word (Y);
1009 Timbuf (2) := Unsigned_Word (Mo);
1010 Timbuf (3) := Unsigned_Word (D);
1011 Timbuf (4) := Unsigned_Word (H);
1012 Timbuf (5) := Unsigned_Word (Mi);
1013 Timbuf (6) := Unsigned_Word (Se);
1016 Cvt_Vectim (Status, Timbuf, Res_M);
1018 if Status mod 2 /= 1 then
1022 -- Step 3: Sub second adjustment
1024 Res_M := Res_M + Time (Su * Mili_F);
1026 -- Step 4: Bounds check
1028 Check_Within_Time_Bounds (Res_M);
1030 -- Step 5: Time zone processing
1032 if Time_Zone /= 0 then
1033 Res_M := Res_M - Time (Time_Zone) * 60 * Mili;
1036 -- Step 6: Leap seconds processing
1038 if Leap_Support then
1039 Cumulative_Leap_Seconds
1040 (Start_Of_Time, Res_M, Elapsed_Leaps, Next_Leap_M);
1042 Res_M := Res_M + Time (Elapsed_Leaps) * Mili;
1044 -- An Ada 2005 caller requesting an explicit leap second or an
1045 -- Ada 95 caller accounting for an invisible leap second.
1048 or else Res_M >= Next_Leap_M
1050 Res_M := Res_M + Time (1) * Mili;
1053 -- Leap second validity check
1055 Rounded_Res_M := Res_M - (Res_M mod Mili);
1059 and then Rounded_Res_M /= Next_Leap_M
1067 end Formatting_Operations;
1069 ---------------------------
1070 -- Time_Zones_Operations --
1071 ---------------------------
1073 package body Time_Zones_Operations is
1075 ---------------------
1076 -- UTC_Time_Offset --
1077 ---------------------
1079 function UTC_Time_Offset (Date : Time) return Long_Integer is
1080 -- Formal parameter Date is here for interfacing, but is never
1083 pragma Unreferenced (Date);
1085 function get_gmtoff return Long_Integer;
1086 pragma Import (C, get_gmtoff, "get_gmtoff");
1089 -- VMS is not capable of determining the time zone in some past or
1090 -- future point in time denoted by Date, thus the current time zone
1094 end UTC_Time_Offset;
1095 end Time_Zones_Operations;